Skip to content

TYP: pd.isna #46222

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 22 commits into from
Mar 18, 2022
Merged

TYP: pd.isna #46222

merged 22 commits into from
Mar 18, 2022

Conversation

twoertwein
Copy link
Member

@twoertwein twoertwein commented Mar 4, 2022

Inferred by mypy and pyright:

import numpy as np
import pandas as pd

class A:
    ...

# bool
reveal_type(pd.isna(1))

# npt.NDArray[np.bool_]
reveal_type(pd.isna([]))
reveal_type(pd.isna(np.zeros(3)))
reveal_type(pd.isna(pd.DatetimeIndex([None])))

# pd.DataFrame
reveal_type(pd.isna(pd.DataFrame()))

# pd.Series
reveal_type(pd.isna(pd.Series()))

# Union of all possible cases
reveal_type(pd.isna(A())) # should be bool

@twoertwein twoertwein added the Typing type annotations, mypy/pyright type checking label Mar 4, 2022
@twoertwein twoertwein requested a review from Dr-Irv March 4, 2022 15:18
@Dr-Irv
Copy link
Contributor

Dr-Irv commented Mar 4, 2022

Would you mind not force pushing the branch in the future? It makes it hard to see what changes you made in just the last commit.

@twoertwein
Copy link
Member Author

Would you mind not force pushing the branch in the future? It makes it hard to see what changes you made in just the last commit.

Okay. was rebasing on main (git pull --rebase main) but kept the new commit separate.

Copy link
Contributor

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

one small comment

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Mar 4, 2022

Would you mind not force pushing the branch in the future? It makes it hard to see what changes you made in just the last commit.

Okay. was rebasing on main (git pull --rebase main) but kept the new commit separate.

I just do

git fetch upstream
git merge upstream/main
git add the_files_I_changed
git commit -m "why I changed those files"
git push origin mybranch

That seems to make it easier to track things for others.

@Dr-Irv Dr-Irv requested a review from simonjayhawkins March 4, 2022 23:35
Copy link
Contributor

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

one small grammatical change

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Mar 5, 2022

@twoertwein I approved, but I think @simonjayhawkins or @jbrockmendel should take a look as well.

@jreback jreback added this to the 1.5 milestone Mar 7, 2022
@jreback
Copy link
Contributor

jreback commented Mar 16, 2022

@twoertwein if you can merge master

@twoertwein
Copy link
Member Author

green, except for mypy error also present on main

@jreback jreback merged commit 2cb628d into pandas-dev:main Mar 18, 2022
@jreback
Copy link
Contributor

jreback commented Mar 18, 2022

thanks @twoertwein

NDFrameT,
Scalar,
npt,
)
Copy link
Member

@simonjayhawkins simonjayhawkins Mar 18, 2022

Choose a reason for hiding this comment

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

Thanks @twoertwein for the PR.

I didn't want to comment on this PR before to avoid "too many cooks" and in general if mypy is happy, so am I. (In some respects, because we have mypy we don't need to review certain aspects of typing as if it is not 100% correct, it will create issues down the line. I see it like a jigsaw puzzle that won't be complete until the last piece is in place, i.e. the codebase is 100% typed.)

However, I think I've seen that your preference is to import from pandas._typing inside the TYPE_CHECKING also elsewhere? If this is correct can you explain the reasoning so that it helps when reviewing other contributors PRs.

AFAIK, we ensure all imports in pandas._typing are guarded so that they can be imported at the top level and for instance npt was added so that we could import without needing to add a TYPE_CHECKING block everywhere it was needed, otherwise we would just import from numpy directly and not include in pandas._typing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! I imported it within the TYPE_CHECKING block because it is only needed for type checking . I will import it outside the TYPE_CHECKING block in next PRs.

Probably one reason why I prefer to put imports in this block is that there are some import cycles that prevent even mypy to function correctly: I think having from pandas import Index caused issues whereas from pandas.core.indexes.base import Index worked without issues (both inside the TYPE_CHECKING block).

Copy link
Member

Choose a reason for hiding this comment

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

Yes, for import cycles it is definitely needed. My preference is also adding imports inside the type checking if added specifically in a typing PR for type annotations but this is difficult to enforce since say a refactor could remove the need for a top level import and it would not be removed because it is still used in type annotations then in theory the import should be moved to the type checking for consistency.

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 having from pandas import Index caused issues whereas from pandas.core.indexes.base import Index worked without issues (both inside the TYPE_CHECKING block).

my preference here is to use from pandas import Index when inside the type checking block. less typing! (the fingers on the keyboard type of typing not the type annotations type of typing!)

Copy link
Member Author

Choose a reason for hiding this comment

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

I just tried replacing it with from pandas import Index and it worked, must have been something different that caused weird mypy issues.

@twoertwein twoertwein deleted the isna branch April 1, 2022 01:36
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request Jul 13, 2022
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.

8 participants