From 318dff8fc8787712488acd4068a30057c7ad1002 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Sat, 28 Nov 2026 04:37:02 +0100 Subject: [PATCH] Add `ruff` to the "lint" session `ruff` runs alongside `flake8` and `isort`, with the rules aligned to theirs: - `noqa` comments go where both tools expect them - `import`s stay sorted case-insensitively, now stated explicitly in both tools - `print()` in the "docs" session became `session.log()` --- noxfile.py | 10 ++- pyproject.toml | 125 ++++++++++++++++++++++++++- src/velofy/console/decorators.py | 2 +- src/velofy/console/forecasts.py | 2 +- src/velofy/db/addresses_addresses.py | 2 +- src/velofy/db/addresses_pixels.py | 2 +- src/velofy/db/cities.py | 4 +- src/velofy/forecasts/timify.py | 4 +- uv.lock | 27 ++++++ 9 files changed, 166 insertions(+), 12 deletions(-) diff --git a/noxfile.py b/noxfile.py index 36fcb3a..d1f7248 100644 --- a/noxfile.py +++ b/noxfile.py @@ -3,7 +3,7 @@ By default, `uv run nox` runs the sessions that assure code quality: - "format": Format the source files with `autoflake`, `black`, and `isort` -- "lint": Lint the source files with `flake8` and `isort` +- "lint": Lint the source files with `flake8`, `isort`, and `ruff` - "test": Run the test suite with `pytest` - "doctest": Check if code snippets in the docstrings run @@ -169,7 +169,7 @@ def format_code(session: Session) -> None: @nox.session(name="lint", python=MAIN_PYTHON) def lint_code(session: Session) -> None: - """Lint source files with `flake8` and `isort`. + """Lint source files with `flake8`, `isort`, and `ruff`. If no extra arguments are provided, all source files are linted. Otherwise, they are interpreted as paths the linters work on recursively. @@ -201,6 +201,7 @@ def lint_code(session: Session) -> None: # 'mypy', "pep8-naming", "pydoclint[flake8]", + "ruff", ) # Interpret extra arguments as `locations` of source files @@ -214,6 +215,9 @@ def lint_code(session: Session) -> None: session.run("isort", "--version") session.run("isort", "--check-only", *locations) + session.run("ruff", "--version") + session.run("ruff", "check", *locations) + # TODO: Include `mypy` once it passes # - Only for the *.py files to be packaged # - It then needs the developed package and its dependencies installed @@ -306,7 +310,7 @@ def build_docs(session: Session) -> None: # Verify that all external links return 200 OK session.run("sphinx-build", "-b", "linkcheck", DOCS_LOCATION, DOCS_BUILD) - print(f"Docs are available at {os.getcwd()}/{DOCS_BUILD}index.html") + session.log(f"Docs are available at {os.getcwd()}/{DOCS_BUILD}index.html") @nox.session(name="venv", python=MAIN_PYTHON, venv_backend="none") diff --git a/pyproject.toml b/pyproject.toml index 3a2100c..818bad1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -108,6 +108,7 @@ dev = [ "mypy (>=2.4.1,<3.0.0)", "pep8-naming (>=0.15.1,<0.16.0)", "pydoclint[flake8] (>=0.12.0,<0.13.0)", + "ruff (>=0.17.6,<0.18.0)", ] test = [ @@ -315,7 +316,7 @@ pytest-parametrize-values-type = "list" # Source: https://pycqa.github.io/isort/docs/configuration/options.html atomic = true -case_sensitive = true +case_sensitive = false # Implied by "force_alphabetical_sort_within_sections" combine_star = true force_alphabetical_sort_within_sections = true lines_after_imports = 2 @@ -418,6 +419,128 @@ markers = [ +[tool.ruff] +# Source: https://docs.astral.sh/ruff/ + +cache-dir = ".cache/ruff" + +src = ["src"] + +target-version = "py313" + +line-length = 88 + + +[tool.ruff.format] + +# Align with black +quote-style = "double" +skip-magic-trailing-comma = false + + +[tool.ruff.lint] # aligned with [tool.flake8] above + +select = [ + # =================================== + # Violations also covered by `flake8` + # =================================== + # flake8-annotations => enforce type checking for functions + "ANN0", "ANN2", + # flake8-unused-arguments ("U100") => declared function arguments must be used + "ARG", + # flake8-bugbear => opinionated bugs and design flaws + "B", + # flake8-comprehensions => better comprehensions + "C4", + # mccabe => cyclomatic complexity + "C90", + # flake8-commas ("C8") => better comma placements + "COM", + # flake8-docstrings => PEP257 compliance + "D", + # pycodestyle => PEP8 compliance + "E", "W", + # pyflakes => basic errors + "F", + # pep8-naming => PEP8 naming conventions + "N", + # flake8-pytest-style => enforce a consistent style with pytest + "PT", + # flake8-quotes => use double quotes everywhere + "Q", + # flake8-bandit => common security issues + "S", + # flake8-debugger => no debugger usage + "T10", + # flake8-todos => unify TODOs + "TD", + # ======================================= + # Violations not covered by `flake8` above + # ======================================= + # isort => imports are sorted + "I", + # flake8-print => no `print()` + "T20", +] + +extend-ignore = [ + # Let the formatter handle trailing commas. + "COM812", + # Let's not do `@pytest.mark.no_cover()` instead of `@pytest.mark.no_cover`. + "PT023", + # TODOs need neither an author nor an issue. + "TD002", "TD003", +] + + +[tool.ruff.lint.per-file-ignores] + +# Top-levels of a sub-packages are intended to import a lot. +"**/__init__.py" = ["F401"] +# Type annotations are not strictly enforced. +"migrations/env.py" = ["ANN0", "ANN2"] +# - ANN0, ANN2 => Type annotations are not strictly enforced. +# - S608 => Do not worry about SQL injection here. +"migrations/versions/*.py" = ["ANN0", "ANN2", "S608"] +# Type annotations are not strictly enforced. +"noxfile.py" = ["ANN0", "ANN2"] +# `print()` is ok in ad-hoc scripts +"scripts/**" = ["T20"] +# - ANN0, ANN2 => Type annotations are not strictly enforced. +# - D106 => The `Meta` class inside the factory_boy models do not need a docstring. +# - S101 => `assert` statements are ok in the test suite. +# - S311 => The `random` module is not used for cryptography. +"tests/**/*.py" = ["ANN0", "ANN2", "D106", "S101", "S311"] + + +[tool.ruff.lint.flake8-pytest-style] # aligned with [tool.flake8] above + +fixture-parentheses = false +parametrize-names-type = "list" +parametrize-values-row-type = "tuple" +parametrize-values-type = "list" + + +[tool.ruff.lint.isort] # aligned with [tool.isort] above + +case-sensitive = false # Same as `isort` +force-single-line = true +lines-after-imports = 2 +order-by-type = false +single-line-exclusions = ["typing"] + + +[tool.ruff.lint.mccabe] + +max-complexity = 10 + + +[tool.ruff.lint.pydocstyle] + +convention = "google" + + + [tool.uv] # Source: https://docs.astral.sh/uv/reference/settings/ diff --git a/src/velofy/console/decorators.py b/src/velofy/console/decorators.py index 7c6fd6d..513882a 100644 --- a/src/velofy/console/decorators.py +++ b/src/velofy/console/decorators.py @@ -20,7 +20,7 @@ def db_revision( """Do not execute the `func` if the revision does not match.""" if not os.getenv("TESTING"): result = subprocess.run( # noqa:S603,S607 - ["alembic", "current"], + ["alembic", "current"], # noqa:S607 capture_output=True, check=False, encoding="utf8", diff --git a/src/velofy/console/forecasts.py b/src/velofy/console/forecasts.py index 6908693..75864e5 100644 --- a/src/velofy/console/forecasts.py +++ b/src/velofy/console/forecasts.py @@ -24,7 +24,7 @@ from velofy.forecasts import timify @click.argument("time_step", default=60, type=int) @click.argument("train_horizon", default=8, type=int) @decorators.db_revision("8bfb928a31f8") -def tactical_heuristic( # noqa:C901,DOC001 +def tactical_heuristic( # noqa:C901,D417,DOC001 city: str, side_length: int, time_step: int, diff --git a/src/velofy/db/addresses_addresses.py b/src/velofy/db/addresses_addresses.py index b23847d..5bb2c6e 100644 --- a/src/velofy/db/addresses_addresses.py +++ b/src/velofy/db/addresses_addresses.py @@ -33,7 +33,7 @@ class Path(meta.Base): Implements an association pattern between `Address` and `Address`. Further info: - https://docs.sqlalchemy.org/en/stable/orm/basic_relationships.html#association-object # noqa:E501 + https://docs.sqlalchemy.org/en/stable/orm/basic_relationships.html#association-object """ __tablename__ = "addresses_addresses" diff --git a/src/velofy/db/addresses_pixels.py b/src/velofy/db/addresses_pixels.py index 7736b90..d75377b 100644 --- a/src/velofy/db/addresses_pixels.py +++ b/src/velofy/db/addresses_pixels.py @@ -13,7 +13,7 @@ class AddressPixelAssociation(meta.Base): updates the `city_id` and `grid_id` columns. Further info: - https://docs.sqlalchemy.org/en/stable/orm/basic_relationships.html#association-object # noqa:E501 + https://docs.sqlalchemy.org/en/stable/orm/basic_relationships.html#association-object """ __tablename__ = "addresses_pixels" diff --git a/src/velofy/db/cities.py b/src/velofy/db/cities.py index b0fdaa5..f8b1e39 100644 --- a/src/velofy/db/cities.py +++ b/src/velofy/db/cities.py @@ -216,14 +216,14 @@ class City(meta.Base): row[0] for row in db.session.execute( sa.text( - f""" -- # noqa:S608 + f""" SELECT DISTINCT {config.CLEAN_SCHEMA}.addresses.zip_code FROM {config.CLEAN_SCHEMA}.addresses AS addresses WHERE {config.CLEAN_SCHEMA}.addresses.city_id = {self.id}; - """, + """, # noqa:S608 ), ) ) diff --git a/src/velofy/forecasts/timify.py b/src/velofy/forecasts/timify.py index 24b50e9..3b6cc1a 100644 --- a/src/velofy/forecasts/timify.py +++ b/src/velofy/forecasts/timify.py @@ -71,7 +71,7 @@ class OrderHistory: # This happens when there is no demand in the `Pixel` in the given `time_step`. data = pd.read_sql_query( sa.text( - f""" -- # noqa:S608 + f""" SELECT pixel_id, start_at, @@ -139,7 +139,7 @@ class OrderHistory: ORDER BY pixel_id, start_at; - """, + """, # noqa:S608 ), con=db.connection, index_col=["pixel_id", "start_at"], diff --git a/uv.lock b/uv.lock index ee0e488..1359a4b 100644 --- a/uv.lock +++ b/uv.lock @@ -3172,6 +3172,31 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/68/3d/51c9bbe79865a1eb0661b7f660e48fbf066d3940cb7773d906fd1d9dafc4/rpy2_robjects-3.6.6-py3-none-any.whl", hash = "sha256:ec377b1eb5a41964b5d3b3b164781c5dd2f602f3bb3d9409094c2b4de3608888", size = 125823, upload-time = "2026-10-10T17:42:37.273Z" }, ] +[[package]] +name = "ruff" +version = "0.17.6" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/df/8e/6929d337aa5380aafc7355f3149f3ccfe55cba5fdd34a045697ebaceb5a0/ruff-0.17.6.tar.gz", hash = "sha256:2b04276561331300325040bdca21460770029ec574d4c479099e39e8ad1ed43c", size = 5374388, upload-time = "2026-11-19T15:56:27.106Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/12/5d/38ef6310c22cacb094e9ae0ff7964062e04ffeecb45ee296894c2b1acc54/ruff-0.17.6-py3-none-linux_armv6l.whl", hash = "sha256:084d9c8f5ec718e7eba007dba339e7cb4f7571d2f267558ae080bf428fb6a69b", size = 10174693, upload-time = "2026-11-19T15:55:52.414Z" }, + { url = "https://files.pythonhosted.org/packages/a2/ed/fd35261049920a4888ef7c787a5c27089502077a4910f6ca8f1213157d6d/ruff-0.17.6-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:9d44ef2140402043fd6f689ef541858b80883a8b0a02e38539ef0614ebe57d0c", size = 10221650, upload-time = "2026-11-19T15:55:54.962Z" }, + { url = "https://files.pythonhosted.org/packages/ba/d4/c31391b36c729cd9fafa65b7a52a58850b99afb2b65da6be4263a53302f2/ruff-0.17.6-py3-none-macosx_11_0_arm64.whl", hash = "sha256:9fbddc08577592e283c265d865e0182de6e33db185494db7b06290d989d888e5", size = 9580986, upload-time = "2026-11-19T15:55:57.391Z" }, + { url = "https://files.pythonhosted.org/packages/eb/11/f3032684dcb3a676d7924545153d76fb56f76753e038f2ea650b99c71bdf/ruff-0.17.6-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:435241c61af5909a022f76219d3d1bf24730dc1bf0acabb5ddf4b569ab3cbb30", size = 10271368, upload-time = "2026-11-19T15:55:59.654Z" }, + { url = "https://files.pythonhosted.org/packages/08/30/c1e024eeb5bae99ae78c6b896ed108f64c9c845a03b56eaeff098e99b6e6/ruff-0.17.6-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:d006bbb2945edb17b0ce6a0c1b5f5a8d81ba11dbac294a0a925effa22ce34feb", size = 9981564, upload-time = "2026-11-19T15:56:01.807Z" }, + { url = "https://files.pythonhosted.org/packages/7d/a5/da3e0c2d4bf02b3ca6c1d7fa4bb04d0081e340b1dd03c4f83b1bec3e15c1/ruff-0.17.6-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:fd661702202a89c00a4c9d0339008805fa5876bfa1c57e165495c2e62230dfbf", size = 10641991, upload-time = "2026-11-19T15:56:03.981Z" }, + { url = "https://files.pythonhosted.org/packages/4d/c1/6d7f981e86201c9db457cca99fd2c09b77574ebab37dff3e9239b218603c/ruff-0.17.6-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:6a12c50a33936dc00db2be21ba25fb168a8b95c55eda2a3ac85005af69885e4b", size = 11403484, upload-time = "2026-11-19T15:56:05.778Z" }, + { url = "https://files.pythonhosted.org/packages/c3/92/8182f07b94998541d5f8f38f53da465aa28062afce24ecc398f45bfc62f1/ruff-0.17.6-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b5aba93b3db4b70c5697e4fee6e0d5b9c8452a9a3a03679611cadc25915a2173", size = 10892316, upload-time = "2026-11-19T15:56:08.016Z" }, + { url = "https://files.pythonhosted.org/packages/be/48/3df7d81249fd69ee6a49519b149fb5d11e13118fd7d0e8d65329a237087b/ruff-0.17.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:aa3b08254a732acba246891089a152c47c5733dc36b8ecb76ce8602f8326e5e4", size = 10645279, upload-time = "2026-11-19T15:56:09.785Z" }, + { url = "https://files.pythonhosted.org/packages/71/80/4d232281544345d7beb98d1d32c24dae804ed360780a6f7753738b81225e/ruff-0.17.6-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:0692088e6b33455572fb18465e891b443f196e98dc1986825685c77ff6f4c5ac", size = 10534501, upload-time = "2026-11-19T15:56:11.718Z" }, + { url = "https://files.pythonhosted.org/packages/98/0a/b477f075a4a94eb541bf1c759a3e72b2a3d6390af7a13a5be0887229f67c/ruff-0.17.6-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:fb589e032806bb942e4fb526f30b80a1a64112147ed9f1d38cc3de2e255b6f45", size = 10136036, upload-time = "2026-11-19T15:56:13.779Z" }, + { url = "https://files.pythonhosted.org/packages/f1/d8/cf32fbfeb1fcda1470fb5c3bee50c69ee6bca257d487ec1eb17a0387edb4/ruff-0.17.6-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:1939b3d701c7b6981b7cf93473de5bf88b129b9ba84efaed5154cc517c44634d", size = 9969640, upload-time = "2026-11-19T15:56:15.738Z" }, + { url = "https://files.pythonhosted.org/packages/a7/22/bb9f9752f7fcdfdc8b9c51576cc142bb377ade745290e35085173963c7be/ruff-0.17.6-py3-none-musllinux_1_2_i686.whl", hash = "sha256:1a0bea6aa1216e0ec4cd35d3dbccec0d772a7c2aad28ec9c6e82571dff2c20b0", size = 10389427, upload-time = "2026-11-19T15:56:17.617Z" }, + { url = "https://files.pythonhosted.org/packages/81/c7/e09d220818ab146b736d62e0676dad215215d7de0884bbdad9fa075dc7d2/ruff-0.17.6-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:3db61f5015600543a581b07fa7f6f64afa0eeedefe16034cadd88c4074087582", size = 10760296, upload-time = "2026-11-19T15:56:19.463Z" }, + { url = "https://files.pythonhosted.org/packages/2e/09/28329441dfa2c463e2d377a69d4ebbe4d51665e9dbd49fb421f3bc80d775/ruff-0.17.6-py3-none-win32.whl", hash = "sha256:c2f4af6670b7a0501904e881bb3f1f24bd0d964dec1eedbd85f0eb73786e1128", size = 9966266, upload-time = "2026-11-19T15:56:21.581Z" }, + { url = "https://files.pythonhosted.org/packages/4b/9d/18ba9730addfc7554c70d6f171fb8dc2c08e774d9fb2b98e7946ef845c59/ruff-0.17.6-py3-none-win_amd64.whl", hash = "sha256:9cd2fd9923e9806b8b6b67a3469e2258e84bd4d21e45d5a1a690afdfb9c8e57d", size = 10365847, upload-time = "2026-11-19T15:56:23.582Z" }, + { url = "https://files.pythonhosted.org/packages/70/8a/59c33e2a64a4f9672f7f842a0dc522915cfbbaffd13d37d0cec57d1c5c5d/ruff-0.17.6-py3-none-win_arm64.whl", hash = "sha256:d8b14e1473dc86ab9069cf7a2ba5041dc012f59110201844ed04cfddf07055a0", size = 9926224, upload-time = "2026-11-19T15:56:25.269Z" }, +] + [[package]] name = "scipy" version = "1.18.1" @@ -3765,6 +3790,7 @@ dev = [ { name = "pep8-naming" }, { name = "pre-commit" }, { name = "pydoclint", extra = ["flake8"] }, + { name = "ruff" }, ] docs = [ { name = "sphinx" }, @@ -3829,6 +3855,7 @@ dev = [ { name = "pep8-naming", specifier = ">=0.15.1,<0.16.0" }, { name = "pre-commit", specifier = ">=4.7.0,<5.0.0" }, { name = "pydoclint", extras = ["flake8"], specifier = ">=0.12.0,<0.13.0" }, + { name = "ruff", specifier = ">=0.17.6,<0.18.0" }, ] docs = [ { name = "sphinx", specifier = ">=9.2.0,<10.0.0" },