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

BUG: Re-enable overriding functions in the `np.strings` module. by roytsmart · Pull Request #28741 · numpy/numpy · GitHub

/ numpy Public

BUG: Re-enable overriding functions in the np.strings module. - #28741

Merged
ngoldbaum merged 8 commits into
numpy:mainfrom
roytsmart:bugfix/strings-array-function
Apr 18, 2025
Merged

ngoldbaum merged 8 commits into
numpy:mainfrom
roytsmart:bugfix/strings-array-function

Conversation

roytsmart commented Apr 16, 2025
edited
Loading

Copy link
Copy Markdown
Contributor

Addresses Issue #28732

…swapcase`, `capitalize`, `title`, `_join`, `_split`, `_rsplit`, `_splitlines`, and `translate` functions in the `np.strings` module.

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 great! Ideally, though, there are also tests to ensure we don't regress... I think it can be fairly simple, along the lines of

class TestOverride:
    @classmethod
    def setup_class(cls):
        class Override:
            def __array_function__(self, *args, **kwargs):
                return "function"
            def __array_ufunc__(self, *args, **kwargs):
                return "ufunc"

        cls.override = Override()

    @pytest.mark.parametrize("func, kwargs", [
        (np.strings.upper, {}),
        (...),
    ])
    def test_override_function(self, func, kwargs):
        assert func(self.override, **kwargs) == "function"

    ... same for any ufuncs ...

Also, I think this warrants a changelog entry...

Copy link
Copy Markdown
Contributor Author

@mhvk, thanks for your review. I've tried to implement your suggestions, but some of the ufuncs seem to be casting Override into an object dtype before giving it a chance to call __array_ufunc__. Do you know what I'm doing wrong?

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

@byrdie - the problem is that some of the functions pass the input through np.asanyarray - so the Override class gets turned into an ndarray with object dtype. I fear those functions just need an array_function_dispatch as well (and then move their test to the one using __array_function__).

Slightly annoying, but also good, in that the tests showed the PR was not quite complete yet -- you need 8 more function:

center
expandtabs
ljust
multiply
partition
replace
rjust
rpartition
zfill

Copy link
Copy Markdown
Contributor Author

I'm afraid I don't think I understand why the remaining tests are failing, they don't seem directly related to what I changed.

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

Thanks, this looks all OK now! The test failures indeed seem unrelated, so approving.

Might be good, though, for @ngoldbaum to have a quick look too.

mattip changed the title BUG: Re-enable overriding the mod, encode, decode, upper, lower, swapcase, capitalize, title, _join, _split, _rsplit, _splitlines, and translate functions in the np.strings module. BUG: Re-enable overriding functions in the np.strings module. Apr 18, 2025

mattip commented Apr 18, 2025

Copy link
Copy Markdown
Member

Please fix the documentation failure before merging.

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

Great that everything now passes too. I'll re-approve but will give it a few more days just in case @mattip has other comments, or @ngoldbaum wants to have a look. (If nothing happens, feel free to ping me middle of next week, and I'll merge.)

Copy link
Copy Markdown
Member

Thanks so much for working on this @byrdie!

I'd still like to add a test that verifies the entire public API is overridable via either __array_function__ or __array_ufunc__, but that shouldn't stop this fix.

ngoldbaum merged commit aa4f974 into numpy:main Apr 18, 2025
roytsmart deleted the bugfix/strings-array-function branch April 20, 2025 14:52
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.

4 participants


Back | FazBrowse Home | New Git URL