|
1 | 1 | import builtins
|
2 | 2 | import sys
|
3 |
| -import os |
4 | 3 | import mmap
|
5 | 4 | import ctypes as ct
|
6 | 5 | import array as _array
|
@@ -208,7 +207,7 @@ from typing import (
|
208 | 207 | # library include `typing_extensions` stubs:
|
209 | 208 | # https://github.com/python/typeshed/blob/main/stdlib/typing_extensions.pyi
|
210 | 209 | from _typeshed import StrOrBytesPath, SupportsFlush, SupportsLenAndGetItem, SupportsWrite
|
211 |
| -from typing_extensions import CapsuleType, Generic, LiteralString, Protocol, Self, TypeVar, Unpack, deprecated, overload |
| 210 | +from typing_extensions import CapsuleType, Generic, LiteralString, Never, Protocol, Self, TypeVar, Unpack, deprecated, overload |
212 | 211 |
|
213 | 212 | from numpy import (
|
214 | 213 | core,
|
@@ -1755,12 +1754,25 @@ _IntegralArrayT = TypeVar("_IntegralArrayT", bound=NDArray[integer[Any] | np.boo
|
1755 | 1754 | _RealArrayT = TypeVar("_RealArrayT", bound=NDArray[floating[Any] | integer[Any] | timedelta64 | np.bool | object_])
|
1756 | 1755 | _NumericArrayT = TypeVar("_NumericArrayT", bound=NDArray[number[Any] | timedelta64 | object_])
|
1757 | 1756 |
|
1758 |
| -_Shape1D: TypeAlias = tuple[int] |
1759 |
| -_Shape2D: TypeAlias = tuple[int, int] |
1760 |
| - |
| 1757 | +_AnyShapeType = TypeVar( |
| 1758 | + "_AnyShapeType", |
| 1759 | + tuple[()], # 0-d |
| 1760 | + tuple[int], # 1-d |
| 1761 | + tuple[int, int], # 2-d |
| 1762 | + tuple[int, int, int], # 3-d |
| 1763 | + tuple[int, int, int, int], # 4-d |
| 1764 | + tuple[int, int, int, int, int], # 5-d |
| 1765 | + tuple[int, int, int, int, int, int], # 6-d |
| 1766 | + tuple[int, int, int, int, int, int, int], # 7-d |
| 1767 | + tuple[int, int, int, int, int, int, int, int], # 8-d |
| 1768 | + tuple[int, ...], # N-d |
| 1769 | +) |
1761 | 1770 | _ShapeType = TypeVar("_ShapeType", bound=_Shape)
|
1762 | 1771 | _ShapeType_co = TypeVar("_ShapeType_co", covariant=True, bound=_Shape)
|
| 1772 | +_Shape2D: TypeAlias = tuple[int, int] |
1763 | 1773 | _Shape2DType_co = TypeVar("_Shape2DType_co", covariant=True, bound=_Shape2D)
|
| 1774 | +_Shape1NType = TypeVar("_Shape1NType", bound=tuple[L[1], Unpack[tuple[L[1], ...]]]) # (1,) | (1, 1) | (1, 1, 1) | ... |
| 1775 | + |
1764 | 1776 | _NumberType = TypeVar("_NumberType", bound=number[Any])
|
1765 | 1777 |
|
1766 | 1778 |
|
@@ -2194,21 +2206,86 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
|
2194 | 2206 | def flatten(self, /, order: _OrderKACF = "C") -> ndarray[tuple[int], _DType_co]: ...
|
2195 | 2207 | def ravel(self, /, order: _OrderKACF = "C") -> ndarray[tuple[int], _DType_co]: ...
|
2196 | 2208 |
|
2197 |
| - @overload |
| 2209 | + # NOTE: reshape also accepts negative integers, so we can't use integer literals |
| 2210 | + @overload # (None) |
| 2211 | + def reshape(self, shape: None, /, *, order: _OrderACF = "C", copy: builtins.bool | None = None) -> Self: ... |
| 2212 | + @overload # (empty_sequence) |
| 2213 | + def reshape( # type: ignore[overload-overlap] # mypy false positive |
| 2214 | + self, |
| 2215 | + shape: Sequence[Never], |
| 2216 | + /, |
| 2217 | + *, |
| 2218 | + order: _OrderACF = "C", |
| 2219 | + copy: builtins.bool | None = None, |
| 2220 | + ) -> ndarray[tuple[()], _DType_co]: ... |
| 2221 | + @overload # (() | (int) | (int, int) | ....) # up to 8-d |
2198 | 2222 | def reshape(
|
2199 | 2223 | self,
|
2200 |
| - shape: _ShapeLike, |
| 2224 | + shape: _AnyShapeType, |
2201 | 2225 | /,
|
2202 | 2226 | *,
|
2203 |
| - order: _OrderACF = ..., |
2204 |
| - copy: None | builtins.bool = ..., |
2205 |
| - ) -> ndarray[_Shape, _DType_co]: ... |
2206 |
| - @overload |
| 2227 | + order: _OrderACF = "C", |
| 2228 | + copy: builtins.bool | None = None, |
| 2229 | + ) -> ndarray[_AnyShapeType, _DType_co]: ... |
| 2230 | + @overload # (index) |
| 2231 | + def reshape( |
| 2232 | + self, |
| 2233 | + size1: SupportsIndex, |
| 2234 | + /, |
| 2235 | + *, |
| 2236 | + order: _OrderACF = "C", |
| 2237 | + copy: builtins.bool | None = None, |
| 2238 | + ) -> ndarray[tuple[int], _DType_co]: ... |
| 2239 | + @overload # (index, index) |
| 2240 | + def reshape( |
| 2241 | + self, |
| 2242 | + size1: SupportsIndex, |
| 2243 | + size2: SupportsIndex, |
| 2244 | + /, |
| 2245 | + *, |
| 2246 | + order: _OrderACF = "C", |
| 2247 | + copy: builtins.bool | None = None, |
| 2248 | + ) -> ndarray[tuple[int, int], _DType_co]: ... |
| 2249 | + @overload # (index, index, index) |
2207 | 2250 | def reshape(
|
2208 | 2251 | self,
|
| 2252 | + size1: SupportsIndex, |
| 2253 | + size2: SupportsIndex, |
| 2254 | + size3: SupportsIndex, |
| 2255 | + /, |
| 2256 | + *, |
| 2257 | + order: _OrderACF = "C", |
| 2258 | + copy: builtins.bool | None = None, |
| 2259 | + ) -> ndarray[tuple[int, int, int], _DType_co]: ... |
| 2260 | + @overload # (index, index, index, index) |
| 2261 | + def reshape( |
| 2262 | + self, |
| 2263 | + size1: SupportsIndex, |
| 2264 | + size2: SupportsIndex, |
| 2265 | + size3: SupportsIndex, |
| 2266 | + size4: SupportsIndex, |
| 2267 | + /, |
| 2268 | + *, |
| 2269 | + order: _OrderACF = "C", |
| 2270 | + copy: builtins.bool | None = None, |
| 2271 | + ) -> ndarray[tuple[int, int, int, int], _DType_co]: ... |
| 2272 | + @overload # (int, *(index, ...)) |
| 2273 | + def reshape( |
| 2274 | + self, |
| 2275 | + size0: SupportsIndex, |
| 2276 | + /, |
2209 | 2277 | *shape: SupportsIndex,
|
2210 |
| - order: _OrderACF = ..., |
2211 |
| - copy: None | builtins.bool = ..., |
| 2278 | + order: _OrderACF = "C", |
| 2279 | + copy: builtins.bool | None = None, |
| 2280 | + ) -> ndarray[_Shape, _DType_co]: ... |
| 2281 | + @overload # (sequence[index]) |
| 2282 | + def reshape( |
| 2283 | + self, |
| 2284 | + shape: Sequence[SupportsIndex], |
| 2285 | + /, |
| 2286 | + *, |
| 2287 | + order: _OrderACF = "C", |
| 2288 | + copy: builtins.bool | None = None, |
2212 | 2289 | ) -> ndarray[_Shape, _DType_co]: ...
|
2213 | 2290 |
|
2214 | 2291 | @overload
|
@@ -3110,10 +3187,88 @@ class generic(_ArrayOrScalarCommon):
|
3110 | 3187 | def flatten(self, /, order: _OrderKACF = "C") -> ndarray[tuple[int], dtype[Self]]: ...
|
3111 | 3188 | def ravel(self, /, order: _OrderKACF = "C") -> ndarray[tuple[int], dtype[Self]]: ...
|
3112 | 3189 |
|
3113 |
| - @overload |
3114 |
| - def reshape(self, shape: _ShapeLike, /, *, order: _OrderACF = ...) -> NDArray[Self]: ... |
3115 |
| - @overload |
3116 |
| - def reshape(self, *shape: SupportsIndex, order: _OrderACF = ...) -> NDArray[Self]: ... |
| 3190 | + @overload # (() | []) |
| 3191 | + def reshape( |
| 3192 | + self, |
| 3193 | + shape: tuple[()] | list[Never], |
| 3194 | + /, |
| 3195 | + *, |
| 3196 | + order: _OrderACF = "C", |
| 3197 | + copy: builtins.bool | None = None, |
| 3198 | + ) -> Self: ... |
| 3199 | + @overload # ((1, *(1, ...))@_ShapeType) |
| 3200 | + def reshape( |
| 3201 | + self, |
| 3202 | + shape: _Shape1NType, |
| 3203 | + /, |
| 3204 | + *, |
| 3205 | + order: _OrderACF = "C", |
| 3206 | + copy: builtins.bool | None = None, |
| 3207 | + ) -> ndarray[_Shape1NType, dtype[Self]]: ... |
| 3208 | + @overload # (Sequence[index, ...]) # not recommended |
| 3209 | + def reshape( |
| 3210 | + self, |
| 3211 | + shape: Sequence[SupportsIndex], |
| 3212 | + /, |
| 3213 | + *, |
| 3214 | + order: _OrderACF = "C", |
| 3215 | + copy: builtins.bool | None = None, |
| 3216 | + ) -> Self | ndarray[tuple[L[1], ...], dtype[Self]]: ... |
| 3217 | + @overload # _(index) |
| 3218 | + def reshape( |
| 3219 | + self, |
| 3220 | + size1: SupportsIndex, |
| 3221 | + /, |
| 3222 | + *, |
| 3223 | + order: _OrderACF = "C", |
| 3224 | + copy: builtins.bool | None = None, |
| 3225 | + ) -> ndarray[tuple[L[1]], dtype[Self]]: ... |
| 3226 | + @overload # _(index, index) |
| 3227 | + def reshape( |
| 3228 | + self, |
| 3229 | + size1: SupportsIndex, |
| 3230 | + size2: SupportsIndex, |
| 3231 | + /, |
| 3232 | + *, |
| 3233 | + order: _OrderACF = "C", |
| 3234 | + copy: builtins.bool | None = None, |
| 3235 | + ) -> ndarray[tuple[L[1], L[1]], dtype[Self]]: ... |
| 3236 | + @overload # _(index, index, index) |
| 3237 | + def reshape( |
| 3238 | + self, |
| 3239 | + size1: SupportsIndex, |
| 3240 | + size2: SupportsIndex, |
| 3241 | + size3: SupportsIndex, |
| 3242 | + /, |
| 3243 | + *, |
| 3244 | + order: _OrderACF = "C", |
| 3245 | + copy: builtins.bool | None = None, |
| 3246 | + ) -> ndarray[tuple[L[1], L[1], L[1]], dtype[Self]]: ... |
| 3247 | + @overload # _(index, index, index, index) |
| 3248 | + def reshape( |
| 3249 | + self, |
| 3250 | + size1: SupportsIndex, |
| 3251 | + size2: SupportsIndex, |
| 3252 | + size3: SupportsIndex, |
| 3253 | + size4: SupportsIndex, |
| 3254 | + /, |
| 3255 | + *, |
| 3256 | + order: _OrderACF = "C", |
| 3257 | + copy: builtins.bool | None = None, |
| 3258 | + ) -> ndarray[tuple[L[1], L[1], L[1], L[1]], dtype[Self]]: ... |
| 3259 | + @overload # _(index, index, index, index, index, *index) # ndim >= 5 |
| 3260 | + def reshape( |
| 3261 | + self, |
| 3262 | + size1: SupportsIndex, |
| 3263 | + size2: SupportsIndex, |
| 3264 | + size3: SupportsIndex, |
| 3265 | + size4: SupportsIndex, |
| 3266 | + size5: SupportsIndex, |
| 3267 | + /, |
| 3268 | + *sizes6_: SupportsIndex, |
| 3269 | + order: _OrderACF = "C", |
| 3270 | + copy: builtins.bool | None = None, |
| 3271 | + ) -> ndarray[tuple[L[1], L[1], L[1], L[1], L[1], Unpack[tuple[L[1], ...]]], dtype[Self]]: ... |
3117 | 3272 |
|
3118 | 3273 | def squeeze(self, axis: None | L[0] | tuple[()] = ...) -> Self: ...
|
3119 | 3274 | def transpose(self, axes: None | tuple[()] = ..., /) -> Self: ...
|
|
0 commit comments