-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: fixes for StringDType/unicode promoters #27636
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
Conversation
The latest version of this PR touches a lot of code that @lysnikolaou wrote in the python-level string ufunc wrappers so I'm giving him a ping in case he wants to take a look. |
@jorenham if you're at all curious how NumPy handles ufunc type dispatching on the C side, this fixes an issue you spotted while playing with the type stubs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. While looking at the tests and also #27637, I did wonder whether you really want the output to be StringDType
in something like replace
when what you started with was a U
array. But I think it probably is just good to moving towards StringDType
by default.
@seberg would you mind taking a look at this? I value your sense of taste for the "right" way to make changes to numpy internals. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine to me. I think there might be a whole in the logic for out=
, but I am not sure it is worth digging deeper (as opposed to a follow-up, maybe when a bug is reported).
I would love to find a better story for this type of thing... The one story that I could potentially see is to make something like an abstract (doesn't need to be an actual thing could just be a tuple as in isinstance
, but conceptually):
class StringOrUnicodeDType:
so that we could just add one promoter, but the promoter would be need to programmatically decide whether the result should be unicode or string.
(Similar to the "this dtype must occur", style of logic. But these ufuncs that include ints show that just a list of dtypes is not super helpful a such)
(Sorry, the changes look good to me, but I am not sure I am in the right state of mind for any inspiration, although I do suspect the tuple idea is about as good as it gets. Together with allowing a promoter to say "do not apply" it would go very far -- if we allow that, a promoter could even register everything, it would just not be friendly)
EDIT: I forgot to say thanks :), this is tricky stuff and looks good, just unfortunately verbose. And verbose isn't fully unintentionally, but it would be good to improve it.
Thanks! I agree there's definitely room for improvement here to make it less verbose. I also noticed while working on this that the use of I opened a followup issue about this: #27671 I'll go ahead and merge this. |
@danish-circuit this PR doesn't have anything to do with typing. Please make a new issue with a full reproducer ideally. |
Fixes #27493. Fixes #27637.
There are a number of missing cases for mixed unicode/string operations that this adds promoters for. Also adds tests for these cases.
Additionally replaces uses of
Py_None
as an abstract promoter target withPyArray_IntAbstractDType
, which makes the promoters fire less offten in unintended cases and is closer to the intention in the code.Also fixes issues with the python wrappers for the string ufuncs incorrectly selecting the fixed-width string branches for some signatures by relying on
np.result_type
to check forStringDType
inputs.