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 falsely rejected value types in ndarray.__setitem__ #27967

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
Dec 12, 2024

Conversation

jorenham
Copy link
Member

fixes #27964

cc @jakevdp

@jorenham jorenham added this to the 2.2.1 release milestone Dec 10, 2024
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

This change looks fine to me (and introducing the _ToIndices seems nice).
But, I am not quite sure about the datetime definition.

I can't say whether it is all the ideal definitions, so if anyone else deeper into typing wants to have a look that would be nice.

@@ -982,6 +983,8 @@ if sys.version_info >= (3, 11):
_ConvertibleToComplex: TypeAlias = SupportsComplex | SupportsFloat | SupportsIndex | _CharLike_co
else:
_ConvertibleToComplex: TypeAlias = complex | SupportsComplex | SupportsFloat | SupportsIndex | _CharLike_co
_ConvertibleToTD64: TypeAlias = dt.timedelta | int | _CharLike_co | character | number | timedelta64 | np.bool | None
_ConvertibleToDT64: TypeAlias = dt.date | int | _CharLike_co | character | number | datetime64 | np.bool | None
Copy link
Member

Choose a reason for hiding this comment

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

How come this has character and _CharLike_co? Is that intentional?

Is this missing dt.datetime?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, it's indeed missing in datetime64:

>>> np.datetime64(np.str_("NaT"))
np.datetime64('NaT')
>>> np.datetime64(np.bytes_("NaT"))
np.datetime64('NaT')

but also

>>> np.datetime64(np.str_("42"))
np.datetime64('0042')
>>> np.datetime64(np.bytes_("42"))
np.datetime64('0042')

🤷🏻

I could sneak it in this PR, or open another PR for it?

Copy link
Member

Choose a reason for hiding this comment

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

I could sneak it in this PR, or open another PR for it?

Not sure I care, if you think it's simple enough (IIUC, this is about the scalar creation?). FWIW, I would expect them to use the identical definitions to scalar assignment.

W.r.t to the strings, what surprises me was that _ConvertibleToComplex does not include character.

The other thing is that you have _ConvertibleToDT64: TypeAlias = dt.date. Should it not also have dt.datetime?

Copy link
Member Author

Choose a reason for hiding this comment

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

W.r.t to the strings, what surprises me was that _ConvertibleToComplex does not include character.

>>> np.str_.__float__
<slot wrapper '__float__' of 'numpy.generic' objects>

which is also reflected in

def __float__(self, /) -> float: ...

so it's already sneakily included

The other thing is that you have _ConvertibleToDT64: TypeAlias = dt.date. Should it not also have dt.datetime?

dt.datetime subclasses of dt.date, so it's automatically included

@jorenham
Copy link
Member Author

I can't say whether it is all the ideal definitions, so if anyone else deeper into typing wants to have a look that would be nice.

There's a good chance I'm missing something here, so more eyes could indeed help in this case

@charris charris merged commit deb00a1 into numpy:main Dec 12, 2024
67 checks passed
@charris
Copy link
Member

charris commented Dec 12, 2024

Let's get this in. I doubt it will be the last :)

Thanks Joren.

@jakevdp
Copy link
Contributor

jakevdp commented Dec 12, 2024

Thanks for the fix!

@jorenham
Copy link
Member Author

There's a good chance I'm missing something here

I missed something...

... and it's now causing the scipy CI to fail 😅

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.

TYP: False positives in ndarray.__setitem__ with object_ dtype in NumPy 2.2
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.