diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2021-01-19 11:58:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-19 11:58:10 -0700 |
commit | 09416c2934a5f592970e1d97c0298924eed009f6 (patch) | |
tree | 8986f70c94ba539885f71ebc679fef869569db08 | |
parent | a56fe3b1b3fd358329736dee1edb0ad24eadfce6 (diff) | |
parent | 0a32e68bced83ce045f043c916a1b0cfa78df967 (diff) | |
download | numpy-09416c2934a5f592970e1d97c0298924eed009f6.tar.gz |
Merge pull request #18174 from BvB93/covariant
MAINT: Changed the `NBitBase` variancy in `number` from co- to invariant
-rw-r--r-- | numpy/__init__.pyi | 182 | ||||
-rw-r--r-- | numpy/typing/__init__.py | 8 | ||||
-rw-r--r-- | numpy/typing/_callable.py | 128 | ||||
-rw-r--r-- | numpy/typing/tests/data/fail/scalars.py | 6 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/nbit_base_example.py | 5 |
5 files changed, 169 insertions, 160 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index be87a166e..dbf807783 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1527,8 +1527,8 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType]): # See https://github.com/numpy/numpy-stubs/pull/80 for more details. _ScalarType = TypeVar("_ScalarType", bound=generic) -_NBit_co = TypeVar("_NBit_co", covariant=True, bound=NBitBase) -_NBit_co2 = TypeVar("_NBit_co2", covariant=True, bound=NBitBase) +_NBit1 = TypeVar("_NBit1", bound=NBitBase) +_NBit2 = TypeVar("_NBit2", bound=NBitBase) class generic(_ArrayOrScalarCommon): @abstractmethod @@ -1562,7 +1562,7 @@ class generic(_ArrayOrScalarCommon): @property def dtype(self: _ScalarType) -> dtype[_ScalarType]: ... -class number(generic, Generic[_NBit_co]): # type: ignore +class number(generic, Generic[_NBit1]): # type: ignore @property def real(self: _ArraySelf) -> _ArraySelf: ... @property @@ -1675,12 +1675,12 @@ else: _FloatValue = Union[None, _CharLike_co, SupportsFloat] _ComplexValue = Union[None, _CharLike_co, SupportsFloat, SupportsComplex] -class integer(number[_NBit_co]): # type: ignore +class integer(number[_NBit1]): # type: ignore # NOTE: `__index__` is technically defined in the bottom-most # sub-classes (`int64`, `uint32`, etc) def __index__(self) -> int: ... - __truediv__: _IntTrueDiv[_NBit_co] - __rtruediv__: _IntTrueDiv[_NBit_co] + __truediv__: _IntTrueDiv[_NBit1] + __rtruediv__: _IntTrueDiv[_NBit1] def __mod__(self, value: _IntLike_co) -> integer: ... def __rmod__(self, value: _IntLike_co) -> integer: ... def __invert__(self: _IntType) -> _IntType: ... @@ -1696,32 +1696,32 @@ class integer(number[_NBit_co]): # type: ignore def __xor__(self, other: _IntLike_co) -> integer: ... def __rxor__(self, other: _IntLike_co) -> integer: ... -class signedinteger(integer[_NBit_co]): +class signedinteger(integer[_NBit1]): def __init__(self, __value: _IntValue = ...) -> None: ... - __add__: _SignedIntOp[_NBit_co] - __radd__: _SignedIntOp[_NBit_co] - __sub__: _SignedIntOp[_NBit_co] - __rsub__: _SignedIntOp[_NBit_co] - __mul__: _SignedIntOp[_NBit_co] - __rmul__: _SignedIntOp[_NBit_co] - __floordiv__: _SignedIntOp[_NBit_co] - __rfloordiv__: _SignedIntOp[_NBit_co] - __pow__: _SignedIntOp[_NBit_co] - __rpow__: _SignedIntOp[_NBit_co] - __lshift__: _SignedIntBitOp[_NBit_co] - __rlshift__: _SignedIntBitOp[_NBit_co] - __rshift__: _SignedIntBitOp[_NBit_co] - __rrshift__: _SignedIntBitOp[_NBit_co] - __and__: _SignedIntBitOp[_NBit_co] - __rand__: _SignedIntBitOp[_NBit_co] - __xor__: _SignedIntBitOp[_NBit_co] - __rxor__: _SignedIntBitOp[_NBit_co] - __or__: _SignedIntBitOp[_NBit_co] - __ror__: _SignedIntBitOp[_NBit_co] - __mod__: _SignedIntMod[_NBit_co] - __rmod__: _SignedIntMod[_NBit_co] - __divmod__: _SignedIntDivMod[_NBit_co] - __rdivmod__: _SignedIntDivMod[_NBit_co] + __add__: _SignedIntOp[_NBit1] + __radd__: _SignedIntOp[_NBit1] + __sub__: _SignedIntOp[_NBit1] + __rsub__: _SignedIntOp[_NBit1] + __mul__: _SignedIntOp[_NBit1] + __rmul__: _SignedIntOp[_NBit1] + __floordiv__: _SignedIntOp[_NBit1] + __rfloordiv__: _SignedIntOp[_NBit1] + __pow__: _SignedIntOp[_NBit1] + __rpow__: _SignedIntOp[_NBit1] + __lshift__: _SignedIntBitOp[_NBit1] + __rlshift__: _SignedIntBitOp[_NBit1] + __rshift__: _SignedIntBitOp[_NBit1] + __rrshift__: _SignedIntBitOp[_NBit1] + __and__: _SignedIntBitOp[_NBit1] + __rand__: _SignedIntBitOp[_NBit1] + __xor__: _SignedIntBitOp[_NBit1] + __rxor__: _SignedIntBitOp[_NBit1] + __or__: _SignedIntBitOp[_NBit1] + __ror__: _SignedIntBitOp[_NBit1] + __mod__: _SignedIntMod[_NBit1] + __rmod__: _SignedIntMod[_NBit1] + __divmod__: _SignedIntDivMod[_NBit1] + __rdivmod__: _SignedIntDivMod[_NBit1] int8 = signedinteger[_8Bit] int16 = signedinteger[_16Bit] @@ -1767,33 +1767,33 @@ class timedelta64(generic): __gt__: _ComparisonOp[Union[timedelta64, _IntLike_co, _BoolLike_co]] __ge__: _ComparisonOp[Union[timedelta64, _IntLike_co, _BoolLike_co]] -class unsignedinteger(integer[_NBit_co]): +class unsignedinteger(integer[_NBit1]): # NOTE: `uint64 + signedinteger -> float64` def __init__(self, __value: _IntValue = ...) -> None: ... - __add__: _UnsignedIntOp[_NBit_co] - __radd__: _UnsignedIntOp[_NBit_co] - __sub__: _UnsignedIntOp[_NBit_co] - __rsub__: _UnsignedIntOp[_NBit_co] - __mul__: _UnsignedIntOp[_NBit_co] - __rmul__: _UnsignedIntOp[_NBit_co] - __floordiv__: _UnsignedIntOp[_NBit_co] - __rfloordiv__: _UnsignedIntOp[_NBit_co] - __pow__: _UnsignedIntOp[_NBit_co] - __rpow__: _UnsignedIntOp[_NBit_co] - __lshift__: _UnsignedIntBitOp[_NBit_co] - __rlshift__: _UnsignedIntBitOp[_NBit_co] - __rshift__: _UnsignedIntBitOp[_NBit_co] - __rrshift__: _UnsignedIntBitOp[_NBit_co] - __and__: _UnsignedIntBitOp[_NBit_co] - __rand__: _UnsignedIntBitOp[_NBit_co] - __xor__: _UnsignedIntBitOp[_NBit_co] - __rxor__: _UnsignedIntBitOp[_NBit_co] - __or__: _UnsignedIntBitOp[_NBit_co] - __ror__: _UnsignedIntBitOp[_NBit_co] - __mod__: _UnsignedIntMod[_NBit_co] - __rmod__: _UnsignedIntMod[_NBit_co] - __divmod__: _UnsignedIntDivMod[_NBit_co] - __rdivmod__: _UnsignedIntDivMod[_NBit_co] + __add__: _UnsignedIntOp[_NBit1] + __radd__: _UnsignedIntOp[_NBit1] + __sub__: _UnsignedIntOp[_NBit1] + __rsub__: _UnsignedIntOp[_NBit1] + __mul__: _UnsignedIntOp[_NBit1] + __rmul__: _UnsignedIntOp[_NBit1] + __floordiv__: _UnsignedIntOp[_NBit1] + __rfloordiv__: _UnsignedIntOp[_NBit1] + __pow__: _UnsignedIntOp[_NBit1] + __rpow__: _UnsignedIntOp[_NBit1] + __lshift__: _UnsignedIntBitOp[_NBit1] + __rlshift__: _UnsignedIntBitOp[_NBit1] + __rshift__: _UnsignedIntBitOp[_NBit1] + __rrshift__: _UnsignedIntBitOp[_NBit1] + __and__: _UnsignedIntBitOp[_NBit1] + __rand__: _UnsignedIntBitOp[_NBit1] + __xor__: _UnsignedIntBitOp[_NBit1] + __rxor__: _UnsignedIntBitOp[_NBit1] + __or__: _UnsignedIntBitOp[_NBit1] + __ror__: _UnsignedIntBitOp[_NBit1] + __mod__: _UnsignedIntMod[_NBit1] + __rmod__: _UnsignedIntMod[_NBit1] + __divmod__: _UnsignedIntDivMod[_NBit1] + __rdivmod__: _UnsignedIntDivMod[_NBit1] uint8 = unsignedinteger[_8Bit] uint16 = unsignedinteger[_16Bit] @@ -1808,29 +1808,29 @@ uint0 = unsignedinteger[_NBitIntP] uint = unsignedinteger[_NBitInt] ulonglong = unsignedinteger[_NBitLongLong] -class inexact(number[_NBit_co]): ... # type: ignore +class inexact(number[_NBit1]): ... # type: ignore _IntType = TypeVar("_IntType", bound=integer) _FloatType = TypeVar('_FloatType', bound=floating) -class floating(inexact[_NBit_co]): +class floating(inexact[_NBit1]): def __init__(self, __value: _FloatValue = ...) -> None: ... - __add__: _FloatOp[_NBit_co] - __radd__: _FloatOp[_NBit_co] - __sub__: _FloatOp[_NBit_co] - __rsub__: _FloatOp[_NBit_co] - __mul__: _FloatOp[_NBit_co] - __rmul__: _FloatOp[_NBit_co] - __truediv__: _FloatOp[_NBit_co] - __rtruediv__: _FloatOp[_NBit_co] - __floordiv__: _FloatOp[_NBit_co] - __rfloordiv__: _FloatOp[_NBit_co] - __pow__: _FloatOp[_NBit_co] - __rpow__: _FloatOp[_NBit_co] - __mod__: _FloatMod[_NBit_co] - __rmod__: _FloatMod[_NBit_co] - __divmod__: _FloatDivMod[_NBit_co] - __rdivmod__: _FloatDivMod[_NBit_co] + __add__: _FloatOp[_NBit1] + __radd__: _FloatOp[_NBit1] + __sub__: _FloatOp[_NBit1] + __rsub__: _FloatOp[_NBit1] + __mul__: _FloatOp[_NBit1] + __rmul__: _FloatOp[_NBit1] + __truediv__: _FloatOp[_NBit1] + __rtruediv__: _FloatOp[_NBit1] + __floordiv__: _FloatOp[_NBit1] + __rfloordiv__: _FloatOp[_NBit1] + __pow__: _FloatOp[_NBit1] + __rpow__: _FloatOp[_NBit1] + __mod__: _FloatMod[_NBit1] + __rmod__: _FloatMod[_NBit1] + __divmod__: _FloatDivMod[_NBit1] + __rdivmod__: _FloatDivMod[_NBit1] float16 = floating[_16Bit] float32 = floating[_32Bit] @@ -1847,25 +1847,25 @@ longfloat = floating[_NBitLongDouble] # It is used to clarify why `complex128`s precision is `_64Bit`, the latter # describing the two 64 bit floats representing its real and imaginary component -class complexfloating(inexact[_NBit_co], Generic[_NBit_co, _NBit_co2]): +class complexfloating(inexact[_NBit1], Generic[_NBit1, _NBit2]): def __init__(self, __value: _ComplexValue = ...) -> None: ... @property - def real(self) -> floating[_NBit_co]: ... # type: ignore[override] - @property - def imag(self) -> floating[_NBit_co2]: ... # type: ignore[override] - def __abs__(self) -> floating[_NBit_co]: ... # type: ignore[override] - __add__: _ComplexOp[_NBit_co] - __radd__: _ComplexOp[_NBit_co] - __sub__: _ComplexOp[_NBit_co] - __rsub__: _ComplexOp[_NBit_co] - __mul__: _ComplexOp[_NBit_co] - __rmul__: _ComplexOp[_NBit_co] - __truediv__: _ComplexOp[_NBit_co] - __rtruediv__: _ComplexOp[_NBit_co] - __floordiv__: _ComplexOp[_NBit_co] - __rfloordiv__: _ComplexOp[_NBit_co] - __pow__: _ComplexOp[_NBit_co] - __rpow__: _ComplexOp[_NBit_co] + def real(self) -> floating[_NBit1]: ... # type: ignore[override] + @property + def imag(self) -> floating[_NBit2]: ... # type: ignore[override] + def __abs__(self) -> floating[_NBit1]: ... # type: ignore[override] + __add__: _ComplexOp[_NBit1] + __radd__: _ComplexOp[_NBit1] + __sub__: _ComplexOp[_NBit1] + __rsub__: _ComplexOp[_NBit1] + __mul__: _ComplexOp[_NBit1] + __rmul__: _ComplexOp[_NBit1] + __truediv__: _ComplexOp[_NBit1] + __rtruediv__: _ComplexOp[_NBit1] + __floordiv__: _ComplexOp[_NBit1] + __rfloordiv__: _ComplexOp[_NBit1] + __pow__: _ComplexOp[_NBit1] + __rpow__: _ComplexOp[_NBit1] complex64 = complexfloating[_32Bit, _32Bit] complex128 = complexfloating[_64Bit, _64Bit] diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py index 3cfdfe50a..a1d943235 100644 --- a/numpy/typing/__init__.py +++ b/numpy/typing/__init__.py @@ -184,13 +184,15 @@ class NBitBase: .. code-block:: python - >>> from typing import TypeVar, TYPE_CHECKING + >>> from __future__ import annotations + >>> from typing import TypeVar, Union, TYPE_CHECKING >>> import numpy as np >>> import numpy.typing as npt - >>> T = TypeVar("T", bound=npt.NBitBase) + >>> T1 = TypeVar("T1", bound=npt.NBitBase) + >>> T2 = TypeVar("T2", bound=npt.NBitBase) - >>> def add(a: "np.floating[T]", b: "np.integer[T]") -> "np.floating[T]": + >>> def add(a: np.floating[T1], b: np.integer[T2]) -> np.floating[Union[T1, T2]]: ... return a + b >>> a = np.float16() diff --git a/numpy/typing/_callable.py b/numpy/typing/_callable.py index 693b6dfed..e1fdee3ba 100644 --- a/numpy/typing/_callable.py +++ b/numpy/typing/_callable.py @@ -61,8 +61,8 @@ if TYPE_CHECKING or HAVE_PROTOCOL: _T = TypeVar("_T") _2Tuple = Tuple[_T, _T] - _NBit_co = TypeVar("_NBit_co", covariant=True, bound=NBitBase) - _NBit = TypeVar("_NBit", bound=NBitBase) + _NBit1 = TypeVar("_NBit1", bound=NBitBase) + _NBit2 = TypeVar("_NBit2", bound=NBitBase) _IntType = TypeVar("_IntType", bound=integer) _FloatType = TypeVar("_FloatType", bound=floating) @@ -141,9 +141,9 @@ if TYPE_CHECKING or HAVE_PROTOCOL: @overload def __call__(self, __other: _FloatLike_co) -> timedelta64: ... - class _IntTrueDiv(Protocol[_NBit_co]): + class _IntTrueDiv(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> floating[_NBit_co]: ... + def __call__(self, __other: bool) -> floating[_NBit1]: ... @overload def __call__(self, __other: int) -> floating[_NBitInt]: ... @overload @@ -151,12 +151,12 @@ if TYPE_CHECKING or HAVE_PROTOCOL: @overload def __call__(self, __other: complex) -> complex128: ... @overload - def __call__(self, __other: integer[_NBit]) -> floating[Union[_NBit_co, _NBit]]: ... + def __call__(self, __other: integer[_NBit2]) -> floating[Union[_NBit1, _NBit2]]: ... - class _UnsignedIntOp(Protocol[_NBit_co]): + class _UnsignedIntOp(Protocol[_NBit1]): # NOTE: `uint64 + signedinteger -> float64` @overload - def __call__(self, __other: bool) -> unsignedinteger[_NBit_co]: ... + def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... @overload def __call__( self, __other: Union[int, signedinteger[Any]] @@ -167,24 +167,24 @@ if TYPE_CHECKING or HAVE_PROTOCOL: def __call__(self, __other: complex) -> complex128: ... @overload def __call__( - self, __other: unsignedinteger[_NBit] - ) -> unsignedinteger[Union[_NBit_co, _NBit]]: ... + self, __other: unsignedinteger[_NBit2] + ) -> unsignedinteger[Union[_NBit1, _NBit2]]: ... - class _UnsignedIntBitOp(Protocol[_NBit_co]): + class _UnsignedIntBitOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> unsignedinteger[_NBit_co]: ... + def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... @overload def __call__(self, __other: int) -> signedinteger[Any]: ... @overload def __call__(self, __other: signedinteger[Any]) -> signedinteger[Any]: ... @overload def __call__( - self, __other: unsignedinteger[_NBit] - ) -> unsignedinteger[Union[_NBit_co, _NBit]]: ... + self, __other: unsignedinteger[_NBit2] + ) -> unsignedinteger[Union[_NBit1, _NBit2]]: ... - class _UnsignedIntMod(Protocol[_NBit_co]): + class _UnsignedIntMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> unsignedinteger[_NBit_co]: ... + def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ... @overload def __call__( self, __other: Union[int, signedinteger[Any]] @@ -193,12 +193,12 @@ if TYPE_CHECKING or HAVE_PROTOCOL: def __call__(self, __other: float) -> float64: ... @overload def __call__( - self, __other: unsignedinteger[_NBit] - ) -> unsignedinteger[Union[_NBit_co, _NBit]]: ... + self, __other: unsignedinteger[_NBit2] + ) -> unsignedinteger[Union[_NBit1, _NBit2]]: ... - class _UnsignedIntDivMod(Protocol[_NBit_co]): + class _UnsignedIntDivMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit_co]]: ... + def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ... @overload def __call__( self, __other: Union[int, signedinteger[Any]] @@ -207,114 +207,114 @@ if TYPE_CHECKING or HAVE_PROTOCOL: def __call__(self, __other: float) -> _2Tuple[float64]: ... @overload def __call__( - self, __other: unsignedinteger[_NBit] - ) -> _2Tuple[unsignedinteger[Union[_NBit_co, _NBit]]]: ... + self, __other: unsignedinteger[_NBit2] + ) -> _2Tuple[unsignedinteger[Union[_NBit1, _NBit2]]]: ... - class _SignedIntOp(Protocol[_NBit_co]): + class _SignedIntOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> signedinteger[_NBit_co]: ... + def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... @overload - def __call__(self, __other: int) -> signedinteger[Union[_NBit_co, _NBitInt]]: ... + def __call__(self, __other: int) -> signedinteger[Union[_NBit1, _NBitInt]]: ... @overload def __call__(self, __other: float) -> float64: ... @overload def __call__(self, __other: complex) -> complex128: ... @overload def __call__( - self, __other: signedinteger[_NBit] - ) -> signedinteger[Union[_NBit_co, _NBit]]: ... + self, __other: signedinteger[_NBit2] + ) -> signedinteger[Union[_NBit1, _NBit2]]: ... - class _SignedIntBitOp(Protocol[_NBit_co]): + class _SignedIntBitOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> signedinteger[_NBit_co]: ... + def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... @overload - def __call__(self, __other: int) -> signedinteger[Union[_NBit_co, _NBitInt]]: ... + def __call__(self, __other: int) -> signedinteger[Union[_NBit1, _NBitInt]]: ... @overload def __call__( - self, __other: signedinteger[_NBit] - ) -> signedinteger[Union[_NBit_co, _NBit]]: ... + self, __other: signedinteger[_NBit2] + ) -> signedinteger[Union[_NBit1, _NBit2]]: ... - class _SignedIntMod(Protocol[_NBit_co]): + class _SignedIntMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> signedinteger[_NBit_co]: ... + def __call__(self, __other: bool) -> signedinteger[_NBit1]: ... @overload - def __call__(self, __other: int) -> signedinteger[Union[_NBit_co, _NBitInt]]: ... + def __call__(self, __other: int) -> signedinteger[Union[_NBit1, _NBitInt]]: ... @overload def __call__(self, __other: float) -> float64: ... @overload def __call__( - self, __other: signedinteger[_NBit] - ) -> signedinteger[Union[_NBit_co, _NBit]]: ... + self, __other: signedinteger[_NBit2] + ) -> signedinteger[Union[_NBit1, _NBit2]]: ... - class _SignedIntDivMod(Protocol[_NBit_co]): + class _SignedIntDivMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit_co]]: ... + def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ... @overload - def __call__(self, __other: int) -> _2Tuple[signedinteger[Union[_NBit_co, _NBitInt]]]: ... + def __call__(self, __other: int) -> _2Tuple[signedinteger[Union[_NBit1, _NBitInt]]]: ... @overload def __call__(self, __other: float) -> _2Tuple[float64]: ... @overload def __call__( - self, __other: signedinteger[_NBit] - ) -> _2Tuple[signedinteger[Union[_NBit_co, _NBit]]]: ... + self, __other: signedinteger[_NBit2] + ) -> _2Tuple[signedinteger[Union[_NBit1, _NBit2]]]: ... - class _FloatOp(Protocol[_NBit_co]): + class _FloatOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> floating[_NBit_co]: ... + def __call__(self, __other: bool) -> floating[_NBit1]: ... @overload - def __call__(self, __other: int) -> floating[Union[_NBit_co, _NBitInt]]: ... + def __call__(self, __other: int) -> floating[Union[_NBit1, _NBitInt]]: ... @overload def __call__(self, __other: float) -> float64: ... @overload def __call__(self, __other: complex) -> complex128: ... @overload def __call__( - self, __other: Union[integer[_NBit], floating[_NBit]] - ) -> floating[Union[_NBit_co, _NBit]]: ... + self, __other: Union[integer[_NBit2], floating[_NBit2]] + ) -> floating[Union[_NBit1, _NBit2]]: ... - class _FloatMod(Protocol[_NBit_co]): + class _FloatMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> floating[_NBit_co]: ... + def __call__(self, __other: bool) -> floating[_NBit1]: ... @overload - def __call__(self, __other: int) -> floating[Union[_NBit_co, _NBitInt]]: ... + def __call__(self, __other: int) -> floating[Union[_NBit1, _NBitInt]]: ... @overload def __call__(self, __other: float) -> float64: ... @overload def __call__( - self, __other: Union[integer[_NBit], floating[_NBit]] - ) -> floating[Union[_NBit_co, _NBit]]: ... + self, __other: Union[integer[_NBit2], floating[_NBit2]] + ) -> floating[Union[_NBit1, _NBit2]]: ... - class _FloatDivMod(Protocol[_NBit_co]): + class _FloatDivMod(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> _2Tuple[floating[_NBit_co]]: ... + def __call__(self, __other: bool) -> _2Tuple[floating[_NBit1]]: ... @overload - def __call__(self, __other: int) -> _2Tuple[floating[Union[_NBit_co, _NBitInt]]]: ... + def __call__(self, __other: int) -> _2Tuple[floating[Union[_NBit1, _NBitInt]]]: ... @overload def __call__(self, __other: float) -> _2Tuple[float64]: ... @overload def __call__( - self, __other: Union[integer[_NBit], floating[_NBit]] - ) -> _2Tuple[floating[Union[_NBit_co, _NBit]]]: ... + self, __other: Union[integer[_NBit2], floating[_NBit2]] + ) -> _2Tuple[floating[Union[_NBit1, _NBit2]]]: ... - class _ComplexOp(Protocol[_NBit_co]): + class _ComplexOp(Protocol[_NBit1]): @overload - def __call__(self, __other: bool) -> complexfloating[_NBit_co, _NBit_co]: ... + def __call__(self, __other: bool) -> complexfloating[_NBit1, _NBit1]: ... @overload - def __call__(self, __other: int) -> complexfloating[Union[_NBit_co, _NBitInt], Union[_NBit_co, _NBitInt]]: ... + def __call__(self, __other: int) -> complexfloating[Union[_NBit1, _NBitInt], Union[_NBit1, _NBitInt]]: ... @overload def __call__(self, __other: Union[float, complex]) -> complex128: ... @overload def __call__( self, __other: Union[ - integer[_NBit], - floating[_NBit], - complexfloating[_NBit, _NBit], + integer[_NBit2], + floating[_NBit2], + complexfloating[_NBit2, _NBit2], ] - ) -> complexfloating[Union[_NBit_co, _NBit], Union[_NBit_co, _NBit]]: ... + ) -> complexfloating[Union[_NBit1, _NBit2], Union[_NBit1, _NBit2]]: ... class _NumberOp(Protocol): - def __call__(self, __other: _NumberLike_co) -> number: ... + def __call__(self, __other: _NumberLike_co) -> Any: ... class _ComparisonOp(Protocol[_T]): @overload diff --git a/numpy/typing/tests/data/fail/scalars.py b/numpy/typing/tests/data/fail/scalars.py index f09740875..0aeff398f 100644 --- a/numpy/typing/tests/data/fail/scalars.py +++ b/numpy/typing/tests/data/fail/scalars.py @@ -1,5 +1,6 @@ import numpy as np +f2: np.float16 f8: np.float64 # Construction @@ -74,3 +75,8 @@ f8.item((0, 1)) # E: incompatible type f8.squeeze(axis=1) # E: incompatible type f8.squeeze(axis=(0, 1)) # E: incompatible type f8.transpose(1) # E: incompatible type + +def func(a: np.float32) -> None: ... + +func(f2) # E: incompatible type +func(f8) # E: incompatible type diff --git a/numpy/typing/tests/data/reveal/nbit_base_example.py b/numpy/typing/tests/data/reveal/nbit_base_example.py index 99fb71560..d34f6f69a 100644 --- a/numpy/typing/tests/data/reveal/nbit_base_example.py +++ b/numpy/typing/tests/data/reveal/nbit_base_example.py @@ -2,9 +2,10 @@ from typing import TypeVar, Union import numpy as np import numpy.typing as npt -T = TypeVar("T", bound=npt.NBitBase) +T1 = TypeVar("T1", bound=npt.NBitBase) +T2 = TypeVar("T2", bound=npt.NBitBase) -def add(a: np.floating[T], b: np.integer[T]) -> np.floating[T]: +def add(a: np.floating[T1], b: np.integer[T2]) -> np.floating[Union[T1, T2]]: return a + b i8: np.int64 |