Skip to content

TypeIs annotation for pands.isna is not quite accurate  #1009

Open
@steve-mavens

Description

@steve-mavens

Describe the bug

Recently #972 introduced a TypeIs annotation for pd.isna. The annotation states that the function returns True if and only if the input value is one of the types NaTType | NAType | None. However, this is not correct, since the function also returns True for the value float('nan'), which is of type float.

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
from pandas import isna

def main() -> None:
   foo: float = float('nan')
   if isna(foo) and 1 == 1:
       print('not a number')
   else:
       print('is a number')

if __name__ == '__main__':
    main()

The output is not a number, but because of the TypeIs annotation, mypy thinks that line is unreachable

  1. Indicate which type checker you are using (mypy or pyright).

mypy 1.10.1

  1. Show the error message received from that type checker while checking your example.

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:

  • OS: [e.g. Windows, Linux, MacOS]: Windows
  • OS Version [e.g. 22]: 11
  • python version: 3.11.4
  • version of type checker: 1.10.1
  • version of installed pandas-stubs: 2.2.2.240909

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions