Skip to content
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

TYP: preserve shape-type in ndarray.astype() #28169

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

ntrrgc
Copy link
Contributor

@ntrrgc ntrrgc commented Jan 16, 2025

This patch changes the return type in astype() from NDArray to ndarray so that shape information is preserved and adds tests for it.

This comment has been minimized.

Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

There's also the numpy.astype dual to numpy.ndarray.astype, and it's probably a good idea to keep those to in sync:

@overload
def astype(
x: NDArray[Any],
dtype: _DTypeLike[_SCT],
copy: bool = ...,
device: None | L["cpu"] = ...,
) -> NDArray[_SCT]: ...
@overload
def astype(
x: NDArray[Any],
dtype: DTypeLike,
copy: bool = ...,
device: None | L["cpu"] = ...,
) -> NDArray[Any]: ...

After that I'd happy to merge this if the CI and mypy_primer agree 👌🏻.

@jorenham jorenham changed the title TYP: preserve shape in ndarray.astype() TYP: preserve shape-type in ndarray.astype() Jan 16, 2025
This patch changes the return type in astype() from NDArray to ndarray
so that shape information is preserved and adds tests for it.

Similar changes are added to np.astype() for consistency.
@jorenham jorenham self-requested a review January 16, 2025 22:00
Copy link

Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code:

pandas (https://github.com/pandas-dev/pandas)
- pandas/core/arrays/interval.py:811: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]]", variable has type "ndarray[tuple[int], dtype[Any]]")  [assignment]
- pandas/io/sas/sas_xport.py:249: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]]", variable has type "ndarray[tuple[int], dtype[Any]]")  [assignment]

assert_type(np.astype(i4_2d, np.uint16), np.ndarray[tuple[int, int], np.dtype[np.uint16]])
assert_type(f8_3d.astype(np.int16), np.ndarray[tuple[int, int, int], np.dtype[np.int16]])
assert_type(np.astype(f8_3d, np.int16), np.ndarray[tuple[int, int, int], np.dtype[np.int16]])
assert_type(i4_2d.astype(uncertain_dtype), np.ndarray[tuple[int, int], np.dtype[np.generic[Any]]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine to leave it like this, but np.generic[Any] is equivalent to np.generic, which is a bit shorter 🤷🏻

@jorenham jorenham merged commit f74b61c into numpy:main Jan 16, 2025
69 checks passed
@jorenham
Copy link
Member

Thanks @ntrrgc, and welcome aboard!

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Jan 16, 2025
@jorenham
Copy link
Member

jorenham commented Jan 16, 2025

this might cause some issues for mypy users (and only mypy users), so I'm not not sure if we should backport this @charris

@charris
Copy link
Member

charris commented Jan 17, 2025

This might cause some issues for mypy

Does mypy_primer catch those? Also, just to be sure what the diff means, a - means a fixed error?

@jorenham
Copy link
Member

This might cause some issues for mypy

Does mypy_primer catch those?

Only for the projects that it checks.

Also, just to be sure what the diff means, a - means a fixed error?

Yea I believe so: red=good here

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants