From 11d2b0250672187ae8a7d3a86aa0dc417d2aaaab Mon Sep 17 00:00:00 2001 From: Callum Forrester Date: Mon, 11 Aug 2025 14:57:47 +0000 Subject: [PATCH] Enable pep8-naming ruff rules Fixes #282 Enable pep8-naming ruff rules, enforcing that projects follow convetional pep8 naming styles. See https://peps.python.org/pep-0008 --- pyproject.toml | 7 +++++++ template/pyproject.toml.jinja | 1 + tests/test_example.py | 15 +++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index b0ab7390..47c023b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -62,7 +62,14 @@ lint.select = [ "C4", # flake8-comprehensions - https://docs.astral.sh/ruff/rules/#flake8-comprehensions-c4 "E", # pycodestyle errors - https://docs.astral.sh/ruff/rules/#error-e "F", # pyflakes rules - https://docs.astral.sh/ruff/rules/#pyflakes-f + "N", # pep8-naming - https://docs.astral.sh/ruff/rules/#pep8-naming-n "W", # pycodestyle warnings - https://docs.astral.sh/ruff/rules/#warning-w "I", # isort - https://docs.astral.sh/ruff/rules/#isort-i "UP", # pyupgrade - https://docs.astral.sh/ruff/rules/#pyupgrade-up ] + +[tool.ruff.lint.per-file-ignores] +# Ignore variable names inside the template because ruff doesn't +# work well with jinja templating. Any violations of N rules inside +# the template are caught by the tests. +"template/**/*" = ["N"] diff --git a/template/pyproject.toml.jinja b/template/pyproject.toml.jinja index 2155ca29..8623ebb3 100644 --- a/template/pyproject.toml.jinja +++ b/template/pyproject.toml.jinja @@ -109,6 +109,7 @@ lint.select = [ "C4", # flake8-comprehensions - https://docs.astral.sh/ruff/rules/#flake8-comprehensions-c4 "E", # pycodestyle errors - https://docs.astral.sh/ruff/rules/#error-e "F", # pyflakes rules - https://docs.astral.sh/ruff/rules/#pyflakes-f + "N", # pep8-naming - https://docs.astral.sh/ruff/rules/#pep8-naming-n "W", # pycodestyle warnings - https://docs.astral.sh/ruff/rules/#warning-w "I", # isort - https://docs.astral.sh/ruff/rules/#isort-i "UP", # pyupgrade - https://docs.astral.sh/ruff/rules/#pyupgrade-up diff --git a/tests/test_example.py b/tests/test_example.py index cd672391..2608b0e7 100644 --- a/tests/test_example.py +++ b/tests/test_example.py @@ -223,6 +223,21 @@ def __init__(self): run("ruff check") +def test_pep8_naming(tmp_path: Path): + code = """ +myVariable = "foo" +""" + + copy_project(tmp_path) + run = make_venv(tmp_path) + + src_file = tmp_path / "src" / "python_copier_template_example" / "bad_example.py" + with src_file.open("w") as stream: + stream.write(code) + with pytest.raises(AssertionError, match=r"N816 .*"): + run("ruff check") + + def test_pyright_works_in_standard_typing_mode(tmp_path: Path): copy_project(tmp_path, type_checker="pyright", strict_typing=False) pyproject_toml = tmp_path / "pyproject.toml"