Skip to content
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

setting warnings to errors changes the behaviour of np.asarray on quantities #892

Closed
keewis opened this issue Oct 17, 2019 · 6 comments · Fixed by #953
Closed

setting warnings to errors changes the behaviour of np.asarray on quantities #892

keewis opened this issue Oct 17, 2019 · 6 comments · Fixed by #953
Labels
numpy Numpy related bug/enhancement

Comments

@keewis
Copy link
Contributor

keewis commented Oct 17, 2019

Using warnings.filterwarnings to promote warnings to errors changes the behaviour of np.asarray (or rather np.array as np.asarray delegates to that):

>>> array = np.arange(10) * ureg.m
>>> np.asarray(array)  # warning emitted because units were stripped
[0 1 2 3 4 5 6 7 8 9]
>>> warnings.filterwarnings("error")
>>> np.asarray(array)  # warning should have become an error, but completely disappeared
array([<Quantity(0, 'meter')>, <Quantity(1, 'meter')>,
       <Quantity(2, 'meter')>, <Quantity(3, 'meter')>,
       <Quantity(4, 'meter')>, <Quantity(5, 'meter')>,
       <Quantity(6, 'meter')>, <Quantity(7, 'meter')>,
       <Quantity(8, 'meter')>, <Quantity(9, 'meter')>], dtype=object)

This happens with pint 0.9, but also with the version from #764 (because __array_function__ does not allow overloading np.asarray)

Edit: I do think this is an issue within numpy, but I think it might help to also track this here. Possibly related: numpy/numpy#9761

@eric-wieser
Copy link
Contributor

In some sense this is a feature - numpy was told it was not allowed to convert to an int in the second case, so it picked an object array instead.

What behavior would you like here instead?

@keewis
Copy link
Contributor Author

keewis commented Oct 17, 2019

The use case here is catching warnings in unit tests. Obviously, if the warning -- that was converted to an error -- gets swallowed, the test won't work.

@keewis
Copy link
Contributor Author

keewis commented Oct 17, 2019

what I would like is that warnings get passed through. So something like:

try:
    ...
except Warning:
    raise
except Exception:
    ...

or catching using AttributeError

@jthielen
Copy link
Contributor

Based on discussion in numpy/numpy#14735, this seems to be a pint issue with using __getattr__ to handle __array_* attributes/methods, in particular, __array__. With this in mind, I think that pint should explicitly set the __array_* attributes/methods it relies on, or at the very least, explicitly define __array__ (see also #878 (comment))?

@keewis
Copy link
Contributor Author

keewis commented Dec 9, 2019

with the merge of numpy/numpy#14745 this won't happen on numpy master and from 1.18 on. We still need to decide if we want to change pint, though, before we can close this

@jthielen
Copy link
Contributor

This relates to some other issues with Pint's handling of __array_* attributes, so I've created #924 to hopefully discuss this and the other issues in general.

@hgrecco hgrecco added the numpy Numpy related bug/enhancement label Dec 11, 2019
@bors bors bot closed this as completed in 5cd3331 Dec 27, 2019
@bors bors bot closed this as completed in #953 Dec 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
numpy Numpy related bug/enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants