Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

TYP: fix ndarray.tolist() and .item() for unknown dtype #28650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 14 additions & 41 deletions 55 numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1093,35 +1093,11 @@ class _SupportsItem(Protocol[_T_co]):
class _SupportsDLPack(Protocol[_T_contra]):
def __dlpack__(self, /, *, stream: _T_contra | None = None) -> CapsuleType: ...

@type_check_only
class _HasShape(Protocol[_ShapeT_co]):
@property
def shape(self, /) -> _ShapeT_co: ...

@type_check_only
class _HasDType(Protocol[_T_co]):
@property
def dtype(self, /) -> _T_co: ...

@type_check_only
class _HasShapeAndSupportsItem(_HasShape[_ShapeT_co], _SupportsItem[_T_co], Protocol[_ShapeT_co, _T_co]): ...

# matches any `x` on `x.type.item() -> _T_co`, e.g. `dtype[np.int8]` gives `_T_co: int`
@type_check_only
class _HasTypeWithItem(Protocol[_T_co]):
@property
def type(self, /) -> type[_SupportsItem[_T_co]]: ...

# matches any `x` on `x.shape: _ShapeT_co` and `x.dtype.type.item() -> _T_co`,
# useful for capturing the item-type (`_T_co`) of the scalar-type of an array with
# specific shape (`_ShapeT_co`).
@type_check_only
class _HasShapeAndDTypeWithItem(Protocol[_ShapeT_co, _T_co]):
@property
def shape(self, /) -> _ShapeT_co: ...
@property
def dtype(self, /) -> _HasTypeWithItem[_T_co]: ...

@type_check_only
class _HasRealAndImag(Protocol[_RealT_co, _ImagT_co]):
@property
Expand Down Expand Up @@ -2204,29 +2180,26 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DType_co]):
@property
def flat(self) -> flatiter[Self]: ...

@overload # special casing for `StringDType`, which has no scalar type
def item(self: ndarray[Any, dtypes.StringDType], /) -> str: ...
@overload
def item(self: ndarray[Any, dtypes.StringDType], arg0: SupportsIndex | tuple[SupportsIndex, ...] = ..., /) -> str: ...
@overload
def item(self: ndarray[Any, dtypes.StringDType], /, *args: SupportsIndex) -> str: ...
@overload # use the same output type as that of the underlying `generic`
def item(self: _HasShapeAndDTypeWithItem[Any, _T], /) -> _T: ...
@overload
def item(self: _HasShapeAndDTypeWithItem[Any, _T], arg0: SupportsIndex | tuple[SupportsIndex, ...] = ..., /) -> _T: ...
@overload
def item(self: _HasShapeAndDTypeWithItem[Any, _T], /, *args: SupportsIndex) -> _T: ...
def item(self: NDArray[generic[_T]], i0: SupportsIndex | tuple[SupportsIndex, ...] = ..., /, *args: SupportsIndex) -> _T: ...
@overload # special casing for `StringDType`, which has no scalar type
def item(
self: ndarray[Any, dtypes.StringDType],
arg0: SupportsIndex | tuple[SupportsIndex, ...] = ...,
/,
*args: SupportsIndex,
) -> str: ...

@overload
def tolist(self: _HasShapeAndSupportsItem[tuple[()], _T], /) -> _T: ...
def tolist(self: ndarray[tuple[()], dtype[generic[_T]]], /) -> _T: ...
@overload
def tolist(self: _HasShapeAndSupportsItem[tuple[int], _T], /) -> list[_T]: ...
def tolist(self: ndarray[tuple[int], dtype[generic[_T]]], /) -> list[_T]: ...
@overload
def tolist(self: _HasShapeAndSupportsItem[tuple[int, int], _T], /) -> list[list[_T]]: ...
def tolist(self: ndarray[tuple[int, int], dtype[generic[_T]]], /) -> list[list[_T]]: ...
@overload
def tolist(self: _HasShapeAndSupportsItem[tuple[int, int, int], _T], /) -> list[list[list[_T]]]: ...
def tolist(self: ndarray[tuple[int, int, int], dtype[generic[_T]]], /) -> list[list[list[_T]]]: ...
@overload
def tolist(self: _HasShapeAndSupportsItem[Any, _T], /) -> _T | list[_T] | list[list[_T]] | list[list[list[Any]]]: ...
def tolist(self, /) -> Any: ...

@overload
def resize(self, new_shape: _ShapeLike, /, *, refcheck: builtins.bool = ...) -> None: ...
Expand Down Expand Up @@ -5379,7 +5352,7 @@ class matrix(ndarray[_2DShapeT_co, _DType_co]):
def ptp(self, axis: None | _ShapeLike = ..., out: _ArrayT = ...) -> _ArrayT: ...

def squeeze(self, axis: None | _ShapeLike = ...) -> matrix[_2D, _DType_co]: ...
def tolist(self: _SupportsItem[_T]) -> list[list[_T]]: ...
def tolist(self: matrix[Any, dtype[generic[_T]]]) -> list[list[_T]]: ... # pyright: ignore[reportIncompatibleMethodOverride]
def ravel(self, /, order: _OrderKACF = "C") -> matrix[tuple[L[1], int], _DType_co]: ... # pyright: ignore[reportIncompatibleMethodOverride]
def flatten(self, /, order: _OrderKACF = "C") -> matrix[tuple[L[1], int], _DType_co]: ... # pyright: ignore[reportIncompatibleMethodOverride]

Expand Down
11 changes: 9 additions & 2 deletions 11 numpy/typing/tests/data/reveal/ndarray_conversion.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ assert_type(b1_0d.tolist(), bool)
assert_type(u2_1d.tolist(), list[int])
assert_type(i4_2d.tolist(), list[list[int]])
assert_type(f8_3d.tolist(), list[list[list[float]]])
assert_type(cG_4d.tolist(), complex | list[complex] | list[list[complex]] | list[list[list[Any]]])
assert_type(i0_nd.tolist(), int | list[int] | list[list[int]] | list[list[list[Any]]])
assert_type(cG_4d.tolist(), Any)
assert_type(i0_nd.tolist(), Any)

# regression tests for numpy/numpy#27944
any_dtype: np.ndarray[Any, Any]
any_sctype: np.ndarray[Any, Any]
assert_type(any_dtype.tolist(), Any)
assert_type(any_sctype.tolist(), Any)


# itemset does not return a value
# tostring is pretty simple
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.