-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
TypeIs annotation for pands.isna
is not quite accurate
#1009
Comments
Python's type system does not provide a way to discriminate between a regular float and a NaN float. I would argue that the current implementation does the most intuitive thing in most cases at the expense of a relatively rare use case. Anecdotally, I have found We could update to include the |
Yes, I think the basic problem here is that |
Thanks for the report. I'm not sure what we can do here, because we can't specify |
One workaround for users would be |
I thought that might work, but the typing system doesn't support it. Found this discussion:
Well, it might work if we could do |
Also, there are allowed to be float NaN values distinct from Is there a way to import |
So, another workaround is to make sure variables/expressions that might contain a float NaN are typed as |
Right now, if you do We're behind on updating the stubs for all 2.x support, which means that |
Great, thanks for your help. I'll probably use |
I ran into the same issue today, and resolved it by typing my variable like |
Describe the bug
Recently #972 introduced a
TypeIs
annotation forpd.isna
. The annotation states that the function returns True if and only if the input value is one of the typesNaTType | NAType | None
. However, this is not correct, since the function also returns True for the valuefloat('nan')
, which is of typefloat
.To Reproduce
pandas
example that is not properly checked by the stubs.The output is
not a number
, but because of theTypeIs
annotation, mypy thinks that line is unreachablemypy
orpyright
).mypy 1.10.1
type_test.py:6: error: Right operand of "and" is never evaluated [unreachable]
if isna(foo) and 1 == 1:
^~~~~~
type_test.py:7: error: Statement is unreachable [unreachable]
print('not a number')
^~~~~~~~~~~~~~~~~~~~~
Found 2 errors in 1 file (checked 1 source file)
Please complete the following information:
pandas-stubs
: 2.2.2.240909Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: