From 543aa5d1482a646a553dc3031ffb2928c94fb4a0 Mon Sep 17 00:00:00 2001 From: Matt Bogosian Date: Mon, 5 Aug 2024 19:04:59 -0500 Subject: [PATCH] Use built-in type indexing --- .github/workflows/on-push.yaml | 2 +- numerary/__init__.py | 10 ++++---- numerary/protocol.py | 23 +++++++++--------- numerary/types.py | 44 +++++++++++++++++----------------- setup.cfg | 2 +- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/.github/workflows/on-push.yaml b/.github/workflows/on-push.yaml index 203dc45..b2b22bd 100644 --- a/.github/workflows/on-push.yaml +++ b/.github/workflows/on-push.yaml @@ -76,7 +76,7 @@ jobs: [ -f "dist/${PKG}-${VERS_PATCH}-py3-none-any.whl" ] ( . .tox/check/bin/activate - mike deploy --rebase --update-aliases "${VERS}" latest + mike deploy --update-aliases "${VERS}" latest ) - name: publish to test PyPI uses: pypa/gh-action-pypi-publish@release/v1 diff --git a/numerary/__init__.py b/numerary/__init__.py index 4218288..f106c49 100644 --- a/numerary/__init__.py +++ b/numerary/__init__.py @@ -6,17 +6,17 @@ # software in any capacity. # ====================================================================================== -from typing import Tuple, Union +from typing import Union from .types import * # noqa: F401,F403 __all__ = () _VersionT = Union[ - Tuple[int, int, int], - Tuple[int, int, int, str], - Tuple[int, int, int, str, str], - Tuple[int, int, int, str, str, str], + tuple[int, int, int], + tuple[int, int, int, str], + tuple[int, int, int, str, str], + tuple[int, int, int, str, str, str], ] __version__: _VersionT diff --git a/numerary/protocol.py b/numerary/protocol.py index e6b7bb3..7bbd829 100644 --- a/numerary/protocol.py +++ b/numerary/protocol.py @@ -7,7 +7,7 @@ # ====================================================================================== from collections import defaultdict -from typing import TYPE_CHECKING, Any, Dict, Set, Tuple, Type, TypeVar +from typing import TYPE_CHECKING, Any, TypeVar __all__ = ("CachingProtocolMeta",) @@ -15,7 +15,6 @@ # ---- Types --------------------------------------------------------------------------- -_T_co = TypeVar("_T_co", covariant=True) _TT = TypeVar("_TT", bound="CachingProtocolMeta") @@ -38,17 +37,17 @@ class CachingProtocolMeta(_BeartypeCachingProtocolMeta): overriding runtime checks. """ - _abc_inst_check_cache_overridden: Dict[Type, bool] - _abc_inst_check_cache_listeners: Set["CachingProtocolMeta"] + _abc_inst_check_cache_overridden: dict[type, bool] + _abc_inst_check_cache_listeners: set["CachingProtocolMeta"] # Defined in beartype.typing.Protocol from which we inherit - _abc_inst_check_cache: Dict[type, bool] + _abc_inst_check_cache: dict[type, bool] def __new__( - mcls: Type[_TT], + mcls: type[_TT], name: str, - bases: Tuple[Type, ...], - namespace: Dict[str, Any], + bases: tuple[type, ...], + namespace: dict[str, Any], **kw: Any, ) -> _TT: cls = super().__new__(mcls, name, bases, namespace, **kw) @@ -65,7 +64,7 @@ def __new__( return cls - def includes(cls, inst_t: Type) -> None: + def includes(cls, inst_t: type) -> None: r""" Registers *inst_t* as supporting the interface in the runtime type-checking cache. This overrides any prior cached value. @@ -107,7 +106,7 @@ def includes(cls, inst_t: Type) -> None: cls._abc_inst_check_cache_overridden[inst_t] = True cls._dirty_for(inst_t) - def excludes(cls, inst_t: Type) -> None: + def excludes(cls, inst_t: type) -> None: r""" Registers *inst_t* as supporting the interface in the runtime type-checking cache. This overrides any prior cached value. @@ -150,7 +149,7 @@ def excludes(cls, inst_t: Type) -> None: cls._abc_inst_check_cache_overridden[inst_t] = True cls._dirty_for(inst_t) - def reset_for(cls, inst_t: Type) -> None: + def reset_for(cls, inst_t: type) -> None: r""" Clears any cached instance check for *inst_t*. """ @@ -159,7 +158,7 @@ def reset_for(cls, inst_t: Type) -> None: del cls._abc_inst_check_cache_overridden[inst_t] cls._dirty_for(inst_t) - def _dirty_for(cls, inst_t: Type) -> None: + def _dirty_for(cls, inst_t: type) -> None: for inheriting_cls in cls._abc_inst_check_cache_listeners: if ( inst_t in inheriting_cls._abc_inst_check_cache diff --git a/numerary/types.py b/numerary/types.py index d677594..5d13b33 100644 --- a/numerary/types.py +++ b/numerary/types.py @@ -15,7 +15,7 @@ from fractions import Fraction from typing import TYPE_CHECKING, Any, Generic, Optional from typing import Protocol as _Protocol -from typing import Tuple, Type, TypeVar, Union, overload, runtime_checkable +from typing import TypeVar, Union, overload, runtime_checkable from beartype.typing import SupportsAbs as _SupportsAbs from beartype.typing import SupportsComplex as _SupportsComplex @@ -241,11 +241,11 @@ class SupportsRealImag( methods.) ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import Any, TypeVar >>> from numerary.types import SupportsRealImag, real, imag >>> MyRealImagT = TypeVar("MyRealImagT", bound=SupportsRealImag) - >>> def real_imag_my_thing(arg: MyRealImagT) -> Tuple[Any, Any]: + >>> def real_imag_my_thing(arg: MyRealImagT) -> tuple[Any, Any]: ... assert isinstance(arg, SupportsRealImag) ... return (real(arg), imag(arg)) @@ -277,7 +277,7 @@ class _SupportsRealImagAsMethod(_Protocol): """ @abstractmethod - def as_real_imag(self) -> Tuple[Any, Any]: + def as_real_imag(self) -> tuple[Any, Any]: pass @@ -297,11 +297,11 @@ class SupportsRealImagAsMethod( helper functions. ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import Any, TypeVar >>> from numerary.types import SupportsRealImagAsMethod, real, imag >>> MyRealImagAsMethodT = TypeVar("MyRealImagAsMethodT", bound=SupportsRealImagAsMethod) - >>> def as_real_imag_my_thing(arg: MyRealImagAsMethodT) -> Tuple[Any, Any]: + >>> def as_real_imag_my_thing(arg: MyRealImagAsMethodT) -> tuple[Any, Any]: ... assert isinstance(arg, SupportsRealImagAsMethod) ... return (real(arg), imag(arg)) @@ -365,11 +365,11 @@ class SupportsTrunc( methods.) ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import Any, TypeVar >>> from numerary.types import SupportsTrunc, __trunc__ >>> MyTruncT = TypeVar("MyTruncT", bound=SupportsTrunc) - >>> def trunc_my_thing(arg: MyTruncT) -> Tuple[Any, Any]: + >>> def trunc_my_thing(arg: MyTruncT) -> tuple[Any, Any]: ... assert isinstance(arg, SupportsTrunc) ... return __trunc__(arg) @@ -442,11 +442,11 @@ class SupportsFloorCeil( [``__ceil__``][numerary.types.__ceil__] helper functions. ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import TypeVar >>> from numerary.types import SupportsFloorCeil, __ceil__, __floor__ >>> MyFloorCeilT = TypeVar("MyFloorCeilT", bound=SupportsFloorCeil) - >>> def floor_ceil_my_thing(arg: MyFloorCeilT) -> Tuple[int, int]: + >>> def floor_ceil_my_thing(arg: MyFloorCeilT) -> tuple[int, int]: ... assert isinstance(arg, SupportsFloorCeil) ... return __floor__(arg), __ceil__(arg) @@ -492,11 +492,11 @@ class _SupportsDivmod( """ @abstractmethod - def __divmod__(self, other: Any) -> Tuple[_T_co, _T_co]: + def __divmod__(self, other: Any) -> tuple[_T_co, _T_co]: pass @abstractmethod - def __rdivmod__(self, other: Any) -> Tuple[_T_co, _T_co]: + def __rdivmod__(self, other: Any) -> tuple[_T_co, _T_co]: pass @@ -518,11 +518,11 @@ class SupportsDivmod( methods.) ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import Any, TypeVar >>> from numerary.types import SupportsDivmod >>> MyDivmodT = TypeVar("MyDivmodT", bound=SupportsDivmod) - >>> def divmod_my_thing(arg: MyDivmodT, other: Any) -> Tuple[MyDivmodT, MyDivmodT]: + >>> def divmod_my_thing(arg: MyDivmodT, other: Any) -> tuple[MyDivmodT, MyDivmodT]: ... assert isinstance(arg, SupportsDivmod) ... return divmod(arg, other) @@ -587,11 +587,11 @@ class SupportsNumeratorDenominator( actual properties.) ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import TypeVar >>> from numerary.types import SupportsNumeratorDenominator, denominator, numerator >>> MyNumDenomT = TypeVar("MyNumDenomT", bound=SupportsNumeratorDenominator) - >>> def num_denom_my_thing(arg: MyNumDenomT) -> Tuple[int, int]: + >>> def num_denom_my_thing(arg: MyNumDenomT) -> tuple[int, int]: ... assert isinstance(arg, SupportsNumeratorDenominator) ... return numerator(arg), denominator(arg) @@ -739,7 +739,7 @@ class SupportsComplexOps( methods.) ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import TypeVar >>> from numerary.types import SupportsComplexOps >>> MyComplexOpsT = TypeVar("MyComplexOpsT", bound=SupportsComplexOps) @@ -804,7 +804,7 @@ class SupportsComplexPow( methods.) ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import TypeVar >>> from numerary.types import SupportsComplexPow >>> MyComplexPowT = TypeVar("MyComplexPowT", bound=SupportsComplexPow) @@ -896,7 +896,7 @@ class SupportsRealOps( methods.) ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import Any, TypeVar >>> from numerary.types import SupportsRealOps >>> MyRealOpsT = TypeVar("MyRealOpsT", bound=SupportsRealOps) @@ -1003,7 +1003,7 @@ class SupportsIntegralOps( methods.) ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import TypeVar >>> from numerary.types import SupportsIntegralOps >>> MyIntegralOpsT = TypeVar("MyIntegralOpsT", bound=SupportsIntegralOps) @@ -1065,7 +1065,7 @@ class SupportsIntegralPow( methods.) ``` python - >>> from typing import Any, Tuple, TypeVar + >>> from typing import TypeVar >>> from numerary.types import SupportsIntegralPow >>> MyIntegralPowT = TypeVar("MyIntegralPowT", bound=SupportsIntegralPow) @@ -1702,7 +1702,7 @@ def denominator(operand: SupportsNumeratorDenominatorMixedU): ) logging.getLogger(__name__).debug(traceback.format_exc()) else: - t: Optional[Type] + t: Optional[type] for t_name in ( "uint8", diff --git a/setup.cfg b/setup.cfg index 394c957..41e247a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -123,7 +123,7 @@ allowlist_externals = commands = rm -frv site mkdocs build --strict - python -c 'from setuptools import setup ; setup()' bdist_wheel + python -m pip wheel --no-deps --wheel-dir dist . twine check dist/* deps = --editable .