Skip to content

Conversation

@kalekundert
Copy link
Contributor

I think this fixes an issue that came up in #2599 -- where certain approx comparisons with numpy arrays trigger a DeprecationWarning -- by simplifying how approx interfaces with numpy arrays.

Previously, approx used inheritance to guarantee that it's __eq__ operator was called rather than the numpy array's. This worked most of the time, but the DeprecationWarning in #2599 was due to a shortcoming of this approach. (It think it also would've choked on comparisons with subclasses of np.ndarray, but I never tested this.)

It turns out that there's a better way to make sure that approx.__eq__ gets called, and that's to set approx.__array_priority__ to a large number. Whenever anything is compared to an array, numpy checks for this attribute and gives priority to whichever object has a higher value. This should avoid the DeprecationWarning, and just in general be more robust. It's also a lot simpler, so I was able to cut out all of the complex dynamic inheritance code that was in there before.

This PR also adds the numpy tests back into tox. My bisect-fu failed me and I couldn't really figure out why they were removed in the first place, but I suspect it was by mistake.

Previously I was subverting the natural order of operations by
subclassing from `ndarray`, but it turns out that you can tell just
numpy to call your operator instead of its by setting the
`__array_priority__` attribute on your class.  This is much simpler, and
it turns out the be a little more robust, too.
I'm not sure why they were removed...
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

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

@kalekundert awesome, thanks a lot for this!

@coveralls
Copy link

Coverage Status

Coverage increased (+0.08%) to 90.886% when pulling ebc7346 on kalekundert:simplify-numpy into 6461dc9 on pytest-dev:features.

@nicoddemus
Copy link
Member

I think this fixes an issue that came up in #2599

It does, I executed pytest testing\python\approx.py -W error from your branch and it passes. 👍

@nicoddemus nicoddemus merged commit 1b732fe into pytest-dev:features Jul 22, 2017
@nicoddemus
Copy link
Member

Thanks again @kalekundert!

@kalekundert kalekundert deleted the simplify-numpy branch July 22, 2017 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants