Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

TYP: Type MaskedArray.__{ge,gt,le,lt}__ #28689

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

Merged
merged 8 commits into from
Apr 15, 2025
Merged

Conversation

MarcoGorelli
Copy link
Member

@MarcoGorelli MarcoGorelli commented Apr 11, 2025

There's quite a few binary operations which are probably quite similar to type, I figured I'd start with checking we can get __ge__ right, then a few more can follow straightforwardly

Comment on lines 438 to 447
@overload
def __ge__(self: _MaskedArrayNumber_co, other: _ArrayLikeNumber_co, /) -> _MaskedArray[bool_]: ...
@overload
def __ge__(self: _MaskedArrayTD64_co, other: _ArrayLikeTD64_co, /) -> _MaskedArray[bool_]: ...
@overload
def __ge__(self: _MaskedArray[datetime64], other: _ArrayLikeDT64_co, /) -> _MaskedArray[bool_]: ...
@overload
def __ge__(self: _MaskedArray[object_], other: Any, /) -> _MaskedArray[bool_]: ...
@overload
def __ge__(self: _MaskedArray[Any], other: _ArrayLikeObject_co, /) -> _MaskedArray[bool_]: ...
Copy link
Member

Choose a reason for hiding this comment

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

>>> np.ma.array([""]) >= np.ma.array([""])
masked_array(data=[ True],
             mask=False,
       fill_value=True)
>>> np.ma.array([b""]) >= np.ma.array([b""])
masked_array(data=[ True],
             mask=False,
       fill_value=True)
>>> np.ma.array([b""]) >= np.ma.array([""])
Traceback (most recent call last):
  File "<python-input-15>", line 1, in <module>
    np.ma.array([b""]) >= np.ma.array([""])
  File "/home/joren/.pyenv/versions/3.13.3/lib/python3.13/site-packages/numpy/ma/core.py", line 4308, in __ge__
    return self._comparison(other, operator.ge)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/home/joren/.pyenv/versions/3.13.3/lib/python3.13/site-packages/numpy/ma/core.py", line 4241, in _comparison
    check = compare(sdata, odata)
numpy._core._exceptions._UFuncNoLoopError: ufunc 'greater_equal' did not contain a loop with signature matching types (<class 'numpy.dtypes.BytesDType'>, <class 'numpy.dtypes.StrDType'>) -> None
>>> np.ma.array([""]) >= np.ma.array([""], dtype=np.dtypes.StringDType)
masked_array(data=[ True],
             mask=False,
       fill_value=True)

so dtype[str_] | StringDType, dtype[str_] | StringDType and dtype[bytes_], dtype[bytes_] will also work, as long as you don't mix bytes_ and string-y dtypes.

I didn't check void yet, but I'm guessing that void, void will also work at runtime.

Copy link
Member

Choose a reason for hiding this comment

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

Btw, I also wouldn't mind if you leave out the overloads and simply accept other: ArrayLike for all self, in case you feel like this is getting too complex ($\times 4$).

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks for your review!

i'll note that this is also an issue for ndarray, have opened a separate issue about it: #28708

Sure, happy to use other: ArrayLike then

@MarcoGorelli MarcoGorelli marked this pull request as ready for review April 15, 2025 09:53
Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

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

Looks good. I wouldn't mind if you also want to do this for the other rich comparison operators while you're at it.

@MarcoGorelli MarcoGorelli changed the title TYP: Type MaskedArray.__ge__ TYP: Type MaskedArray.__{ge,gt,le,lt}__ Apr 15, 2025
Copy link
Member

@jorenham jorenham left a comment

Choose a reason for hiding this comment

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

Mangos; nice.

@jorenham jorenham merged commit d5fd966 into numpy:main Apr 15, 2025
74 checks passed
@jorenham
Copy link
Member

Thanks Marco

VascoConceicao pushed a commit to VascoConceicao/numpy that referenced this pull request May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.