Skip to content

Commit c3fbf03

Browse files
authored
Merge pull request #28940 from jorenham/typing/optional-ndarray-typars
TYP: optional type parameters for ``ndarray`` and ``flatiter``
2 parents 1b8a42a + ede9ea4 commit c3fbf03

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
* Static type-checkers now interpret:
2+
3+
- ``_: np.ndarray`` as ``_: npt.NDArray[typing.Any]``.
4+
- ``_: np.flatiter`` as ``_: np.flatiter[np.ndarray]``.
5+
6+
This is because their type parameters now have default values.

numpy/__init__.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -784,19 +784,19 @@ _ImagT_co = TypeVar("_ImagT_co", covariant=True)
784784
_CallableT = TypeVar("_CallableT", bound=Callable[..., object])
785785

786786
_DTypeT = TypeVar("_DTypeT", bound=dtype)
787-
_DTypeT_co = TypeVar("_DTypeT_co", bound=dtype, covariant=True)
787+
_DTypeT_co = TypeVar("_DTypeT_co", bound=dtype, default=dtype, covariant=True)
788788
_FlexDTypeT = TypeVar("_FlexDTypeT", bound=dtype[flexible])
789789

790-
_ArrayT = TypeVar("_ArrayT", bound=NDArray[Any])
791-
_ArrayT_co = TypeVar("_ArrayT_co", bound=NDArray[Any], covariant=True)
790+
_ArrayT = TypeVar("_ArrayT", bound=ndarray)
791+
_ArrayT_co = TypeVar("_ArrayT_co", bound=ndarray, default=ndarray, covariant=True)
792792
_IntegralArrayT = TypeVar("_IntegralArrayT", bound=NDArray[integer | np.bool | object_])
793793
_RealArrayT = TypeVar("_RealArrayT", bound=NDArray[floating | integer | timedelta64 | np.bool | object_])
794794
_NumericArrayT = TypeVar("_NumericArrayT", bound=NDArray[number | timedelta64 | object_])
795795

796796
_ShapeT = TypeVar("_ShapeT", bound=_Shape)
797-
_ShapeT_co = TypeVar("_ShapeT_co", bound=_Shape, covariant=True)
797+
_ShapeT_co = TypeVar("_ShapeT_co", bound=_Shape, default=_Shape, covariant=True)
798798
_1DShapeT = TypeVar("_1DShapeT", bound=_1D)
799-
_2DShapeT_co = TypeVar("_2DShapeT_co", bound=_2D, covariant=True)
799+
_2DShapeT_co = TypeVar("_2DShapeT_co", bound=_2D, default=_2D, covariant=True)
800800
_1NShapeT = TypeVar("_1NShapeT", bound=tuple[L[1], *tuple[L[1], ...]]) # (1,) | (1, 1) | (1, 1, 1) | ...
801801

802802
_ScalarT = TypeVar("_ScalarT", bound=generic)
@@ -807,9 +807,9 @@ _FloatingT_co = TypeVar("_FloatingT_co", bound=floating, default=floating, covar
807807
_IntegerT = TypeVar("_IntegerT", bound=integer)
808808
_IntegerT_co = TypeVar("_IntegerT_co", bound=integer, default=integer, covariant=True)
809809

810-
_NBit = TypeVar("_NBit", bound=NBitBase, default=Any)
811-
_NBit1 = TypeVar("_NBit1", bound=NBitBase, default=Any)
812-
_NBit2 = TypeVar("_NBit2", bound=NBitBase, default=_NBit1)
810+
_NBit = TypeVar("_NBit", bound=NBitBase, default=Any) # pyright: ignore[reportDeprecated]
811+
_NBit1 = TypeVar("_NBit1", bound=NBitBase, default=Any) # pyright: ignore[reportDeprecated]
812+
_NBit2 = TypeVar("_NBit2", bound=NBitBase, default=_NBit1) # pyright: ignore[reportDeprecated]
813813

814814
_ItemT_co = TypeVar("_ItemT_co", default=Any, covariant=True)
815815
_BoolItemT = TypeVar("_BoolItemT", bound=builtins.bool)

numpy/_core/defchararray.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ from typing import (
22
Literal as L,
33
overload,
44
TypeAlias,
5-
TypeVar,
65
Any,
76
SupportsIndex,
87
SupportsInt,
98
)
9+
from typing_extensions import TypeVar
1010

1111
import numpy as np
1212
from numpy import (
@@ -89,10 +89,10 @@ __all__ = [
8989
"chararray",
9090
]
9191

92-
_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], covariant=True)
92+
_ShapeT_co = TypeVar("_ShapeT_co", bound=_Shape, default=_Shape, covariant=True)
9393
_CharacterT = TypeVar("_CharacterT", bound=np.character)
94-
_CharDTypeT_co = TypeVar("_CharDTypeT_co", bound=dtype[np.character], covariant=True)
95-
_CharArray: TypeAlias = chararray[tuple[int, ...], dtype[_CharacterT]]
94+
_CharDTypeT_co = TypeVar("_CharDTypeT_co", bound=dtype[np.character], default=dtype, covariant=True)
95+
_CharArray: TypeAlias = chararray[_Shape, dtype[_CharacterT]]
9696

9797
_StringDTypeArray: TypeAlias = np.ndarray[_Shape, np.dtypes.StringDType]
9898
_StringDTypeSupportsArray: TypeAlias = _SupportsArray[np.dtypes.StringDType]

numpy/ma/core.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# ruff: noqa: ANN001, ANN002, ANN003, ANN201, ANN202 ANN204, ANN401
33

44
from collections.abc import Sequence
5-
from typing import Any, Literal, Self, SupportsIndex, TypeAlias, TypeVar, overload
5+
from typing import Any, Literal, Self, SupportsIndex, TypeAlias, overload
66

77
from _typeshed import Incomplete
8-
from typing_extensions import TypeIs, deprecated
8+
from typing_extensions import TypeIs, TypeVar, deprecated
99

1010
import numpy as np
1111
from numpy import (
@@ -221,10 +221,10 @@ __all__ = [
221221
"zeros_like",
222222
]
223223

224-
_ShapeT = TypeVar("_ShapeT", bound=tuple[int, ...])
225-
_ShapeT_co = TypeVar("_ShapeT_co", bound=tuple[int, ...], covariant=True)
224+
_ShapeT = TypeVar("_ShapeT", bound=_Shape)
225+
_ShapeT_co = TypeVar("_ShapeT_co", bound=_Shape, default=_Shape, covariant=True)
226226
_DTypeT = TypeVar("_DTypeT", bound=dtype)
227-
_DTypeT_co = TypeVar("_DTypeT_co", bound=dtype, covariant=True)
227+
_DTypeT_co = TypeVar("_DTypeT_co", bound=dtype, default=dtype, covariant=True)
228228
_ArrayT = TypeVar("_ArrayT", bound=ndarray[Any, Any])
229229
_ScalarT = TypeVar("_ScalarT", bound=generic)
230230
_ScalarT_co = TypeVar("_ScalarT_co", bound=generic, covariant=True)

numpy/typing/tests/data/pass/literal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
CF = frozenset({None, "C", "F"})
1818

1919
order_list: list[tuple[frozenset[str | None], Callable[..., Any]]] = [
20-
(KACF, partial(np.ndarray, 1)),
2120
(KACF, AR.tobytes),
2221
(KACF, partial(AR.astype, int)),
2322
(KACF, AR.copy),
2423
(ACF, partial(AR.reshape, 1)),
2524
(KACF, AR.flatten),
2625
(KACF, AR.ravel),
2726
(KACF, partial(np.array, 1)),
28-
# NOTE: __call__ is needed due to mypy 1.11 bugs (#17620, #17631)
27+
# NOTE: __call__ is needed due to mypy bugs (#17620, #17631)
28+
(KACF, partial(np.ndarray.__call__, 1)),
2929
(CF, partial(np.zeros.__call__, 1)),
3030
(CF, partial(np.ones.__call__, 1)),
3131
(CF, partial(np.empty.__call__, 1)),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy