Repository navigation
Fix problem comparing unicode to bytes masked column (#6838) - #6899
Conversation
|
Hi there @taldcroft 👋 - thanks for the pull request! I'm just a friendly 🤖 that checks for issues related to the changelog and making sure that this pull request is milestoned and labeled correctly. This is mainly intended for the maintainers, so if you are not a maintainer you can ignore this, and a maintainer will let you know if any action is required on your part 😃. Everything looks good from my point of view! 👍 If there are any issues with this message, please report them here. |
| # case just swap self and other. | ||
| if (isinstance(self, MaskedColumn) and self.dtype.kind == 'U' and | ||
| isinstance(other, MaskedColumn) and other.dtype.kind == 'S'): | ||
| self, other = other, self |
There was a problem hiding this comment.
This is OK only for == and !=, not for the other comparisons...
There was a problem hiding this comment.
Good point. I actually had written a < compare into the tests but then deleted.
There was a problem hiding this comment.
Actually, have you tried a simpler option, of just returning NotImplemented? python should then automatically call the reverse operator:
In [6]: class A:
...: def __lt__(self, other):
...: return NotImplemented
...: def __gt__(self, other):
...: return 'Yes'
...:
In [7]: a = A()
In [8]: a > a
Out[8]: 'Yes'
In [9]: a < a
Out[9]: 'Yes'
I think this would not work for __eq__ and __ne__, though, since those do not have a different swapped version. So perhaps the clarity of this implementation still is preferable.
There was a problem hiding this comment.
I implemented the suggestion of defining swapped_oper early, which does look cleaner (and saves some memory and 98 ns per compare). On this idea I think that code simplicity wins.
|
It turns out fixing #6839 required this fix in this PR so I combined them. |
| Make comparison methods which encode the ``other`` object to utf-8 | ||
| in the case of a bytestring dtype for Py3+. | ||
| """ | ||
| swap_op = {'__eq__': '__eq__', |
There was a problem hiding this comment.
Minor nitpick - since this is inside _make_compare and you know oper already here, I would defined
swapped_oper = {....}[oper]
and then do op = swapped_oper inside the if statement in _compare - that way the dict look-up doesn't have to happen every time a comparison is done (and possibly it ensures we don't get six versions of the same dict hanging around - not quite sure how python does this internally, but it seems likely it would create a new dict for every call to _make_compare which would then hang around since it is used in _compare).
Even more minor nitpick: you could remove __eq__ and __ne__ if you do {...}.get(oper, oper) - saves another two references to needless strings... But arguably the clarity provided by having all is more important.
|
It looks like this isn't a bug on numpy >=1.13 (#6838 (comment)) - so, we'd need an I wonder if we shouldn't just make that the minimal version, since as that introduced |
|
If I put in this change to test for numpy < 1.13 and run with 1.13, then the current PR fails tests deep in |
|
@taldcroft - OK, sad to hear that the full tests didn't work on numpy 1.13. I agree that in that case we should keep it simple, and like the comment you added. So, I'll go ahead and merge! p.s. If you have a minimal example with |
|
Crossed paths in the air. Using astropy master |
Fix problem comparing unicode to bytes masked column (#6838)
|
Some of the tests here fail for python2 (on the bugfix branch). I would suspect that it's enough to just simply fix the tests, but let me know if I'm on the wrong track. |
|
I've added an ugly workaround hack for the tests for the bugfix branch in #6943. Let me know if you would rather not backport this back, or have a fix in mind. |
This PR addresses #6838 and #6839. The first appears to be an upstream problem in numpy MaskedArray (see #6838 comments). This PR improves testing and implements a narrow fix for the only case that previously failed. For #6839 there was an issue that the encoding dropped the masked array. Fixing this required the fix for #6838 so they are combined in one PR.
Fixes #6838
Fixed #6839