Skip to content

Commit a1466ae

Browse files
authored
Use ruff format for qmds (#473)
* use ruff instead of black when formatting qmd * format qmd's * add ruff config to pyproject
1 parent 32e80fd commit a1466ae

6 files changed

Lines changed: 22 additions & 22 deletions

File tree

.pre-commit-config.yaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ repos:
2525
hooks:
2626
# Sort imports
2727
- id: ruff
28-
args: ['check', '--select', 'I', '--fix']
28+
args: ["check", "--select", "I", "--fix"]
2929
# Run the linter
3030
- id: ruff
3131
# Run the formatter
3232
- id: ruff-format
33-
args: ['--line-length', '79']
33+
args: ["--line-length", "79"]
3434
- repo: https://github.com/numpy/numpydoc
3535
rev: v1.7.0
3636
hooks:
@@ -44,18 +44,14 @@ repos:
4444
entry: python hook_scripts/quarto_python_formatter.py "-q --line-length 79"
4545
language: python
4646
files: \.qmd$
47-
additional_dependencies: [black]
47+
additional_dependencies: [ruff]
4848
#####
4949
# Secrets
5050
- repo: https://github.com/Yelp/detect-secrets
5151
rev: v1.4.0
5252
hooks:
5353
- id: detect-secrets
54-
args:
55-
[
56-
"--baseline",
57-
".secrets.baseline",
58-
]
54+
args: ["--baseline", ".secrets.baseline"]
5955
exclude: package.lock.json
6056
####
6157
# Typos

docs/source/tutorials/basic_renewal_model.qmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ I0 = InfectionInitializationProcess(
131131
# (3) The random walk on log Rt, with an inferred s.d. Here, we
132132
# construct a custom RandomVariable.
133133
class MyRt(RandomVariable):
134-
135134
def validate(self):
136135
pass
137136

docs/source/tutorials/extending_pyrenew.qmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ latent_infections = InfectionsWithFeedback(
6464
6565
6666
class MyRt(RandomVariable):
67-
6867
def validate(self):
6968
pass
7069

docs/source/tutorials/hospital_admissions_model.qmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ gen_int = deterministic.DeterministicPMF(name="gen_int", value=gen_int)
186186
187187
188188
class MyRt(metaclass.RandomVariable):
189-
190189
def validate(self):
191190
pass
192191

hook_scripts/quarto_python_formatter.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010

1111
def format_python_code(
12-
code: str, black_args: List[str]
12+
code: str, ruff_args: List[str]
1313
) -> str: # numpydoc ignore=RT01
14-
"""Format Python code using Black with custom arguments."""
14+
"""Format Python code using Ruff with custom arguments."""
1515
try:
16-
cmd = ["black", "-"] + black_args
16+
cmd = ["ruff", "format", "-"] + ruff_args
1717
result = subprocess.run(
1818
cmd,
1919
input=code,
@@ -24,28 +24,28 @@ def format_python_code(
2424
return result.stdout
2525
except subprocess.CalledProcessError:
2626
print(
27-
"Error: Failed to format Python code with Black.", file=sys.stderr
27+
"Error: Failed to format Python code with Ruff.", file=sys.stderr
2828
)
2929
return code
3030

3131

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

3838

3939
def process_file(
40-
filepath: Path, black_args: List[str]
40+
filepath: Path, ruff_args: List[str]
4141
) -> None: # numpydoc ignore=RT01
4242
"""Process the given file, formatting Python code blocks."""
4343
python_code_block_pattern = r"(```\{python\})(.*?)(```)"
4444
try:
4545
content = filepath.read_text()
4646
formatted_content = re.sub(
4747
python_code_block_pattern,
48-
lambda m: replace_code_block(m, black_args),
48+
lambda m: replace_code_block(m, ruff_args),
4949
content,
5050
flags=re.DOTALL,
5151
)
@@ -63,11 +63,11 @@ def process_file(
6363
if __name__ == "__main__":
6464
if len(sys.argv) < 3:
6565
print(
66-
'Usage: python hook_scripts/quarto_python_formatter.py "BLACK_ARGS" <filename1.qmd> [filename2.qmd ...]'
66+
'Usage: python hook_scripts/quarto_python_formatter.py "RUFF_ARGS" <filename1.qmd> [filename2.qmd ...]'
6767
)
6868
sys.exit(1)
6969

70-
black_args = sys.argv[1].split()
70+
ruff_args = sys.argv[1].split()
7171

7272
missing_files = [file for file in sys.argv[2:] if not Path(file).exists()]
7373
if missing_files:
@@ -76,4 +76,4 @@ def process_file(
7676
)
7777
for filepath in sys.argv[2:]:
7878
path = Path(filepath)
79-
process_file(path, black_args)
79+
process_file(path, ruff_args)

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ known_first_party = ["pyrenew", "test"]
8686

8787
[tool.deptry.per_rule_ignores]
8888
DEP004 = ["pytest", "scipy"]
89+
90+
[tool.ruff]
91+
fix = true
92+
line-length = 79
93+
94+
[tool.ruff.lint]
95+
select = ["I"]

0 commit comments

Comments
 (0)