Type-check noxfile.py

This commit is contained in:
Alexander Hess 2026-12-01 13:54:23 +01:00
commit f7a6aa1881
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -63,7 +63,10 @@ def load_pyproject_toml() -> collections.defaultdict[str, t.Any]:
so, missing keys and tables (i.e., "sections" in the .ini format) so, missing keys and tables (i.e., "sections" in the .ini format)
do not result in `KeyError`s but return empty `nested_defaultdict`s. do not result in `KeyError`s but return empty `nested_defaultdict`s.
""" """
return defaultify(nox.project.load_toml("pyproject.toml")) return t.cast(
collections.defaultdict[str, t.Any],
defaultify(nox.project.load_toml("pyproject.toml")),
)
def load_supported_python_versions(*, reverse: bool = False) -> list[str]: def load_supported_python_versions(*, reverse: bool = False) -> list[str]:
@ -77,6 +80,9 @@ def load_supported_python_versions(*, reverse: bool = False) -> list[str]:
return sorted(version_names, key=pkg_version.Version, reverse=reverse) return sorted(version_names, key=pkg_version.Version, reverse=reverse)
# The "./" keeps `bandit` from warning about its module name
NOXFILE_LOCATION = "./noxfile.py"
PACKAGE = "velofy" PACKAGE = "velofy"
PACKAGE_LOCATION = "src/" PACKAGE_LOCATION = "src/"
@ -90,7 +96,7 @@ SRC_LOCATIONS = (
f"{DOCS_LOCATION}conf.py", f"{DOCS_LOCATION}conf.py",
"migrations/env.py", "migrations/env.py",
"migrations/versions/", "migrations/versions/",
"./noxfile.py", # The "./" keeps `bandit` from warning about its module name NOXFILE_LOCATION,
"scripts/", "scripts/",
PACKAGE_LOCATION, PACKAGE_LOCATION,
TESTS_LOCATION, TESTS_LOCATION,
@ -180,7 +186,7 @@ def lint_code(session: nox.Session) -> None:
If no extra arguments are provided, all source files are linted. If no extra arguments are provided, all source files are linted.
Otherwise, they are interpreted as paths the linters work on recursively. Otherwise, they are interpreted as paths the linters work on recursively.
`mypy` always checks the entire package regardless `mypy` always checks the noxfile and the entire package regardless
because it needs the whole program to follow types across modules. because it needs the whole program to follow types across modules.
""" """
start(session) start(session)
@ -220,6 +226,7 @@ def lint_code(session: nox.Session) -> None:
"flake8-unused-arguments", "flake8-unused-arguments",
"isort", "isort",
"mypy", "mypy",
"nox", # Because we type-check the noxfile.py as well
"pandas-stubs", "pandas-stubs",
"pandas-vet", "pandas-vet",
"pep8-naming", "pep8-naming",
@ -243,7 +250,7 @@ def lint_code(session: nox.Session) -> None:
session.run("ruff", "check", *locations) session.run("ruff", "check", *locations)
session.run("mypy", "--version") session.run("mypy", "--version")
session.run("mypy", PACKAGE_LOCATION) session.run("mypy", NOXFILE_LOCATION, PACKAGE_LOCATION)
@nox.session(name="test", python=MAIN_PYTHON) @nox.session(name="test", python=MAIN_PYTHON)
@ -567,7 +574,7 @@ def do_not_accept_arguments(session: nox.Session, *, raise_error: bool = True) -
def do_not_reuse(session: nox.Session, *, raise_error: bool = True) -> None: def do_not_reuse(session: nox.Session, *, raise_error: bool = True) -> None:
"""Do not reuse a session with the "-r" or "-R" flags.""" """Do not reuse a session with the "-r" or "-R" flags."""
if session._runner.venv._reused: # noqa:SLF001 if session.virtualenv._reused: # noqa:SLF001
msg = 'be run without the "-r" and "-R" flags' msg = 'be run without the "-r" and "-R" flags'
if raise_error: if raise_error:
@ -656,7 +663,7 @@ def skip_install(session: nox.Session) -> bool:
# Same logic to skip package installation as in core `nox` # Same logic to skip package installation as in core `nox`
# See: https://github.com/wntrblm/nox/blob/2026.10.20/nox/sessions.py#L905 # See: https://github.com/wntrblm/nox/blob/2026.10.20/nox/sessions.py#L905
no_install = session._runner.global_config.no_install # noqa:SLF001 no_install = session._runner.global_config.no_install # noqa:SLF001
venv_reused = session._runner.venv._reused # noqa:SLF001 venv_reused = session.virtualenv._reused # noqa:SLF001
return bool(no_install and venv_reused) return bool(no_install and venv_reused)