8000 TYP: Type ``np.ma.getmask`` by MarcoGorelli · Pull Request #28832 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TYP: Type np.ma.getmask #28832

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 3 commits into from
Apr 26, 2025
Merged

TYP: Type np.ma.getmask #28832

merged 3 commits into from
Apr 26, 2025

Conversation

MarcoGorelli
Copy link
Member
@MarcoGorelli MarcoGorelli commented Apr 26, 2025

I've tried specialising this further with:

@overload
def getmask(a: np.ndarray[_ShapeT_co, _DTypeT_co]) -> np.ndarray[_ShapeT_co, dtype[bool_]] | bool_: ...
@overload
def getmask(a: ArrayLike) -> bool_: ...

but then the tests fail with

E   AssertionError: Reveal mismatch at line 286
    
    error:
    Expression is of type "Any", not
    "ndarray[tuple[int, ...], dtype[numpy.bool[builtins.bool]]] | numpy.bool[builtins.bool]"
     [assert-type]
        assert_type(np.ma.getmask(MAR_1d), NDArray[np.bool] | np.bool)

for

assert_type(np.ma.getmask(MAR_1d), NDArray[np.bool] | np.bool)

@jorenham jorenham self-requested a review April 26, 2025 14:26
@MarcoGorelli MarcoGorelli marked this pull request as ready for review April 26, 2025 14:56
@jorenham jorenham added the component: numpy.ma masked arrays label Apr 26, 2025
@jorenham
Copy link
Member

I've tried specialising this further with:

@overload
def getmask(a: np.ndarray[_ShapeT_co, _DTypeT_co]) -> np.ndarray[_ShapeT_co, dtype[bool_]] | bool_: ...
@overload
def getmask(a: ArrayLike) -> bool_: ...

The _DTypeT_co seems redundant; maybe removing it helps?
These overloads are also overlapping in an incompatible way now, because ArrayLike is broader than np.ndarray, but the bool_ it returns is narrower than the return type of the other overload.

@MarcoGorelli
Copy link
Member Author

ah right, thanks! now it passes (except one which PyRight detects accurates but MyPy doesn't, similar to in another issue)

@jorenham
Copy link
Member

except one which PyRight detects accurates but MyPy doesn't

yea... mypy can be a bit "special" in some situations 😅

@jorenham
Copy link
Member

needs a rebase

@jorenham jorenham merged commit 1923b38 into numpy:main Apr 26, 2025
75 checks passed
@jorenham
Copy link
Member

Thanks Marco

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
0