Unify the way the imports work #9

Merged
alexander merged 21 commits from unify-imports into develop 2026-11-30 20:30:54 +01:00
7 changed files with 26 additions and 28 deletions
Showing only changes of commit 9c3b52befb - Show all commits

Import typing as t ...

... and `collections.abc` as `cabc`

Linting rule for `velofy`: Import modules instead of objects
Alexander Hess 2026-11-30 15:10:29 +01:00
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -23,20 +23,19 @@ e.g., `uv run nox -s test -- tests/test_config.py`.
"""
import collections
import collections.abc as cabc
import itertools
import os
import pathlib
import tempfile
from collections.abc import Generator
from collections.abc import Mapping
from typing import Any
import typing as t
import nox
from nox.sessions import Session
from packaging import version as pkg_version
def nested_defaultdict() -> collections.defaultdict[str, Any]:
def nested_defaultdict() -> collections.defaultdict[str, t.Any]:
"""Create a multi-level `defaultdict` with variable depth.
The returned `dict`ionary never raises a `KeyError`
@ -48,9 +47,9 @@ def nested_defaultdict() -> collections.defaultdict[str, Any]:
return collections.defaultdict(nested_defaultdict)
def defaultify(obj: Any) -> Any:
def defaultify(obj: t.Any) -> t.Any:
"""Turn nested `dict`s into nested `defaultdict`s."""
if isinstance(obj, Mapping):
if isinstance(obj, cabc.Mapping):
return collections.defaultdict(
nested_defaultdict,
{key: defaultify(val) for key, val in obj.items()},
@ -58,7 +57,7 @@ def defaultify(obj: Any) -> Any:
return obj
def load_pyproject_toml() -> collections.defaultdict[str, Any]:
def load_pyproject_toml() -> collections.defaultdict[str, t.Any]:
"""Load the contents of the pyproject.toml file.
The contents are represented as a `nested_defaultdict`;
@ -522,7 +521,7 @@ def clean_cwd(session: Session) -> None:
session.run("rm", "-rf", path)
def _expand(*patterns: str) -> Generator[pathlib.Path, None, None]:
def _expand(*patterns: str) -> cabc.Generator[pathlib.Path, None, None]:
"""Expand glob patterns into (resolved) paths.
Args:
@ -600,7 +599,7 @@ def start(session: Session) -> None:
def install_pinned(
session: Session,
*packages_or_pip_args: str,
**kwargs: Any,
**kwargs: t.Any,
) -> None:
"""Install packages respecting the uv.lock file.

View file

@ -1,25 +1,24 @@
"""Utils for the CLI scripts."""
import collections.abc as cabc
import functools
import os
import subprocess # noqa:S404
import sys
from collections.abc import Callable
from typing import Any
import typing as t
import click
def db_revision(
rev: str,
) -> Callable[..., Callable[..., Any]]: # pragma: no cover -> easy to check visually
def db_revision(rev: str) -> cabc.Callable[..., cabc.Callable[..., t.Any]]:
"""A decorator ensuring the database is at a given revision."""
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
def decorator(func: cabc.Callable[..., t.Any]) -> cabc.Callable[..., t.Any]:
@functools.wraps(func)
def ensure(*args: Any, **kwargs: Any) -> Any:
def ensure(*args: t.Any, **kwargs: t.Any) -> t.Any:
"""Do not execute the `func` if the revision does not match."""
if not os.getenv("TESTING"):
# Skip in tests as the database then either has no or the latest revision
if not os.getenv("TESTING"): # pragma: no cover
result = subprocess.run( # noqa:S603,S607
["alembic", "current"], # noqa:S607
capture_output=True,

View file

@ -1,6 +1,6 @@
"""The entry point for all CLI scripts in the project."""
from typing import Any
import typing as t
import click
from click import core as cli_core
@ -8,7 +8,7 @@ from click import core as cli_core
import velofy
def show_version(ctx: cli_core.Context, _param: Any, value: bool) -> None:
def show_version(ctx: cli_core.Context, _param: t.Any, value: bool) -> None:
"""Show the package's version."""
# If --version / -V is NOT passed in,
# continue with the command.

View file

@ -4,7 +4,7 @@ from __future__ import annotations
import datetime as dt
import functools
from typing import Any
import typing as t
import folium
import sqlalchemy as sa
@ -185,7 +185,7 @@ class Address(meta.Base):
"""Shortcut to the `.city.map` object."""
return self.city.map
def draw(self, **kwargs: Any) -> folium.Map: # pragma: no cover
def draw(self, **kwargs: t.Any) -> folium.Map: # pragma: no cover
"""Draw the address on the `.city.map`.
By default, addresses are shown as black dots.
@ -197,7 +197,7 @@ class Address(meta.Base):
Returns:
`.city.map` for convenience in interactive usage
"""
defaults: dict[str, Any] = {
defaults: dict[str, t.Any] = {
"color": f"{config.NEUTRAL_COLOR}",
"popup": f"{self.street}, {self.zip_code} {self.city_name}",
}

View file

@ -8,7 +8,7 @@ That is the case on the CI server.
"""
import os
from typing import cast
import typing as t
import sqlalchemy as sa
from sqlalchemy import orm
@ -21,5 +21,5 @@ if os.getenv("TESTING"):
session: orm.Session = None # type: ignore[assignment]
else: # pragma: no cover
engine = sa.create_engine(cast(str, velofy.config.DATABASE_URI))
engine = sa.create_engine(t.cast(str, velofy.config.DATABASE_URI))
session = orm.Session(bind=engine)

View file

@ -2,7 +2,7 @@
from __future__ import annotations
from typing import Any
import typing as t
import folium
import sqlalchemy as sa
@ -136,7 +136,7 @@ class Grid(meta.Base):
"""Shortcut to the `.city.map` object."""
return self.city.map
def draw(self, **kwargs: Any) -> folium.Map: # pragma: no cover
def draw(self, **kwargs: t.Any) -> folium.Map: # pragma: no cover
"""Draw all pixels in the grid.
Args:

View file

@ -1,6 +1,6 @@
"""Provide the ORM's declarative base."""
from typing import Any
import typing as t
import sqlalchemy as sa
from sqlalchemy import orm
@ -8,7 +8,7 @@ from sqlalchemy import orm
import velofy
Base: Any = orm.declarative_base(
Base: t.Any = orm.declarative_base(
metadata=sa.MetaData(
schema=velofy.config.CLEAN_SCHEMA,
naming_convention={