Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff format for qmds #473

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -25,12 +25,12 @@ repos:
hooks:
# Sort imports
- id: ruff
args: ['check', '--select', 'I', '--fix']
args: ["check", "--select", "I", "--fix"]
# Run the linter
- id: ruff
# Run the formatter
- id: ruff-format
args: ['--line-length', '79']
args: ["--line-length", "79"]
- repo: https://github.com/numpy/numpydoc
rev: v1.7.0
hooks:
@@ -44,18 +44,14 @@ repos:
entry: python hook_scripts/quarto_python_formatter.py "-q --line-length 79"
language: python
files: \.qmd$
additional_dependencies: [black]
additional_dependencies: [ruff]
#####
# Secrets
- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
- id: detect-secrets
args:
[
"--baseline",
".secrets.baseline",
]
args: ["--baseline", ".secrets.baseline"]
exclude: package.lock.json
####
# Typos
1 change: 0 additions & 1 deletion docs/source/tutorials/basic_renewal_model.qmd
Original file line number Diff line number Diff line change
@@ -131,7 +131,6 @@ I0 = InfectionInitializationProcess(
# (3) The random walk on log Rt, with an inferred s.d. Here, we
# construct a custom RandomVariable.
class MyRt(RandomVariable):

def validate(self):
pass

1 change: 0 additions & 1 deletion docs/source/tutorials/extending_pyrenew.qmd
Original file line number Diff line number Diff line change
@@ -64,7 +64,6 @@ latent_infections = InfectionsWithFeedback(


class MyRt(RandomVariable):

def validate(self):
pass

1 change: 0 additions & 1 deletion docs/source/tutorials/hospital_admissions_model.qmd
Original file line number Diff line number Diff line change
@@ -186,7 +186,6 @@ gen_int = deterministic.DeterministicPMF(name="gen_int", value=gen_int)


class MyRt(metaclass.RandomVariable):

def validate(self):
pass

22 changes: 11 additions & 11 deletions hook_scripts/quarto_python_formatter.py
Original file line number Diff line number Diff line change
@@ -9,11 +9,11 @@


def format_python_code(
code: str, black_args: List[str]
code: str, ruff_args: List[str]
) -> str: # numpydoc ignore=RT01
"""Format Python code using Black with custom arguments."""
"""Format Python code using Ruff with custom arguments."""
try:
cmd = ["black", "-"] + black_args
cmd = ["ruff", "format", "-"] + ruff_args
result = subprocess.run(
cmd,
input=code,
@@ -24,28 +24,28 @@ def format_python_code(
return result.stdout
except subprocess.CalledProcessError:
print(
"Error: Failed to format Python code with Black.", file=sys.stderr
"Error: Failed to format Python code with Ruff.", file=sys.stderr
)
return code


def replace_code_block(
match: Match[str], black_args: List[str]
match: Match[str], ruff_args: List[str]
) -> str: # numpydoc ignore=RT01
"""Replace code block with formatted version."""
return f"{match.group(1)}\n{format_python_code(match.group(2), black_args)}{match.group(3)}"
return f"{match.group(1)}\n{format_python_code(match.group(2), ruff_args)}{match.group(3)}"


def process_file(
filepath: Path, black_args: List[str]
filepath: Path, ruff_args: List[str]
) -> None: # numpydoc ignore=RT01
"""Process the given file, formatting Python code blocks."""
python_code_block_pattern = r"(```\{python\})(.*?)(```)"
try:
content = filepath.read_text()
formatted_content = re.sub(
python_code_block_pattern,
lambda m: replace_code_block(m, black_args),
lambda m: replace_code_block(m, ruff_args),
content,
flags=re.DOTALL,
)
@@ -63,11 +63,11 @@ def process_file(
if __name__ == "__main__":
if len(sys.argv) < 3:
print(
'Usage: python hook_scripts/quarto_python_formatter.py "BLACK_ARGS" <filename1.qmd> [filename2.qmd ...]'
'Usage: python hook_scripts/quarto_python_formatter.py "RUFF_ARGS" <filename1.qmd> [filename2.qmd ...]'
)
sys.exit(1)

black_args = sys.argv[1].split()
ruff_args = sys.argv[1].split()

missing_files = [file for file in sys.argv[2:] if not Path(file).exists()]
if missing_files:
@@ -76,4 +76,4 @@ def process_file(
)
for filepath in sys.argv[2:]:
path = Path(filepath)
process_file(path, black_args)
process_file(path, ruff_args)
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -86,3 +86,10 @@ known_first_party = ["pyrenew", "test"]

[tool.deptry.per_rule_ignores]
DEP004 = ["pytest", "scipy"]

[tool.ruff]
fix = true
line-length = 79

[tool.ruff.lint]
select = ["I"]