From a1466ae2f2c953214aef543178f3a51238f2f48e Mon Sep 17 00:00:00 2001 From: Damon Bayer Date: Thu, 3 Oct 2024 17:49:34 -0500 Subject: [PATCH] Use `ruff format` for `qmd`s (#473) * use ruff instead of black when formatting qmd * format qmd's * add ruff config to pyproject --- .pre-commit-config.yaml | 12 ++++------ docs/source/tutorials/basic_renewal_model.qmd | 1 - docs/source/tutorials/extending_pyrenew.qmd | 1 - .../tutorials/hospital_admissions_model.qmd | 1 - hook_scripts/quarto_python_formatter.py | 22 +++++++++---------- pyproject.toml | 7 ++++++ 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5144226e..f844d107 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/docs/source/tutorials/basic_renewal_model.qmd b/docs/source/tutorials/basic_renewal_model.qmd index b5fff380..985a053e 100644 --- a/docs/source/tutorials/basic_renewal_model.qmd +++ b/docs/source/tutorials/basic_renewal_model.qmd @@ -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 diff --git a/docs/source/tutorials/extending_pyrenew.qmd b/docs/source/tutorials/extending_pyrenew.qmd index 3834d858..77a83a53 100644 --- a/docs/source/tutorials/extending_pyrenew.qmd +++ b/docs/source/tutorials/extending_pyrenew.qmd @@ -64,7 +64,6 @@ latent_infections = InfectionsWithFeedback( class MyRt(RandomVariable): - def validate(self): pass diff --git a/docs/source/tutorials/hospital_admissions_model.qmd b/docs/source/tutorials/hospital_admissions_model.qmd index 0acebc6b..e8211a9b 100644 --- a/docs/source/tutorials/hospital_admissions_model.qmd +++ b/docs/source/tutorials/hospital_admissions_model.qmd @@ -186,7 +186,6 @@ gen_int = deterministic.DeterministicPMF(name="gen_int", value=gen_int) class MyRt(metaclass.RandomVariable): - def validate(self): pass diff --git a/hook_scripts/quarto_python_formatter.py b/hook_scripts/quarto_python_formatter.py index 54149c85..7a290583 100755 --- a/hook_scripts/quarto_python_formatter.py +++ b/hook_scripts/quarto_python_formatter.py @@ -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,20 +24,20 @@ 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\})(.*?)(```)" @@ -45,7 +45,7 @@ def process_file( 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" [filename2.qmd ...]' + 'Usage: python hook_scripts/quarto_python_formatter.py "RUFF_ARGS" [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) diff --git a/pyproject.toml b/pyproject.toml index 23afcd89..bf9a7656 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"]