Skip to content

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

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 8 commits into from
Apr 18, 2025

Conversation

byrdie
Copy link
Contributor

@byrdie byrdie commented Apr 16, 2025

Addresses Issue #28732

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

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

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

@byrdie
Copy link
Contributor Author

byrdie commented Apr 17, 2025

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

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

@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

@byrdie
Copy link
Contributor Author

byrdie commented Apr 17, 2025

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.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

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 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
Copy link
Member

mattip commented Apr 18, 2025

Please fix the documentation failure before merging.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

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

@ngoldbaum
Copy link
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 ngoldbaum merged commit aa4f974 into numpy:main Apr 18, 2025
72 checks passed
@byrdie byrdie deleted the bugfix/strings-array-function branch April 20, 2025 14:52
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy