From 1398a93521f82bf615c29bdb44ea0018681ebaf2 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Thu, 9 Feb 2023 15:32:50 +0100 Subject: [PATCH] Attempting type hinting improvements --- python_utils/__init__.py | 4 ++++ python_utils/containers.py | 18 ++++++++++++------ tox.ini | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/python_utils/__init__.py b/python_utils/__init__.py index de72a02..900bcc8 100644 --- a/python_utils/__init__.py +++ b/python_utils/__init__.py @@ -12,6 +12,7 @@ types, ) from .aio import acount +from .containers import CastedDict, LazyCastedDict, UniqueList from .converters import remap, scale_1024, to_float, to_int, to_str, to_unicode from .decorators import listify, set_attributes from .exceptions import raise_exception, reraise @@ -70,4 +71,7 @@ 'raise_exception', 'Logged', 'LoggerBase', + 'CastedDict', + 'LazyCastedDict', + 'UniqueList', ] diff --git a/python_utils/containers.py b/python_utils/containers.py index 68d48c8..f8920a8 100644 --- a/python_utils/containers.py +++ b/python_utils/containers.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import abc import typing from typing import Any, Generator @@ -9,11 +7,17 @@ if typing.TYPE_CHECKING: import _typeshed # noqa: F401 +#: A type alias for a type that can be used as a key in a dictionary. KT = types.TypeVar('KT') +#: A type alias for a type that can be used as a value in a dictionary. VT = types.TypeVar('VT') +#: A type alias for a dictionary with keys of type KT and values of type VT. DT = types.Dict[KT, VT] +#: A type alias for the casted type of a dictionary key. KT_cast = types.Optional[types.Callable[[Any], KT]] +#: A type alias for the casted type of a dictionary value. VT_cast = types.Optional[types.Callable[[Any], VT]] +#: A type alias for the hashable values of the `UniqueList` HT = types.TypeVar('HT', bound=types.Hashable) # Using types.Union instead of | since Python 3.7 doesn't fully support it @@ -61,7 +65,7 @@ class CastedDict(CastedDictBase[KT, VT]): Note that you can specify the types for mypy and type hinting with: CastedDict[int, int](int, int) - >>> d = CastedDict(int, int) + >>> d: CastedDict[int, int] = CastedDict(int, int) >>> d[1] = 2 >>> d['3'] = '4' >>> d.update({'5': '6'}) @@ -105,7 +109,7 @@ class LazyCastedDict(CastedDictBase[KT, VT]): Note that you can specify the types for mypy and type hinting with: LazyCastedDict[int, int](int, int) - >>> d = LazyCastedDict(int, int) + >>> d: LazyCastedDict[int, int] = LazyCastedDict(int, int) >>> d[1] = 2 >>> d['3'] = '4' >>> d.update({'5': '6'}) @@ -159,7 +163,9 @@ def __getitem__(self, key: Any) -> VT: return value - def items(self) -> Generator[tuple[KT, VT], None, None]: # type: ignore + def items( # type: ignore + self, + ) -> Generator[types.Tuple[KT, VT], None, None]: if self._value_cast is None: yield from super().items() else: @@ -208,7 +214,7 @@ class UniqueList(types.List[HT]): ValueError: Duplicate value: 4 ''' - _set: set[HT] + _set: types.Set[HT] def __init__( self, diff --git a/tox.ini b/tox.ini index 901cfce..8290500 100644 --- a/tox.ini +++ b/tox.ini @@ -60,5 +60,5 @@ commands = mkdir -p docs/_static sphinx-apidoc -o docs/ python_utils rm -f docs/modules.rst - sphinx-build -n -W -b html -d docs/_build/doctrees docs docs/_build/html {posargs} + sphinx-build -W -b html -d docs/_build/doctrees docs docs/_build/html {posargs}