FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

TYP: fix ``np.shape`` assignability issue for python lists by jorenham · Pull Request #31171 · numpy/numpy · GitHub

/ numpy Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .pyi  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 9 additions & 8 deletions numpy/_core/fromnumeric.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -741,25 +741,26 @@ def nonzero(a: _ToArray3D[Any]) -> tuple[_Array1D[np.intp], _Array1D[np.intp], _
@overload # Nd (fallback)
def nonzero(a: _ArrayLike[Any]) -> tuple[_Array1D[np.intp], ...]: ...

# this prevents `Any` from being returned with Pyright
@overload
# `collections.abc.Sequence` can't be used here because `bytes` and `str` are
# subtypes of it, which would make the return types incompatible.
@overload # this prevents `Any` from being returned with Pyright
def shape(a: _SupportsShape[Never]) -> _AnyShape: ...
@overload
def shape[ShapeT: _Shape](a: _SupportsShape[ShapeT]) -> ShapeT: ...
@overload
def shape(a: _PyScalar) -> tuple[()]: ...
# `collections.abc.Sequence` can't be used hesre, since `bytes` and `str` are
# subtypes of it, which would make the return types incompatible.
@overload # an unbound type variable is used because `list` is invariant
def shape[ScalarT: _PyScalar](a: _PyArray[ScalarT]) -> _1D: ...
@overload
def shape(a: _PyArray[_PyScalar]) -> _1D: ...
@overload
def shape(a: _PyArray[_PyArray[_PyScalar]]) -> _2D: ...
# this overload will be skipped by typecheckers that don't support PEP 688
def shape[ScalarT: _PyScalar](a: Sequence[_PyArray[ScalarT]]) -> _2D: ...
@overload
def shape[ScalarT: _PyScalar](a: Sequence[Sequence[_PyArray[ScalarT]]]) -> _3D: ...
@overload # this will be skipped by typecheckers that don't support PEP 688
def shape(a: memoryview | bytearray) -> _1D: ...
@overload
def shape(a: ArrayLike) -> _AnyShape: ...

#
@overload
def compress[ScalarT: np.generic](
condition: _ArrayLikeBool_co, # 1D bool array
Expand Down
9 changes: 8 additions & 1 deletion numpy/typing/tests/data/reveal/fromnumeric.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ f4: np.float32
i8: np.int64
f: float

_py_list_1d: list[int]
_py_list_2d: list[list[int]]
_py_list_3d: list[list[list[int]]]

_dtype_list: list[np.dtype]
_any_list: list[Any]

Expand Down Expand Up @@ -184,7 +188,10 @@ assert_type(np.shape(b), tuple[()])
assert_type(np.shape(f), tuple[()])
assert_type(np.shape([1]), tuple[int])
assert_type(np.shape([[2]]), tuple[int, int])
assert_type(np.shape([[[3]]]), tuple[Any, ...])
assert_type(np.shape([[[3]]]), tuple[int, int, int])
assert_type(np.shape(_py_list_1d), tuple[int])
assert_type(np.shape(_py_list_2d), tuple[int, int])
assert_type(np.shape(_py_list_3d), tuple[int, int, int])
assert_type(np.shape(AR_b), tuple[Any, ...])
assert_type(np.shape(AR_nd), tuple[Any, ...])
# these fail on mypy, but it works as expected with pyright/pylance
Expand Down
Loading

Back | FazBrowse Home | New Git URL