Skip to content

Commit

Permalink
Use built-in type indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
posita committed Aug 6, 2024
1 parent 95e8717 commit 93ce5ed
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
10 changes: 5 additions & 5 deletions numerary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 11 additions & 12 deletions numerary/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
# ======================================================================================

from collections import defaultdict
from typing import TYPE_CHECKING, Any, Dict, Set, Tuple, Type, TypeVar
from typing import TYPE_CHECKING, Any, TypeVar

__all__ = ("CachingProtocolMeta",)


# ---- Types ---------------------------------------------------------------------------


_T_co = TypeVar("_T_co", covariant=True)
_TT = TypeVar("_TT", bound="CachingProtocolMeta")


Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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*.
"""
Expand All @@ -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
Expand Down
44 changes: 22 additions & 22 deletions numerary/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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


Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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


Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 93ce5ed

Please sign in to comment.