Import typing as t ...
... and `collections.abc` as `cabc` Linting rule for `velofy`: Import modules instead of objects
This commit is contained in:
parent
c6518f6b39
commit
9c3b52befb
7 changed files with 26 additions and 28 deletions
17
noxfile.py
17
noxfile.py
|
|
@ -23,20 +23,19 @@ e.g., `uv run nox -s test -- tests/test_config.py`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
|
import collections.abc as cabc
|
||||||
import itertools
|
import itertools
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import tempfile
|
import tempfile
|
||||||
from collections.abc import Generator
|
import typing as t
|
||||||
from collections.abc import Mapping
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import nox
|
import nox
|
||||||
from nox.sessions import Session
|
from nox.sessions import Session
|
||||||
from packaging import version as pkg_version
|
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.
|
"""Create a multi-level `defaultdict` with variable depth.
|
||||||
|
|
||||||
The returned `dict`ionary never raises a `KeyError`
|
The returned `dict`ionary never raises a `KeyError`
|
||||||
|
|
@ -48,9 +47,9 @@ def nested_defaultdict() -> collections.defaultdict[str, Any]:
|
||||||
return collections.defaultdict(nested_defaultdict)
|
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."""
|
"""Turn nested `dict`s into nested `defaultdict`s."""
|
||||||
if isinstance(obj, Mapping):
|
if isinstance(obj, cabc.Mapping):
|
||||||
return collections.defaultdict(
|
return collections.defaultdict(
|
||||||
nested_defaultdict,
|
nested_defaultdict,
|
||||||
{key: defaultify(val) for key, val in obj.items()},
|
{key: defaultify(val) for key, val in obj.items()},
|
||||||
|
|
@ -58,7 +57,7 @@ def defaultify(obj: Any) -> Any:
|
||||||
return obj
|
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.
|
"""Load the contents of the pyproject.toml file.
|
||||||
|
|
||||||
The contents are represented as a `nested_defaultdict`;
|
The contents are represented as a `nested_defaultdict`;
|
||||||
|
|
@ -522,7 +521,7 @@ def clean_cwd(session: Session) -> None:
|
||||||
session.run("rm", "-rf", path)
|
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.
|
"""Expand glob patterns into (resolved) paths.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -600,7 +599,7 @@ def start(session: Session) -> None:
|
||||||
def install_pinned(
|
def install_pinned(
|
||||||
session: Session,
|
session: Session,
|
||||||
*packages_or_pip_args: str,
|
*packages_or_pip_args: str,
|
||||||
**kwargs: Any,
|
**kwargs: t.Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Install packages respecting the uv.lock file.
|
"""Install packages respecting the uv.lock file.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,24 @@
|
||||||
"""Utils for the CLI scripts."""
|
"""Utils for the CLI scripts."""
|
||||||
|
|
||||||
|
import collections.abc as cabc
|
||||||
import functools
|
import functools
|
||||||
import os
|
import os
|
||||||
import subprocess # noqa:S404
|
import subprocess # noqa:S404
|
||||||
import sys
|
import sys
|
||||||
from collections.abc import Callable
|
import typing as t
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
|
|
||||||
def db_revision(
|
def db_revision(rev: str) -> cabc.Callable[..., cabc.Callable[..., t.Any]]:
|
||||||
rev: str,
|
|
||||||
) -> Callable[..., Callable[..., Any]]: # pragma: no cover -> easy to check visually
|
|
||||||
"""A decorator ensuring the database is at a given revision."""
|
"""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)
|
@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."""
|
"""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
|
result = subprocess.run( # noqa:S603,S607
|
||||||
["alembic", "current"], # noqa:S607
|
["alembic", "current"], # noqa:S607
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
"""The entry point for all CLI scripts in the project."""
|
"""The entry point for all CLI scripts in the project."""
|
||||||
|
|
||||||
from typing import Any
|
import typing as t
|
||||||
|
|
||||||
import click
|
import click
|
||||||
from click import core as cli_core
|
from click import core as cli_core
|
||||||
|
|
@ -8,7 +8,7 @@ from click import core as cli_core
|
||||||
import velofy
|
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."""
|
"""Show the package's version."""
|
||||||
# If --version / -V is NOT passed in,
|
# If --version / -V is NOT passed in,
|
||||||
# continue with the command.
|
# continue with the command.
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
import functools
|
import functools
|
||||||
from typing import Any
|
import typing as t
|
||||||
|
|
||||||
import folium
|
import folium
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
|
@ -185,7 +185,7 @@ class Address(meta.Base):
|
||||||
"""Shortcut to the `.city.map` object."""
|
"""Shortcut to the `.city.map` object."""
|
||||||
return self.city.map
|
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`.
|
"""Draw the address on the `.city.map`.
|
||||||
|
|
||||||
By default, addresses are shown as black dots.
|
By default, addresses are shown as black dots.
|
||||||
|
|
@ -197,7 +197,7 @@ class Address(meta.Base):
|
||||||
Returns:
|
Returns:
|
||||||
`.city.map` for convenience in interactive usage
|
`.city.map` for convenience in interactive usage
|
||||||
"""
|
"""
|
||||||
defaults: dict[str, Any] = {
|
defaults: dict[str, t.Any] = {
|
||||||
"color": f"{config.NEUTRAL_COLOR}",
|
"color": f"{config.NEUTRAL_COLOR}",
|
||||||
"popup": f"{self.street}, {self.zip_code} {self.city_name}",
|
"popup": f"{self.street}, {self.zip_code} {self.city_name}",
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ That is the case on the CI server.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from typing import cast
|
import typing as t
|
||||||
|
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import orm
|
from sqlalchemy import orm
|
||||||
|
|
@ -21,5 +21,5 @@ if os.getenv("TESTING"):
|
||||||
session: orm.Session = None # type: ignore[assignment]
|
session: orm.Session = None # type: ignore[assignment]
|
||||||
|
|
||||||
else: # pragma: no cover
|
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)
|
session = orm.Session(bind=engine)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any
|
import typing as t
|
||||||
|
|
||||||
import folium
|
import folium
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
|
|
@ -136,7 +136,7 @@ class Grid(meta.Base):
|
||||||
"""Shortcut to the `.city.map` object."""
|
"""Shortcut to the `.city.map` object."""
|
||||||
return self.city.map
|
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.
|
"""Draw all pixels in the grid.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
"""Provide the ORM's declarative base."""
|
"""Provide the ORM's declarative base."""
|
||||||
|
|
||||||
from typing import Any
|
import typing as t
|
||||||
|
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import orm
|
from sqlalchemy import orm
|
||||||
|
|
@ -8,7 +8,7 @@ from sqlalchemy import orm
|
||||||
import velofy
|
import velofy
|
||||||
|
|
||||||
|
|
||||||
Base: Any = orm.declarative_base(
|
Base: t.Any = orm.declarative_base(
|
||||||
metadata=sa.MetaData(
|
metadata=sa.MetaData(
|
||||||
schema=velofy.config.CLEAN_SCHEMA,
|
schema=velofy.config.CLEAN_SCHEMA,
|
||||||
naming_convention={
|
naming_convention={
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue