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

TYP: remove mypy ignore from pandas/core/construction.py #53112

Merged
merged 5 commits into from
May 6, 2023

Conversation

natmokval
Copy link
Contributor

Related to #37715
mypy ignore[assignment] was removed from pandas/core/construction.py

@natmokval natmokval marked this pull request as ready for review May 5, 2023 22:53
# Incompatible types in assignment (expression has type "ndarray[Any,
# dtype[Any]]", variable has type "MaskedArray[Any, Any]")
data = data.astype(dtype, copy=True) # type: ignore[assignment]
data = ma.asarray(data.astype(dtype, copy=True))
Copy link
Member

Choose a reason for hiding this comment

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

thanks for your PR - would it work to just rename data? looks like the issue that it's getting reassigned to a different type

something like

    data_newname = data.astype(dtype, copy=True)
    data_newname.soften_mask()
    data_newname[mask] = fill_value
    return data_newname
else:
    return data.copy()

(the hardest part would arguably be coming up with a good name, i.e. not data_newname but something more descriptive, but I haven't looked at this carefully enough yet to think of what)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, @MarcoGorelli for your comment. I will do, as you suggest. What do you think, could be sanitized_data or sanitized_ma a suitable new name for data?

Copy link
Member

@MarcoGorelli MarcoGorelli 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 to me, leaving open a bit in case others have comments

EDIT: oops, not green

@natmokval
Copy link
Contributor Author

Looks like mypy raises an error, I will fix it.

@MarcoGorelli
Copy link
Member

MarcoGorelli commented May 6, 2023

I think the issue might be that astype isn't meant to be called on numpy.ma.MaskedArray https://numpy.org/doc/stable/reference/routines.ma.html

Calling it, the return type is typed to be ndarray, even though if we print it we see it's numpy.ma.MaskedArray:

data = np.ma.MaskedArray([1,2,3], mask=[False, False, True])
reveal_type(data)  # Revealed type is "numpy.ma.core.MaskedArray[Any, Any]"
reveal_type(data.astype('int64', copy=True))  # Revealed type is "numpy.ndarray[Any, numpy.dtype[Any]]"

Might be worth checking if there's an issue in numpy about it, and if not, then reporting to them? Perhaps this could be made more precise on their end?

The method which says "Convert the input to a masked array of the given data-type" is the one you'd originally used: numpy.ma.asarray, so your original solution was probably the correct one, sorry for the noise 🙈 The docs say it that asarray doesn't copy if the original data is an ndarray anyway

This reverts commit 533d841.
@natmokval
Copy link
Contributor Author

I think the issue might be that astype isn't meant to be called on numpy.ma.MaskedArray https://numpy.org/doc/stable/reference/routines.ma.html

Calling it, the return type is typed to be ndarray, even though if we print it we see it's numpy.ma.MaskedArray:

data = np.ma.MaskedArray([1,2,3], mask=[False, False, True])
reveal_type(data)  # Revealed type is "numpy.ma.core.MaskedArray[Any, Any]"
reveal_type(data.astype('int64', copy=True))  # Revealed type is "numpy.ndarray[Any, numpy.dtype[Any]]"

Might be worth checking if there's an issue in numpy about it, and if not, then reporting to them? Perhaps this could be made more precise on their end?

Thanks, I'll check if there's an issue on GitHub about it.

The method which says "Convert the input to a masked array of the given data-type" is the one you'd originally used: numpy.ma.asarray, so your original solution was probably the correct one, sorry for the noise 🙈 The docs say it that asarray doesn't copy if the original data is an ndarray anyway

I rolled back the commit to the first one.

@MarcoGorelli MarcoGorelli added the Typing type annotations, mypy/pyright type checking label May 6, 2023
@MarcoGorelli MarcoGorelli added this to the 2.1 milestone May 6, 2023
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

let's get this in, thanks @natmokval

@MarcoGorelli MarcoGorelli merged commit 607316c into pandas-dev:main May 6, 2023
Rylie-W pushed a commit to Rylie-W/pandas that referenced this pull request May 19, 2023
…53112)

* remove ignore[assignment] from pandas/core/construction.py

* rename data

* Revert "rename data"

This reverts commit 533d841.
Daquisu pushed a commit to Daquisu/pandas that referenced this pull request Jul 8, 2023
…53112)

* remove ignore[assignment] from pandas/core/construction.py

* rename data

* Revert "rename data"

This reverts commit 533d841.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants