Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@seberg
Copy link
Member

@seberg seberg commented Nov 8, 2025

This is a lot more complex then I expected. We, correctly, deprecated .dtype attribute lookup recursion, however...
The code still had a try/except that still recursed and that try except carried meaning at least in a weird path of:

class mydtype(np.void):
    dtype = ...

Now does that make sense? Maybe not... we also fall back to object in some paths which should have been broken when a dtype attribute existed on a type but it is a property/descriptor.

So, this removes the recursion, but adds a check for __get__ to filter out those cases, this is something we successfully did for other protocols __array__, __array_interface__, etc.

Closes gh-25306

This is a lot more complex then I expected. We, correctly, deprecated
`.dtype` attribute lookup recursion, however...
The code still had a `try/except` that still recursed and that try
except carried meaning at least in a weird path of:
```
class mydtype(np.void):
    dtype = ...
```

Now does that make sense? Maybe not... we also fall back to object
in some paths which should have been broken when a dtype attribute
existed on a type but it is a property/descriptor.

So, this removes the recursion, but adds a check for `__get__` to
filter out those cases, this is something we successfully did for
other protocols `__array__`, `__array_interface__`, etc.

Signed-off-by: Sebastian Berg <[email protected]>
@seberg seberg added 03 - Maintenance 30 - API and removed 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes labels Nov 8, 2025
@mattip
Copy link
Member

mattip commented Nov 8, 2025

Would adding a __numpy_dtype__ attribute to np.float64 make the detection logic simpler? The documentation mentions one must use np.dtype(np.float64), maybe we could have np.float64.__numpy_dytpe__ instead

@seberg
Copy link
Member Author

seberg commented Nov 9, 2025

Yeah, that is a nice thought, we should maybe add it to deprecate the use of the dtype on the instance.
I think there are two things that make me not jump at it. First, we need type lookup for some things anyway (i.e. I would prefer not to require this for user DTypes; that said such user dtypes don'tsupport the conversion right nowI suspect, that should be fixed).
Second, it does need a careful property dance, since the class attribute needs to give a dtype, while the instance attribute should give an error. That said... while I sm not sure that the Python API makes that simple, it alsodoesn't make it hard.

I guess that makesme lean to: worth an issue/followup, but maybe not needed here?

Comment on lines 10 to 12
For array-like objects we encourage you to implement ``__numpy_dtype__``
with a warning or error to _prevent_ using e.g. ``dtype=dataframe`` in
NumPy functions (it may be good to go via a Deprecation).
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need other array libraries to take action? If they don't do anything, the status-quo for these types of array-likes is maintained (works or doesn't work depending on the case).

A warning / deprecation / error can be handled centrally in numpy. You could even make this behavior dependent on whether the passed object is array-like and/or whether they return a numpy dtype.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me rephrase it to say that you could do this. NumPy would indeed deprecate it for you as soon as we actually deprecate .dtype.
I agree with that deprecation, but think we may want to wait a little bit, implementing it in the downstream library would be an early opt-in effectively.

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.

DEP: Deprecate constructing dtypes from any object having a .dtype attribute

3 participants