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

Skip to content

ENH: call np.positive in ndarray.__pos__, and issue a deprecation warning when it falls #9081

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

Closed
shoyer opened this issue May 9, 2017 · 10 comments · Fixed by #11450
Closed

Comments

@shoyer
Copy link
Member

shoyer commented May 9, 2017

As I wrote in #8967 (comment):

Probably, the desired behavior is something like:

def __pos__(self):
    try:
        return np.positive(self)
    except TypeError:
        issue deprecation warning
        return self.copy()

(This may be a little tricky, though, because it will need to be done in C.)

This will be useful for ndarray subclasses that override __array_ufunc__.

@eric-wieser
Copy link
Member

eric-wieser commented May 10, 2017

I think you also want if self.__array_ufunc__ != ndarray.__array_ufunc__ in there, so that if __array_ufunc__ raises an unrelated TypeError, it's not just swallowed

@njsmith
Copy link
Member

njsmith commented May 10, 2017

In fact, that could potentially replace the TypeError catch altogether?

if self.__array_ufunc__ is not ndarray.__array_ufunc__:
    return np.positive(self)
else:
    return self.copy()

I don't think we'd even need to deprecate anything if we did it this way.

@eric-wieser
Copy link
Member

Difference would be that __array_wrap__ would never see np.positive, but I don't think we care. So that does sound like a good solution

@njsmith
Copy link
Member

njsmith commented May 10, 2017

Or more precisely, __array_wrap__ would see explicit calls to np.positive, but wouldn't see calls to __pos__ (just like today). And that's actually probably what we want, since we don't want to risk breaking existing __array_wrap__ users.

@shoyer
Copy link
Member Author

shoyer commented May 10, 2017

I don't think we'd even need to deprecate anything if we did it this way.

I also wanted to deprecate calls like +np.array(['foo']), which currently works but should really raise an error.

But we could switch to only catching TypeError if __array_ufunc__ is not set. Something like:

def __pos__(self):
    if self.__array_ufunc__ is not ndarray.__array_ufunc__:
        return np.positive(self)
    else:
        try:
            return np.positive(self)
        except TypeError:
            issue deprecation warning
            return self.copy()

This would give us a path to eventually replacing the implementation with:

def __pos__(self):
    return np.positive(self)

@ngoldbaum
Copy link
Member

I'll just say that it would be really nice if np.positive were invoked by operator.pos. With __array_ufunc__ I've been able to remove all of the magic method overrides on my ndarray subclass except for the override for __pos__ due to this issue.

@eric-wieser
Copy link
Member

@ngoldbaum: Although it's also really not hard for you to use def __pos__(self): return np.positive(self) until this is fixed :)

@ngoldbaum
Copy link
Member

I realize, it's just yet another corner case that an implementer of an ndarray subclass needs to deal with.

@shoyer
Copy link
Member Author

shoyer commented May 23, 2017

@ngoldbaum this logic is all pretty self-contained (I would start here), so implementing something like @njsmith's suggestion both would be straightforward. Contributions would certainly be welcome!

It would indeed be nice to get this into 1.13 before the final release.

@mhvk
Copy link
Contributor

mhvk commented Jun 29, 2018

See #11450 for a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants