FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

BUG: fixes for StringDType/unicode promoters by ngoldbaum · Pull Request #27636 · numpy/numpy · GitHub

/ numpy Public

BUG: fixes for StringDType/unicode promoters - #27636

Merged
ngoldbaum merged 4 commits into
numpy:mainfrom
ngoldbaum:fix-stringdtype-promoters
Oct 30, 2024
Merged

ngoldbaum merged 4 commits into
numpy:mainfrom
ngoldbaum:fix-stringdtype-promoters

Conversation

ngoldbaum commented Oct 24, 2024
edited
Loading

Copy link
Copy Markdown
Member

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 with PyArray_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 for StringDType inputs.

Copy link
Copy Markdown
Member Author

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.

Copy link
Copy Markdown
Member Author

@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.

mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Member Author

@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.

seberg left a comment
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Member Author

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 None in the Unicode promoters makes them fire way too often (e.g. for add and multiply for purely numeric operands). We should fix that too.

I opened a followup issue about this: #27671

I'll go ahead and merge this.

ngoldbaum merged commit 3126b97 into numpy:main Oct 30, 2024
charris removed the 09 - Backport-Candidate PRs tagged should be backported label Oct 30, 2024

seberg commented Dec 9, 2024

Copy link
Copy Markdown
Member

@danish-circuit this PR doesn't have anything to do with typing. Please make a new issue with a full reproducer ideally.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Operation spuriously returns a legacy string BUG: index string ufunc promoter issues

4 participants


Back | FazBrowse Home | New Git URL