diff options
-rw-r--r-- | azure-pipelines.yml | 5 | ||||
-rw-r--r-- | azure-steps-windows.yml | 2 | ||||
-rw-r--r-- | numpy/__init__.pyi | 337 | ||||
-rw-r--r-- | numpy/core/arrayprint.pyi | 10 | ||||
-rw-r--r-- | numpy/core/fromnumeric.pyi | 31 | ||||
-rw-r--r-- | numpy/core/function_base.pyi | 4 | ||||
-rw-r--r-- | numpy/f2py/cfuncs.py | 7 | ||||
-rw-r--r-- | numpy/random/_generator.pyx | 2 | ||||
-rw-r--r-- | numpy/typing/__init__.py | 54 | ||||
-rw-r--r-- | numpy/typing/_array_like.py | 24 | ||||
-rw-r--r-- | numpy/typing/_callable.py | 150 | ||||
-rw-r--r-- | numpy/typing/_scalars.py | 28 | ||||
-rw-r--r-- | numpy/typing/tests/data/fail/scalars.py | 6 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/nbit_base_example.py | 5 | ||||
-rw-r--r-- | test_requirements.txt | 2 |
15 files changed, 347 insertions, 320 deletions
diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 5f7afdaaf..b56d596a5 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -206,6 +206,11 @@ stages: PYTHON_ARCH: 'x64' TEST_MODE: full BITS: 64 + PyPy37-64bit-full: + PYTHON_VERSION: 'PyPy3.7' + PYTHON_ARCH: 'x64' + TEST_MODE: fast + BITS: 64 Python38-32bit-fast: PYTHON_VERSION: '3.8' PYTHON_ARCH: 'x86' diff --git a/azure-steps-windows.yml b/azure-steps-windows.yml index 28762f5d9..6a69db753 100644 --- a/azure-steps-windows.yml +++ b/azure-steps-windows.yml @@ -6,7 +6,7 @@ steps: architecture: $(PYTHON_ARCH) condition: not(contains(variables['PYTHON_VERSION'], 'PyPy')) - powershell: | - $url = "http://buildbot.pypy.org/nightly/py3.6/pypy-c-jit-latest-win32.zip" + $url = "http://buildbot.pypy.org/nightly/py3.7/pypy-c-jit-latest-win64.zip" $output = "pypy.zip" $wc = New-Object System.Net.WebClient $wc.DownloadFile($url, $output) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 656048173..dbf807783 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -7,19 +7,28 @@ from contextlib import ContextDecorator from numpy.core._internal import _ctypes from numpy.typing import ( + # Arrays ArrayLike, + + # DTypes DTypeLike, - _Shape, - _ShapeLike, - _CharLike, - _BoolLike, - _IntLike, - _FloatLike, - _ComplexLike, - _TD64Like, - _NumberLike, _SupportsDType, _VoidDTypeLike, + + # Shapes + _Shape, + _ShapeLike, + + # Scalars + _CharLike_co, + _BoolLike_co, + _IntLike_co, + _FloatLike_co, + _ComplexLike_co, + _TD64Like_co, + _NumberLike_co, + + # `number` precision NBitBase, _256Bit, _128Bit, @@ -39,8 +48,8 @@ from numpy.typing import ( _NBitSingle, _NBitDouble, _NBitLongDouble, -) -from numpy.typing import ( + + # Character codes _BoolCodes, _UInt8Codes, _UInt16Codes, @@ -858,11 +867,11 @@ _PartitionKind = Literal["introselect"] _SortKind = Literal["quicksort", "mergesort", "heapsort", "stable"] _SortSide = Literal["left", "right"] -_ArrayLikeBool = Union[_BoolLike, Sequence[_BoolLike], ndarray] +_ArrayLikeBool = Union[_BoolLike_co, Sequence[_BoolLike_co], ndarray] _ArrayLikeIntOrBool = Union[ - _IntLike, + _IntLike_co, ndarray, - Sequence[_IntLike], + Sequence[_IntLike_co], Sequence[Sequence[Any]], # TODO: wait for support for recursive types ] @@ -1073,7 +1082,7 @@ class _ArrayOrScalarCommon: axis: None = ..., out: None = ..., keepdims: Literal[False] = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> number: ... @overload @@ -1082,7 +1091,7 @@ class _ArrayOrScalarCommon: axis: Optional[_ShapeLike] = ..., out: None = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> Union[number, ndarray]: ... @overload @@ -1091,7 +1100,7 @@ class _ArrayOrScalarCommon: axis: Optional[_ShapeLike] = ..., out: _NdArraySubClass = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> _NdArraySubClass: ... @overload @@ -1124,7 +1133,7 @@ class _ArrayOrScalarCommon: axis: None = ..., out: None = ..., keepdims: Literal[False] = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> number: ... @overload @@ -1133,7 +1142,7 @@ class _ArrayOrScalarCommon: axis: Optional[_ShapeLike] = ..., out: None = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> Union[number, ndarray]: ... @overload @@ -1142,7 +1151,7 @@ class _ArrayOrScalarCommon: axis: Optional[_ShapeLike] = ..., out: _NdArraySubClass = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> _NdArraySubClass: ... def newbyteorder(self: _ArraySelf, __new_order: _ByteOrder = ...) -> _ArraySelf: ... @@ -1153,7 +1162,7 @@ class _ArrayOrScalarCommon: dtype: DTypeLike = ..., out: None = ..., keepdims: Literal[False] = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> number: ... @overload @@ -1163,7 +1172,7 @@ class _ArrayOrScalarCommon: dtype: DTypeLike = ..., out: None = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> Union[number, ndarray]: ... @overload @@ -1173,7 +1182,7 @@ class _ArrayOrScalarCommon: dtype: DTypeLike = ..., out: _NdArraySubClass = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> _NdArraySubClass: ... @overload @@ -1234,7 +1243,7 @@ class _ArrayOrScalarCommon: dtype: DTypeLike = ..., out: None = ..., keepdims: Literal[False] = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> number: ... @overload @@ -1244,7 +1253,7 @@ class _ArrayOrScalarCommon: dtype: DTypeLike = ..., out: None = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> Union[number, ndarray]: ... @overload @@ -1254,13 +1263,13 @@ class _ArrayOrScalarCommon: dtype: DTypeLike = ..., out: _NdArraySubClass = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> _NdArraySubClass: ... @overload def take( self, - indices: _IntLike, + indices: _IntLike_co, axis: Optional[int] = ..., out: None = ..., mode: _ModeKind = ..., @@ -1518,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 @@ -1553,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 @@ -1577,10 +1586,10 @@ class number(generic, Generic[_NBit_co]): # type: ignore __rpow__: _NumberOp __truediv__: _NumberOp __rtruediv__: _NumberOp - __lt__: _ComparisonOp[_NumberLike] - __le__: _ComparisonOp[_NumberLike] - __gt__: _ComparisonOp[_NumberLike] - __ge__: _ComparisonOp[_NumberLike] + __lt__: _ComparisonOp[_NumberLike_co] + __le__: _ComparisonOp[_NumberLike_co] + __gt__: _ComparisonOp[_NumberLike_co] + __ge__: _ComparisonOp[_NumberLike_co] class bool_(generic): def __init__(self, __value: object = ...) -> None: ... @@ -1619,10 +1628,10 @@ class bool_(generic): __rmod__: _BoolMod __divmod__: _BoolDivMod __rdivmod__: _BoolDivMod - __lt__: _ComparisonOp[_NumberLike] - __le__: _ComparisonOp[_NumberLike] - __gt__: _ComparisonOp[_NumberLike] - __ge__: _ComparisonOp[_NumberLike] + __lt__: _ComparisonOp[_NumberLike_co] + __le__: _ComparisonOp[_NumberLike_co] + __gt__: _ComparisonOp[_NumberLike_co] + __ge__: _ComparisonOp[_NumberLike_co] class object_(generic): def __init__(self, __value: object = ...) -> None: ... @@ -1635,21 +1644,21 @@ class datetime64(generic): @overload def __init__( self, - __value: Union[None, datetime64, _CharLike, dt.datetime] = ..., - __format: Union[_CharLike, Tuple[_CharLike, _IntLike]] = ..., + __value: Union[None, datetime64, _CharLike_co, dt.datetime] = ..., + __format: Union[_CharLike_co, Tuple[_CharLike_co, _IntLike_co]] = ..., ) -> None: ... @overload def __init__( self, __value: int, - __format: Union[_CharLike, Tuple[_CharLike, _IntLike]] + __format: Union[_CharLike_co, Tuple[_CharLike_co, _IntLike_co]] ) -> None: ... - def __add__(self, other: _TD64Like) -> datetime64: ... - def __radd__(self, other: _TD64Like) -> datetime64: ... + def __add__(self, other: _TD64Like_co) -> datetime64: ... + def __radd__(self, other: _TD64Like_co) -> datetime64: ... @overload def __sub__(self, other: datetime64) -> timedelta64: ... @overload - def __sub__(self, other: _TD64Like) -> datetime64: ... + def __sub__(self, other: _TD64Like_co) -> datetime64: ... def __rsub__(self, other: datetime64) -> timedelta64: ... __lt__: _ComparisonOp[datetime64] __le__: _ComparisonOp[datetime64] @@ -1658,61 +1667,61 @@ class datetime64(generic): # Support for `__index__` was added in python 3.8 (bpo-20092) if sys.version_info >= (3, 8): - _IntValue = Union[SupportsInt, _CharLike, SupportsIndex] - _FloatValue = Union[None, _CharLike, SupportsFloat, SupportsIndex] - _ComplexValue = Union[None, _CharLike, SupportsFloat, SupportsComplex, SupportsIndex] + _IntValue = Union[SupportsInt, _CharLike_co, SupportsIndex] + _FloatValue = Union[None, _CharLike_co, SupportsFloat, SupportsIndex] + _ComplexValue = Union[None, _CharLike_co, SupportsFloat, SupportsComplex, SupportsIndex] else: - _IntValue = Union[SupportsInt, _CharLike] - _FloatValue = Union[None, _CharLike, SupportsFloat] - _ComplexValue = Union[None, _CharLike, SupportsFloat, SupportsComplex] + _IntValue = Union[SupportsInt, _CharLike_co] + _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] - def __mod__(self, value: _IntLike) -> integer: ... - def __rmod__(self, value: _IntLike) -> integer: ... + __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: ... # Ensure that objects annotated as `integer` support bit-wise operations - def __lshift__(self, other: _IntLike) -> integer: ... - def __rlshift__(self, other: _IntLike) -> integer: ... - def __rshift__(self, other: _IntLike) -> integer: ... - def __rrshift__(self, other: _IntLike) -> integer: ... - def __and__(self, other: _IntLike) -> integer: ... - def __rand__(self, other: _IntLike) -> integer: ... - def __or__(self, other: _IntLike) -> integer: ... - def __ror__(self, other: _IntLike) -> integer: ... - def __xor__(self, other: _IntLike) -> integer: ... - def __rxor__(self, other: _IntLike) -> integer: ... - -class signedinteger(integer[_NBit_co]): + def __lshift__(self, other: _IntLike_co) -> integer: ... + def __rlshift__(self, other: _IntLike_co) -> integer: ... + def __rshift__(self, other: _IntLike_co) -> integer: ... + def __rrshift__(self, other: _IntLike_co) -> integer: ... + def __and__(self, other: _IntLike_co) -> integer: ... + def __rand__(self, other: _IntLike_co) -> integer: ... + def __or__(self, other: _IntLike_co) -> integer: ... + def __ror__(self, other: _IntLike_co) -> integer: ... + def __xor__(self, other: _IntLike_co) -> integer: ... + def __rxor__(self, other: _IntLike_co) -> integer: ... + +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] @@ -1730,8 +1739,8 @@ longlong = signedinteger[_NBitLongLong] class timedelta64(generic): def __init__( self, - __value: Union[None, int, _CharLike, dt.timedelta, timedelta64] = ..., - __format: Union[_CharLike, Tuple[_CharLike, _IntLike]] = ..., + __value: Union[None, int, _CharLike_co, dt.timedelta, timedelta64] = ..., + __format: Union[_CharLike_co, Tuple[_CharLike_co, _IntLike_co]] = ..., ) -> None: ... def __int__(self) -> int: ... def __float__(self) -> float: ... @@ -1739,12 +1748,12 @@ class timedelta64(generic): def __neg__(self: _ArraySelf) -> _ArraySelf: ... def __pos__(self: _ArraySelf) -> _ArraySelf: ... def __abs__(self: _ArraySelf) -> _ArraySelf: ... - def __add__(self, other: _TD64Like) -> timedelta64: ... - def __radd__(self, other: _TD64Like) -> timedelta64: ... - def __sub__(self, other: _TD64Like) -> timedelta64: ... - def __rsub__(self, other: _TD64Like) -> timedelta64: ... - def __mul__(self, other: _FloatLike) -> timedelta64: ... - def __rmul__(self, other: _FloatLike) -> timedelta64: ... + def __add__(self, other: _TD64Like_co) -> timedelta64: ... + def __radd__(self, other: _TD64Like_co) -> timedelta64: ... + def __sub__(self, other: _TD64Like_co) -> timedelta64: ... + def __rsub__(self, other: _TD64Like_co) -> timedelta64: ... + def __mul__(self, other: _FloatLike_co) -> timedelta64: ... + def __rmul__(self, other: _FloatLike_co) -> timedelta64: ... __truediv__: _TD64Div[float64] __floordiv__: _TD64Div[int64] def __rtruediv__(self, other: timedelta64) -> float64: ... @@ -1753,38 +1762,38 @@ class timedelta64(generic): def __rmod__(self, other: timedelta64) -> timedelta64: ... def __divmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ... def __rdivmod__(self, other: timedelta64) -> Tuple[int64, timedelta64]: ... - __lt__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]] - __le__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]] - __gt__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]] - __ge__: _ComparisonOp[Union[timedelta64, _IntLike, _BoolLike]] + __lt__: _ComparisonOp[Union[timedelta64, _IntLike_co, _BoolLike_co]] + __le__: _ComparisonOp[Union[timedelta64, _IntLike_co, _BoolLike_co]] + __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] @@ -1799,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] @@ -1838,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] @@ -1873,7 +1882,7 @@ longcomplex = complexfloating[_NBitLongDouble, _NBitLongDouble] class flexible(generic): ... # type: ignore class void(flexible): - def __init__(self, __value: Union[_IntLike, bytes]): ... + def __init__(self, __value: Union[_IntLike_co, bytes]): ... @property def real(self: _ArraySelf) -> _ArraySelf: ... @property diff --git a/numpy/core/arrayprint.pyi b/numpy/core/arrayprint.pyi index 6aaae0320..b2565b345 100644 --- a/numpy/core/arrayprint.pyi +++ b/numpy/core/arrayprint.pyi @@ -21,7 +21,7 @@ from numpy import ( longdouble, clongdouble, ) -from numpy.typing import ArrayLike, _CharLike, _FloatLike +from numpy.typing import ArrayLike, _CharLike_co, _FloatLike_co if sys.version_info > (3, 8): from typing import Literal, TypedDict @@ -40,13 +40,13 @@ class _FormatDict(TypedDict, total=False): complexfloat: Callable[[complexfloating[Any, Any]], str] longcomplexfloat: Callable[[clongdouble], str] void: Callable[[void], str] - numpystr: Callable[[_CharLike], str] + numpystr: Callable[[_CharLike_co], str] object: Callable[[object], str] all: Callable[[object], str] int_kind: Callable[[integer[Any]], str] float_kind: Callable[[floating[Any]], str] complex_kind: Callable[[complexfloating[Any, Any]], str] - str_kind: Callable[[_CharLike], str] + str_kind: Callable[[_CharLike_co], str] class _FormatOptions(TypedDict): precision: int @@ -96,7 +96,7 @@ def array2string( legacy: Optional[Literal[False, "1.13"]] = ..., ) -> str: ... def format_float_scientific( - x: _FloatLike, + x: _FloatLike_co, precision: Optional[int] = ..., unique: bool = ..., trim: Literal["k", ".", "0", "-"] = ..., @@ -105,7 +105,7 @@ def format_float_scientific( exp_digits: Optional[int] = ..., ) -> str: ... def format_float_positional( - x: _FloatLike, + x: _FloatLike_co, precision: Optional[int] = ..., unique: bool = ..., fractional: bool = ..., diff --git a/numpy/core/fromnumeric.pyi b/numpy/core/fromnumeric.pyi index 3b147e1d7..fc7f28a59 100644 --- a/numpy/core/fromnumeric.pyi +++ b/numpy/core/fromnumeric.pyi @@ -23,9 +23,8 @@ from numpy.typing import ( ArrayLike, _ShapeLike, _Shape, - _IntLike, - _BoolLike, - _NumberLike, + _IntLike_co, + _NumberLike_co, ) if sys.version_info >= (3, 8): @@ -98,7 +97,7 @@ def choose( ) -> _ScalarIntOrBool: ... @overload def choose( - a: Union[_IntLike, _BoolLike], choices: ArrayLike, out: Optional[ndarray] = ..., mode: _ModeKind = ... + a: _IntLike_co, choices: ArrayLike, out: Optional[ndarray] = ..., mode: _ModeKind = ... ) -> Union[integer, bool_]: ... @overload def choose( @@ -250,7 +249,7 @@ def sum( dtype: DTypeLike = ..., out: Optional[ndarray] = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> _Number: ... @overload @@ -260,7 +259,7 @@ def sum( dtype: DTypeLike = ..., out: Optional[ndarray] = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> Union[number, ndarray]: ... @overload @@ -324,7 +323,7 @@ def amax( axis: Optional[_ShapeLike] = ..., out: Optional[ndarray] = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> _Number: ... @overload @@ -333,7 +332,7 @@ def amax( axis: None = ..., out: Optional[ndarray] = ..., keepdims: Literal[False] = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> number: ... @overload @@ -342,7 +341,7 @@ def amax( axis: Optional[_ShapeLike] = ..., out: Optional[ndarray] = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> Union[number, ndarray]: ... @overload @@ -351,7 +350,7 @@ def amin( axis: Optional[_ShapeLike] = ..., out: Optional[ndarray] = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> _Number: ... @overload @@ -360,7 +359,7 @@ def amin( axis: None = ..., out: Optional[ndarray] = ..., keepdims: Literal[False] = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> number: ... @overload @@ -369,7 +368,7 @@ def amin( axis: Optional[_ShapeLike] = ..., out: Optional[ndarray] = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> Union[number, ndarray]: ... @@ -387,7 +386,7 @@ def prod( dtype: DTypeLike = ..., out: None = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> _Number: ... @overload @@ -397,7 +396,7 @@ def prod( dtype: DTypeLike = ..., out: None = ..., keepdims: Literal[False] = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> number: ... @overload @@ -407,7 +406,7 @@ def prod( dtype: DTypeLike = ..., out: Optional[ndarray] = ..., keepdims: bool = ..., - initial: _NumberLike = ..., + initial: _NumberLike_co = ..., where: _ArrayLikeBool = ..., ) -> Union[number, ndarray]: ... def cumprod( @@ -424,7 +423,7 @@ def around( ) -> _Number: ... @overload def around( - a: _NumberLike, decimals: int = ..., out: Optional[ndarray] = ... + a: _NumberLike_co, decimals: int = ..., out: Optional[ndarray] = ... ) -> number: ... @overload def around( diff --git a/numpy/core/function_base.pyi b/numpy/core/function_base.pyi index 1490bed4a..d353543f6 100644 --- a/numpy/core/function_base.pyi +++ b/numpy/core/function_base.pyi @@ -2,7 +2,7 @@ import sys from typing import overload, Tuple, Union, Sequence, Any from numpy import ndarray, inexact -from numpy.typing import ArrayLike, DTypeLike, _SupportsArray, _NumberLike +from numpy.typing import ArrayLike, DTypeLike, _SupportsArray, _NumberLike_co if sys.version_info >= (3, 8): from typing import SupportsIndex, Literal @@ -15,7 +15,7 @@ else: # TODO: wait for support for recursive types _ArrayLikeNested = Sequence[Sequence[Any]] _ArrayLikeNumber = Union[ - _NumberLike, Sequence[_NumberLike], ndarray, _SupportsArray, _ArrayLikeNested + _NumberLike_co, Sequence[_NumberLike_co], ndarray, _SupportsArray, _ArrayLikeNested ] @overload def linspace( diff --git a/numpy/f2py/cfuncs.py b/numpy/f2py/cfuncs.py index 26b43e7e6..40496ccf1 100644 --- a/numpy/f2py/cfuncs.py +++ b/numpy/f2py/cfuncs.py @@ -549,7 +549,12 @@ cppmacros["F2PY_THREAD_LOCAL_DECL"] = """\ #define F2PY_THREAD_LOCAL_DECL __declspec(thread) #elif defined(__STDC_VERSION__) \\ && (__STDC_VERSION__ >= 201112L) \\ - && !defined(__STDC_NO_THREADS__) + && !defined(__STDC_NO_THREADS__) \\ + && (!defined(__GLIBC__) || __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 12)) +/* __STDC_NO_THREADS__ was first defined in a maintenance release of glibc 2.12, + see https://lists.gnu.org/archive/html/commit-hurd/2012-07/msg00180.html, + so `!defined(__STDC_NO_THREADS__)` may give false positive for the existence + of `threads.h` when using an older release of glibc 2.12 */ #include <threads.h> #define F2PY_THREAD_LOCAL_DECL thread_local #elif defined(__GNUC__) \\ diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index e00bc4d98..7d652ce89 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -1092,7 +1092,7 @@ cdef class Generator: 0.0 # may vary >>> abs(sigma - np.std(s, ddof=1)) - 0.1 # may vary + 0.0 # may vary Display the histogram of the samples, along with the probability density function: diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py index 86bba57be..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() @@ -283,35 +285,35 @@ from ._char_codes import ( _ObjectCodes, ) from ._scalars import ( - _CharLike, - _BoolLike, - _UIntLike, - _IntLike, - _FloatLike, - _ComplexLike, - _TD64Like, - _NumberLike, - _ScalarLike, - _VoidLike, + _CharLike_co, + _BoolLike_co, + _UIntLike_co, + _IntLike_co, + _FloatLike_co, + _ComplexLike_co, + _TD64Like_co, + _NumberLike_co, + _ScalarLike_co, + _VoidLike_co, ) from ._shape import _Shape, _ShapeLike -from ._dtype_like import _SupportsDType, _VoidDTypeLike, DTypeLike +from ._dtype_like import _SupportsDType, _VoidDTypeLike, DTypeLike as DTypeLike from ._array_like import ( - ArrayLike, + ArrayLike as ArrayLike, _ArrayLike, _NestedSequence, _SupportsArray, - _ArrayLikeBool, - _ArrayLikeUInt, - _ArrayLikeInt, - _ArrayLikeFloat, - _ArrayLikeComplex, - _ArrayLikeTD64, - _ArrayLikeDT64, - _ArrayLikeObject, - _ArrayLikeVoid, - _ArrayLikeStr, - _ArrayLikeBytes, + _ArrayLikeBool_co, + _ArrayLikeUInt_co, + _ArrayLikeInt_co, + _ArrayLikeFloat_co, + _ArrayLikeComplex_co, + _ArrayLikeTD64_co, + _ArrayLikeDT64_co, + _ArrayLikeObject_co, + _ArrayLikeVoid_co, + _ArrayLikeStr_co, + _ArrayLikeBytes_co, ) if __doc__ is not None: diff --git a/numpy/typing/_array_like.py b/numpy/typing/_array_like.py index d6473442c..35413393c 100644 --- a/numpy/typing/_array_like.py +++ b/numpy/typing/_array_like.py @@ -78,41 +78,41 @@ ArrayLike = Union[ ], ] -# `ArrayLike<X>`: array-like objects that can be coerced into `X` +# `ArrayLike<X>_co`: array-like objects that can be coerced into `X` # given the casting rules `same_kind` -_ArrayLikeBool = _ArrayLike[ +_ArrayLikeBool_co = _ArrayLike[ "dtype[bool_]", bool, ] -_ArrayLikeUInt = _ArrayLike[ +_ArrayLikeUInt_co = _ArrayLike[ "dtype[Union[bool_, unsignedinteger[Any]]]", bool, ] -_ArrayLikeInt = _ArrayLike[ +_ArrayLikeInt_co = _ArrayLike[ "dtype[Union[bool_, integer[Any]]]", Union[bool, int], ] -_ArrayLikeFloat = _ArrayLike[ +_ArrayLikeFloat_co = _ArrayLike[ "dtype[Union[bool_, integer[Any], floating[Any]]]", Union[bool, int, float], ] -_ArrayLikeComplex = _ArrayLike[ +_ArrayLikeComplex_co = _ArrayLike[ "dtype[Union[bool_, integer[Any], floating[Any], complexfloating[Any, Any]]]", Union[bool, int, float, complex], ] -_ArrayLikeTD64 = _ArrayLike[ +_ArrayLikeTD64_co = _ArrayLike[ "dtype[Union[bool_, integer[Any], timedelta64]]", Union[bool, int], ] -_ArrayLikeDT64 = _NestedSequence[_SupportsArray["dtype[datetime64]"]] -_ArrayLikeObject = _NestedSequence[_SupportsArray["dtype[object_]"]] +_ArrayLikeDT64_co = _NestedSequence[_SupportsArray["dtype[datetime64]"]] +_ArrayLikeObject_co = _NestedSequence[_SupportsArray["dtype[object_]"]] -_ArrayLikeVoid = _NestedSequence[_SupportsArray["dtype[void]"]] -_ArrayLikeStr = _ArrayLike[ +_ArrayLikeVoid_co = _NestedSequence[_SupportsArray["dtype[void]"]] +_ArrayLikeStr_co = _ArrayLike[ "dtype[str_]", str, ] -_ArrayLikeBytes = _ArrayLike[ +_ArrayLikeBytes_co = _ArrayLike[ "dtype[bytes_]", bytes, ] diff --git a/numpy/typing/_callable.py b/numpy/typing/_callable.py index 8f464cc75..e1fdee3ba 100644 --- a/numpy/typing/_callable.py +++ b/numpy/typing/_callable.py @@ -37,11 +37,11 @@ from numpy import ( ) from ._nbit import _NBitInt from ._scalars import ( - _BoolLike, - _IntLike, - _FloatLike, - _ComplexLike, - _NumberLike, + _BoolLike_co, + _IntLike_co, + _FloatLike_co, + _ComplexLike_co, + _NumberLike_co, ) from . import NBitBase from ._array_like import ArrayLike @@ -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) @@ -72,7 +72,7 @@ if TYPE_CHECKING or HAVE_PROTOCOL: class _BoolOp(Protocol[_GenericType_co]): @overload - def __call__(self, __other: _BoolLike) -> _GenericType_co: ... + def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ... @overload # platform dependent def __call__(self, __other: int) -> int_: ... @overload @@ -84,7 +84,7 @@ if TYPE_CHECKING or HAVE_PROTOCOL: class _BoolBitOp(Protocol[_GenericType_co]): @overload - def __call__(self, __other: _BoolLike) -> _GenericType_co: ... + def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ... @overload # platform dependent def __call__(self, __other: int) -> int_: ... @overload @@ -105,7 +105,7 @@ if TYPE_CHECKING or HAVE_PROTOCOL: class _BoolTrueDiv(Protocol): @overload - def __call__(self, __other: Union[float, _IntLike]) -> float64: ... + def __call__(self, __other: Union[float, _IntLike_co]) -> float64: ... @overload def __call__(self, __other: complex) -> complex128: ... @overload @@ -113,7 +113,7 @@ if TYPE_CHECKING or HAVE_PROTOCOL: class _BoolMod(Protocol): @overload - def __call__(self, __other: _BoolLike) -> int8: ... + def __call__(self, __other: _BoolLike_co) -> int8: ... @overload # platform dependent def __call__(self, __other: int) -> int_: ... @overload @@ -125,7 +125,7 @@ if TYPE_CHECKING or HAVE_PROTOCOL: class _BoolDivMod(Protocol): @overload - def __call__(self, __other: _BoolLike) -> _2Tuple[int8]: ... + def __call__(self, __other: _BoolLike_co) -> _2Tuple[int8]: ... @overload # platform dependent def __call__(self, __other: int) -> _2Tuple[int_]: ... @overload @@ -139,11 +139,11 @@ if TYPE_CHECKING or HAVE_PROTOCOL: @overload def __call__(self, __other: timedelta64) -> _NumberType_co: ... @overload - def __call__(self, __other: _FloatLike) -> timedelta64: ... + 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) -> number: ... + def __call__(self, __other: _NumberLike_co) -> Any: ... class _ComparisonOp(Protocol[_T]): @overload diff --git a/numpy/typing/_scalars.py b/numpy/typing/_scalars.py index 90b2eff7b..516b996dc 100644 --- a/numpy/typing/_scalars.py +++ b/numpy/typing/_scalars.py @@ -2,22 +2,22 @@ from typing import Union, Tuple, Any import numpy as np -# NOTE: `_StrLike` and `_BytesLike` are pointless, as `np.str_` and `np.bytes_` -# are already subclasses of their builtin counterpart +# NOTE: `_StrLike_co` and `_BytesLike_co` are pointless, as `np.str_` and +# `np.bytes_` are already subclasses of their builtin counterpart -_CharLike = Union[str, bytes] +_CharLike_co = Union[str, bytes] -# The 6 `<X>Like` type-aliases below represent all scalars that can be +# The 6 `<X>Like_co` type-aliases below represent all scalars that can be # coerced into `<X>` (with the casting rule `same_kind`) -_BoolLike = Union[bool, np.bool_] -_UIntLike = Union[_BoolLike, np.unsignedinteger] -_IntLike = Union[_BoolLike, int, np.integer] -_FloatLike = Union[_IntLike, float, np.floating] -_ComplexLike = Union[_FloatLike, complex, np.complexfloating] -_TD64Like = Union[_IntLike, np.timedelta64] +_BoolLike_co = Union[bool, np.bool_] +_UIntLike_co = Union[_BoolLike_co, np.unsignedinteger] +_IntLike_co = Union[_BoolLike_co, int, np.integer] +_FloatLike_co = Union[_IntLike_co, float, np.floating] +_ComplexLike_co = Union[_FloatLike_co, complex, np.complexfloating] +_TD64Like_co = Union[_IntLike_co, np.timedelta64] -_NumberLike = Union[int, float, complex, np.number, np.bool_] -_ScalarLike = Union[ +_NumberLike_co = Union[int, float, complex, np.number, np.bool_] +_ScalarLike_co = Union[ int, float, complex, @@ -26,5 +26,5 @@ _ScalarLike = Union[ np.generic, ] -# `_VoidLike` is technically not a scalar, but it's close enough -_VoidLike = Union[Tuple[Any, ...], np.void] +# `_VoidLike_co` is technically not a scalar, but it's close enough +_VoidLike_co = Union[Tuple[Any, ...], np.void] 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 diff --git a/test_requirements.txt b/test_requirements.txt index b6aaa8995..52109a5d5 100644 --- a/test_requirements.txt +++ b/test_requirements.txt @@ -1,7 +1,7 @@ cython==0.29.21 wheel<0.36.3 setuptools<49.2.0 -hypothesis==6.0.0 +hypothesis==6.0.2 pytest==6.2.1 pytz==2020.5 pytest-cov==2.10.1 |