Skip to content

TYP: remove mypy ignore from array_manager.py #52249

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 6 commits into from
Mar 29, 2023

Conversation

natmokval
Copy link
Contributor

Related to #37715

mypy ignore was removed from pandas/core/internals/array_manager.py

# error: Incompatible types in assignment (expression has type "ndarray",
# variable has type "range")
indices = np.nonzero(loc)[0] # type: ignore[assignment]
indices = np.array([i for i in range(len(loc)) if loc[i] is True])
Copy link
Member

Choose a reason for hiding this comment

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

I think you would need to change line 837 to indices : range | np.ndarray = range(

Mypy uses early binding, the if block declares the variable for the first time with type range. When mypy parses the else block, it thinks the type should be range and not np.ndarray. I believe pyright doesn't do that (for each block it uses its own type and then uses the union after an if/else)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you would need to change line 837 to indices : range | np.ndarray = range(

Thank you for the comment. I did as you suggested.

Mypy uses early binding, the if block declares the variable for the first time with type range. When mypy parses the else block, it thinks the type should be range and not np.ndarray. I believe pyright doesn't do that (for each block it uses its own type and then uses the union after an if/else)

Could it be the reason why locally I didn't have a failure in test_dup_across_dtypes(self), but in ci there was an error in the same function in line pandas/tests/frame/test_nonunique_indexes.py:147: ?

Copy link
Member

Choose a reason for hiding this comment

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

Could it be the reason why locally I didn't have a failure in test_dup_across_dtypes(self), but in ci there was an error in the same function in line pandas/tests/frame/test_nonunique_indexes.py:147: ?

The type annotations shouldn't cause a test to fail at runtime. I'm not familiar with the test but I assume that the code changes from your previous commits might have changed the runtime behavior which triggered some tests to fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, I get it. np.nonzero works much more effectively regarding time than my code changes.

@natmokval natmokval force-pushed the 37715-remove-mypy-ignore branch from 5b850ac to 73c4ad7 Compare March 29, 2023 11:00
@natmokval natmokval marked this pull request as ready for review March 29, 2023 20:28
@mroeschke mroeschke added Typing type annotations, mypy/pyright type checking ArrayManager labels Mar 29, 2023
@mroeschke mroeschke added this to the 2.1 milestone Mar 29, 2023
@mroeschke mroeschke merged commit 323ed9b into pandas-dev:main Mar 29, 2023
@mroeschke
Copy link
Member

Thanks @natmokval

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.

3 participants