Skip to content

Commit

Permalink
chore: Enable TypeAlias lint
Browse files Browse the repository at this point in the history
  • Loading branch information
rumpelsepp committed Dec 18, 2024
1 parent 5d1d222 commit 0c48eda
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 31 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ ignore = [
"TD001", # Invalid TODO tag: {tag}
"TD002", # Missing author in TODO; try: # TODO(<author_name>): ... or # TODO @<author_name>: ...
"TD003", # Missing issue link on the line following this TODO
"UP040", # Type alias uses `TypeAlias` annotation instead of the `type` keyword
]

[tool.pytest.ini_options]
Expand Down
7 changes: 3 additions & 4 deletions src/gallia/command/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
TYPE_CHECKING,
Annotated,
Any,
TypeAlias,
TypeVar,
Unpack,
get_args,
Expand Down Expand Up @@ -132,9 +131,9 @@ def auto_enum(x: str, enum_type: type[EnumType]) -> EnumType:


if TYPE_CHECKING:
Idempotent: TypeAlias = Annotated[T, ""]
EnumArg: TypeAlias = Annotated[EnumType, ""]
AutoLiteral: TypeAlias = Annotated[LiteralType, ""]
type Idempotent[T] = Annotated[T, ""]
type EnumArg[EnumType: Enum] = Annotated[EnumType, ""]
type AutoLiteral[LiteralType] = Annotated[LiteralType, ""]
else:

class _TrickType:
Expand Down
4 changes: 2 additions & 2 deletions src/gallia/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from pathlib import Path
from queue import Queue
from types import TracebackType
from typing import TYPE_CHECKING, Any, BinaryIO, Self, TextIO, TypeAlias, cast
from typing import TYPE_CHECKING, Any, BinaryIO, Self, TextIO, cast

import zstandard

Expand Down Expand Up @@ -343,7 +343,7 @@ class _PenlogRecordV2:
_python_func_name: str | None = None


_PenlogRecord: TypeAlias = _PenlogRecordV2
type _PenlogRecord = _PenlogRecordV2


def _colorize_msg(data: str, levelno: int) -> tuple[str, int]:
Expand Down
10 changes: 5 additions & 5 deletions src/gallia/pydantic_argparse/argparse/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import argparse
import sys
from typing import Any, Generic, Never, NoReturn
from typing import Any, Never, NoReturn

from pydantic import BaseModel, ValidationError

Expand All @@ -29,10 +29,10 @@
from gallia.pydantic_argparse.parsers import command
from gallia.pydantic_argparse.utils.field import ArgFieldInfo
from gallia.pydantic_argparse.utils.nesting import _NestedArgumentParser
from gallia.pydantic_argparse.utils.pydantic import PydanticField, PydanticModelT
from gallia.pydantic_argparse.utils.pydantic import PydanticField


class ArgumentParser(argparse.ArgumentParser, Generic[PydanticModelT]):
class ArgumentParser[T: BaseModel](argparse.ArgumentParser):
"""Declarative and Typed Argument Parser.
The `ArgumentParser` declaratively generates a command-line interface using
Expand Down Expand Up @@ -60,7 +60,7 @@ class ArgumentParser(argparse.ArgumentParser, Generic[PydanticModelT]):

def __init__(
self,
model: type[PydanticModelT],
model: type[T],
prog: str | None = None,
description: str | None = None,
version: str | None = None,
Expand Down Expand Up @@ -116,7 +116,7 @@ def __init__(
def parse_typed_args(
self,
args: list[str] | None = None,
) -> tuple[PydanticModelT, BaseModel]:
) -> tuple[T, BaseModel]:
"""Parses command line arguments.
If `args` are not supplied by the user, then they are automatically
Expand Down
16 changes: 8 additions & 8 deletions src/gallia/pydantic_argparse/utils/nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@
"""Utilities to help with parsing arbitrarily nested `pydantic` models."""

from argparse import Namespace
from typing import Any, Generic, TypeAlias
from typing import Any

from boltons.iterutils import get_path, remap # type: ignore
from pydantic import BaseModel

from .namespaces import to_dict
from .pydantic import PydanticField, PydanticModelT
from .pydantic import PydanticField

ModelT: TypeAlias = PydanticModelT | type[PydanticModelT] | BaseModel | type[BaseModel]


class _NestedArgumentParser(Generic[PydanticModelT]):
class _NestedArgumentParser[T: BaseModel]:
"""Parses arbitrarily nested `pydantic` models and inserts values passed at the command line."""

def __init__(
self,
model: PydanticModelT | type[PydanticModelT],
model: T | type[T],
namespace: Namespace,
) -> None:
self.model = model
Expand All @@ -30,7 +28,9 @@ def __init__(
self.schema: dict[str, Any] = self._get_nested_model_fields(self.model, namespace)
self.schema = self._remove_null_leaves(self.schema)

def _get_nested_model_fields(self, model: ModelT[Any], namespace: Namespace) -> dict[str, Any]:
def _get_nested_model_fields(
self, model: T | type[T] | BaseModel | type[BaseModel], namespace: Namespace
) -> dict[str, Any]:
def contains_subcommand(ns: Namespace, subcommand_path: tuple[str, ...]) -> bool:
for step in subcommand_path:
tmp = getattr(ns, step, None)
Expand Down Expand Up @@ -82,7 +82,7 @@ def _remove_null_leaves(self, schema: dict[str, Any]) -> Any:
# the schema
return remap(schema, visit=lambda p, k, v: v is not None)

def validate(self) -> tuple[PydanticModelT, BaseModel]:
def validate(self) -> tuple[T, BaseModel]:
"""Return the root of the model, as well as the sub-model for the bottom subcommand"""
model = self.model.model_validate(self.schema)
subcommand = model
Expand Down
12 changes: 3 additions & 9 deletions src/gallia/pydantic_argparse/utils/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Annotated,
Any,
Literal,
TypeVar,
Self,
Union,
cast,
get_args,
Expand All @@ -33,12 +33,6 @@

from .types import all_types

# Constants
T = TypeVar("T")
PydanticModelT = TypeVar("PydanticModelT", bound=BaseModel)
PydanticValidator = classmethod
NoneType = type(None)


@dataclass
class PydanticField:
Expand All @@ -55,7 +49,7 @@ class PydanticField:
extra_default: tuple[str, Any] | None = None

@classmethod
def parse_model(cls, model: BaseModel | type[BaseModel]) -> Iterator["PydanticField"]:
def parse_model(cls, model: BaseModel | type[BaseModel]) -> Iterator[Self]:
"""Iterator over the pydantic model fields, yielding this wrapper class.
Yields:
Expand All @@ -71,7 +65,7 @@ def _get_type(self, annotation: type | None) -> type | tuple[type | None, ...] |
return origin
elif origin is Union or origin is UnionType:
args = get_args(annotation)
types = [arg for arg in args if arg is not NoneType]
types = [arg for arg in args if arg is not type(None)]
elif origin is None:
types = [annotation]
else:
Expand Down
4 changes: 2 additions & 2 deletions src/gallia/transports/flexray_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
from enum import IntEnum, unique
from itertools import batched
from typing import ClassVar, Self, TypeAlias
from typing import ClassVar, Self

from pydantic import BaseModel, ConfigDict, field_validator

Expand Down Expand Up @@ -281,7 +281,7 @@ def parse(cls, data: bytes) -> Self:
)


FlexRayTPFrame: TypeAlias = (
type FlexRayTPFrame = (
FlexRayTPSingleFrame
| FlexRayTPFirstFrame
| FlexRayTPConsecutiveFrame
Expand Down

0 comments on commit 0c48eda

Please sign in to comment.