Streamline the names, docstrings, and comments

This commit is contained in:
Alexander Hess 2026-11-27 12:50:07 +01:00
commit 7c2e21130f
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -78,38 +78,36 @@ 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)
PACKAGE_IMPORT_NAME = 'velofy' PACKAGE = 'velofy'
PACKAGE_LOCATION = 'src/'
# Docs/sphinx locations.
DOCS_SRC = 'docs/'
DOCS_BUILD = '.cache/docs/' DOCS_BUILD = '.cache/docs/'
DOCS_LOCATION = 'docs/'
# Path to the *.py files to be packaged. TESTS_LOCATION = 'tests/'
PACKAGE_SOURCE_LOCATION = 'src/'
# Path to the test suite. # Locations of all *.py files
PYTEST_LOCATION = 'tests/'
# Paths with all *.py files.
SRC_LOCATIONS = ( SRC_LOCATIONS = (
f'{DOCS_SRC}conf.py', f'{DOCS_LOCATION}conf.py',
'migrations/env.py', 'migrations/env.py',
'migrations/versions/', 'migrations/versions/',
'noxfile.py', 'noxfile.py',
'scripts/', 'scripts/',
PACKAGE_SOURCE_LOCATION, PACKAGE_LOCATION,
PYTEST_LOCATION, TESTS_LOCATION,
) )
SUPPORTED_PYTHONS = load_supported_python_versions() SUPPORTED_PYTHONS = load_supported_python_versions()
# Must be listed in the "classifiers", which is checked at the bottom # Must be listed in the "classifiers", which is checked at the bottom
# of this file, and satisfy `requires-python` in pyproject.toml # of this file, and satisfy "requires-python" in pyproject.toml
MAIN_PYTHON = '3.13' MAIN_PYTHON = '3.13'
nox.options.default_venv_backend = 'uv' nox.options.default_venv_backend = 'uv'
# Use a unified .cache/ folder for all tools. # Use a unified .cache/ folder for all tools
nox.options.envdir = '.cache/nox' nox.options.envdir = '.cache/nox'
# All tools except `git` and `uv` are project dependencies # All tools except `git` and `uv` are project dependencies
@ -123,7 +121,7 @@ nox.options.reuse_venv = 'no'
# Do not run further sessions once one fails # Do not run further sessions once one fails
nox.options.stop_on_first_error = True nox.options.stop_on_first_error = True
# Run only local checks by default. # Run only the code quality checks by default
nox.options.sessions = ( nox.options.sessions = (
'format', 'format',
'lint', 'lint',
@ -131,7 +129,8 @@ nox.options.sessions = (
'doctest', 'doctest',
) )
# Make `uv` use its own Python no matter what
# Make `uv` use its own `python`
# because only then are the headers needed to build `rpy2` from source included # because only then are the headers needed to build `rpy2` from source included
os.environ.setdefault('UV_PYTHON_PREFERENCE', 'only-managed') os.environ.setdefault('UV_PYTHON_PREFERENCE', 'only-managed')
@ -142,21 +141,21 @@ os.makedirs(tempfile.tempdir, exist_ok=True)
@nox.session(name='format', python=MAIN_PYTHON) @nox.session(name='format', python=MAIN_PYTHON)
def format_code(session: Session) -> None: def format_code(session: Session) -> None:
"""Format source files with autoflake, black, and isort. """Format source files with `autoflake`, `black`, and `isort`.
If no extra arguments are provided, all source files are formatted. If no extra arguments are provided, all source files are formatted.
Otherwise, they are interpreted as paths the formatters work on recursively. Otherwise, they are interpreted as paths the formatters work on recursively.
""" """
start(session) start(session)
# The formatting tools do not require the developed # The formatters do not need `velofy` installed
# package be installed in the virtual environment. # because they work on the source files directly
install_pinned(session, 'autoflake', 'black', 'isort') install_pinned(session, 'autoflake', 'black', 'isort')
# Interpret extra arguments as locations of source files. # Interpret extra arguments as `locations` of source files
locations = session.posargs or SRC_LOCATIONS locations = session.posargs or SRC_LOCATIONS
# `autoflake` only changes files because of "in-place = true" # `autoflake` changes files only with the "in-place = true"
# in the "[tool.autoflake]" section in pyproject.toml # in the "[tool.autoflake]" section in pyproject.toml
session.run('autoflake', '--version') session.run('autoflake', '--version')
session.run('autoflake', *locations) session.run('autoflake', *locations)
@ -170,15 +169,15 @@ def format_code(session: Session) -> None:
@nox.session(name='lint', python=MAIN_PYTHON) @nox.session(name='lint', python=MAIN_PYTHON)
def lint_code(session: Session) -> None: def lint_code(session: Session) -> None:
"""Lint source files with flake8 and mypy. """Lint source files with `flake8` and `isort`.
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.
""" """
start(session) start(session)
# The linting tools do not require the developed # The linters do not need `velofy` installed
# package be installed in the virtual environment. # because they work on the source files directly
install_pinned( install_pinned(
session, session,
'flake8', 'flake8',
@ -191,7 +190,7 @@ def lint_code(session: Session) -> None:
# 'mypy', # 'mypy',
) )
# Interpret extra arguments as locations of source files. # Interpret extra arguments as `locations` of source files
locations = session.posargs or SRC_LOCATIONS locations = session.posargs or SRC_LOCATIONS
# `flake8` reads the "[tool.flake8]" section in pyproject.toml # `flake8` reads the "[tool.flake8]" section in pyproject.toml
@ -207,7 +206,7 @@ def lint_code(session: Session) -> None:
# - It then needs the developed package and its dependencies installed # - It then needs the developed package and its dependencies installed
# as it treats missing packages as `Any` and reports false successes # as it treats missing packages as `Any` and reports false successes
# session.run('mypy', '--version') # session.run('mypy', '--version')
# session.run('mypy', PACKAGE_SOURCE_LOCATION) # session.run('mypy', PACKAGE_LOCATION)
@nox.session(name='test', python=MAIN_PYTHON) @nox.session(name='test', python=MAIN_PYTHON)
@ -242,7 +241,7 @@ def test_code(session: Session) -> None:
'--cov', '--cov',
'--no-cov-on-fail', '--no-cov-on-fail',
'--cov-fail-under=100', '--cov-fail-under=100',
PYTEST_LOCATION, TESTS_LOCATION,
) )
session.run('pytest', *(session.posargs or pytest_default_args)) session.run('pytest', *(session.posargs or pytest_default_args))
@ -268,7 +267,7 @@ def test_docstrings(session: Session) -> None:
'python', 'python',
'-m', '-m',
'xdoctest', 'xdoctest',
PACKAGE_IMPORT_NAME, PACKAGE,
*(session.posargs or ('all',)), *(session.posargs or ('all',)),
) )
@ -319,22 +318,24 @@ def audit_pinned_dependencies(session: Session) -> None:
@nox.session(name='docs', python=MAIN_PYTHON) @nox.session(name='docs', python=MAIN_PYTHON)
def build_docs(session: Session) -> None: def build_docs(session: Session) -> None:
"""Build the documentation with sphinx.""" """Build the documentation with `sphinx`.
Also, verify that all external links work.
"""
start(session) start(session)
# The documentation tools require the developed package and its # The `sphinx-autodoc` extension imports `velofy` to read the docstrings
# non-develop dependencies be installed in the virtual environment. # => `velofy` and its dependencies must be installed
# Otherwise, sphinx's autodoc could not include the docstrings.
install_pinned(session, '.', 'sphinx', 'sphinx-autodoc-typehints') install_pinned(session, '.', 'sphinx', 'sphinx-autodoc-typehints')
# The "TESTING" environment variable forces the global `engine`, `connection`, # As `pytest-env` does for `pytest`,
# and `session` objects to be set to `None` and avoid any database connection. # keep `velofy` from connecting to the production database
session.env['TESTING'] = 'true' session.env['TESTING'] = 'true'
session.run('sphinx-build', DOCS_SRC, DOCS_BUILD) session.run('sphinx-build', DOCS_LOCATION, DOCS_BUILD)
# Verify all external links return 200 OK.
session.run('sphinx-build', '-b', 'linkcheck', DOCS_SRC, DOCS_BUILD) # 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') # noqa:WPS421 print(f'Docs are available at {os.getcwd()}/{DOCS_BUILD}index.html') # noqa:WPS421
@ -403,6 +404,7 @@ def check_dependency_upgrades(session: Session) -> None:
The output is written to ".cache/dependencies_checked.txt". The output is written to ".cache/dependencies_checked.txt".
""" """
do_not_accept_arguments(session) do_not_accept_arguments(session)
start(session) start(session)
# The script only uses the standard library => Nothing to install # The script only uses the standard library => Nothing to install
@ -460,7 +462,7 @@ def _expand(*patterns: str) -> Generator[pathlib.Path, None, None]:
@nox.session(name='init-project', python=MAIN_PYTHON, venv_backend='none') @nox.session(name='init-project', python=MAIN_PYTHON, venv_backend='none')
def initialize_project(session: Session) -> None: def initialize_project(session: Session) -> None:
"""Install the pre-commit hooks.""" """Install the `pre-commit` hooks."""
for type_ in ('pre-commit', 'pre-merge-commit', 'pre-push'): for type_ in ('pre-commit', 'pre-merge-commit', 'pre-push'):
session.run('uv', 'run', 'pre-commit', 'install', f'--hook-type={type_}') session.run('uv', 'run', 'pre-commit', 'install', f'--hook-type={type_}')
@ -510,6 +512,7 @@ def do_not_accept_arguments(session: Session, *, raise_error: bool = True) -> No
"""Do not accept any `session.posargs`.""" """Do not accept any `session.posargs`."""
if session.posargs: if session.posargs:
msg = 'be run without any positional arguments behind the "--"' msg = 'be run without any positional arguments behind the "--"'
if raise_error: if raise_error:
session.error(f'Must {msg}') session.error(f'Must {msg}')
else: else:
@ -517,9 +520,10 @@ def do_not_accept_arguments(session: Session, *, raise_error: bool = True) -> No
def do_not_reuse(session: Session, *, raise_error: bool = True) -> None: def do_not_reuse(session: Session, *, raise_error: bool = True) -> None:
"""Do not reuse a session with the "-r" flag.""" """Do not reuse a session with the "-r" or "-R" flags."""
if session._runner.venv._reused: if session._runner.venv._reused:
msg = 'be run without the "-r" and "-R" flags' msg = 'be run without the "-r" and "-R" flags'
if raise_error: if raise_error:
session.error(f'Must {msg}') session.error(f'Must {msg}')
else: else:
@ -532,6 +536,7 @@ def start(session: Session) -> None:
session.debug(f'Received extra arguments: {session.posargs}') session.debug(f'Received extra arguments: {session.posargs}')
session.debug('Some generic information about the environment') session.debug('Some generic information about the environment')
session.run('python', '--version') session.run('python', '--version')
session.run('python', '-c', 'import sys; print(sys.executable)') session.run('python', '-c', 'import sys; print(sys.executable)')
session.run('python', '-c', 'import sys; print(sys.path)') session.run('python', '-c', 'import sys; print(sys.path)')
@ -569,7 +574,7 @@ def install_pinned(
Wraps `nox.Session.install()` such that it installs packages Wraps `nox.Session.install()` such that it installs packages
respecting the versions pinned in the uv.lock file, respecting the versions pinned in the uv.lock file,
exported into a temporary constraints file. exported into a temporary constraints file.
This makes nox sessions more deterministic. This makes `nox` sessions more deterministic.
The developed package itself (i.e., ".") is only rebuilt The developed package itself (i.e., ".") is only rebuilt
if a file listed in the "cache-keys" in the "[tool.uv]" section if a file listed in the "cache-keys" in the "[tool.uv]" section
@ -603,7 +608,7 @@ def skip_install(session: Session) -> bool:
Skipping package installations must be requested with the "-R" flag. Skipping package installations must be requested with the "-R" flag.
""" """
# 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/2024.10.09/nox/sessions.py#L816 # See: https://github.com/wntrblm/nox/blob/2026.10.20/nox/sessions.py#L905
venv = session._runner.venv venv = session._runner.venv
if session._runner.global_config.no_install and venv._reused: if session._runner.global_config.no_install and venv._reused:
return True return True