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
Units: Quantity always thinks it is iterable #878
Conversation
It looks like the root of the problem here is that in
But because How about changing it to:
I have a branch ready to go with this in a PR if it seems ok...tests still pass. |
Seems reasonable. |
Is there any case where iterating over an iterable (via |
@taldcroft Hm, I guess it could be an issue for file-like objects? |
For file-like objects the test of Just getting nitpicky, an iterable object can have zero length (like
So to me the |
Good point, but that too would get caught by |
This issue probably runs a bit deeper than astropy's I've hit it in my own code (in which I often use things like I have the vague idea of hiding the irrelevant attribute methods behind By the way, |
I think that if we are successful in making Quantity an ndarray subtype then that should resolve this issue. isiterable seems to work for "array scalars":
|
@iguananaut I suspected this might well be part of the solution, however, it is still a little unsatisfying. I suspect that the behavior of "array scalars" are well defined upstream. Although they pass the astropy Its unfortunate that Numpy breaks this with scalar arrays, and an argument which suggests we might not want to move entirely to scalar arrays for Do you know if the Numpy crowd has ever considered fixing this? |
I agree that Numpy scalar arrays are broken in this sense. I think I'm just okay with it because everyone who's used Numpy has just learned that it's something we have to accept. I'd be all for finding a way to fix this from the Numpy end though. |
Yeah, the borken Numpy scalar arrays were the reason for our own Two alternatives for @adrn's approach:
|
I like alternative 1️⃣ I'll try it out and attach a PR if it works. |
Well, turns out that doesn't work because as soon as you define |
Nope: That doesn't quite do it either. Putting 'import Quantity' anywhere in utils.misc (even inside the function call) leads to a nasty import loop. This isn't too surprising actually, and in general stuff in |
Compromise: I changed it to |
whoops, looks like this needs a rebase. Other than that, looks good to me! |
…ator which is nice, and a little preferable over relying on ``__getitem__`. Also fixed ``isiterable()`` to better catch false positives in cases like Numpy arrays and Quantities, where they implement ``__iter__``, but it can raise a ``TypeError`` in certain cases.
Rebased. |
@@ -10,14 +10,14 @@ | |||
import pytest | |||
import numpy as np | |||
|
|||
from ...tests.helper import raises |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it appears this line is necessary - raises
is used below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that too, just forgot to delete it.
Looks like it needs another rebase ( |
All those failures are surprising...I'd think I'd have run the tests before pushing but maybe I didn't? |
The problem here is a bit tricky. ABCs can implement a magic classmethod call According to http://docs.python.org/2/library/collections.html, a class that implements the I still think this is a bug in Python--both in the documentation and the behavior, because even if a class is not properly identifiable as a sequence, calling I also went ahead and registered a few classes as implementing existing collection types where appropriate. |
Indicentally, registering the mentioned classes as Sequences fixed all the tests without having to make any changes to |
…ator which is nice, and a little preferable over relying on ``__getitem__`. Also fixed ``isiterable()`` to better catch false positives in cases like Numpy arrays and Quantities, where they implement ``__iter__``, but it can raise a ``TypeError`` in certain cases.
…pes where appropriate
Wow that's surprisingly subtle. I wonder if you should report it to the python core? Anyway, it's working as expected for us now, so I'll go ahead and merge (I'll just manually fix up |
Weird that this is "closed" instead of "merged". Ah well, nevertheless, it's in. |
And as such this got missed by my backport script, so good thing I double-checked. Usually I've seen GitHub be pretty good about attaching a manual merge to a pull request but as you said that didn't happen this time :/ |
Conflicts: CHANGES.rst Conflicts: astropy/table/table.py astropy/units/tests/test_quantity.py
Don't have time to look at this today, but probably can tackle over the weekend.