You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Code that passed the output of itertuples to functions expecting NamedTuple fails in mypy with incompatible type "_PandasNamedTuple"; expected "NamedTuple"
(Maybe related to the fix for issue #834)
To Reproduce
% python3.12 -m venv venv_pandas_stubs
% source venv_pandas_stubs/bin/activate
% pip install --upgrade pip
% pip install mypy pandas pandas-stubs
Collecting mypy
Downloading mypy-1.8.0-cp312-cp312-macosx_11_0_arm64.whl.metadata (1.8 kB)
Collecting pandas
Downloading pandas-2.2.1-cp312-cp312-macosx_11_0_arm64.whl.metadata (19 kB)
Collecting pandas-stubs
Downloading pandas_stubs-2.2.0.240218-py3-none-any.whl.metadata (9.5 kB)
% cat foo.py
import pandas as pd
from typing import NamedTuple
def process_tuple(t: NamedTuple) -> None:
pass
def process_dataframe(df: pd.DataFrame) -> None:
for t in df.itertuples():
process_tuple(t)
% mypy foo.py
foo.py:11: error: Argument 1 to "process_tuple" has incompatible type "_PandasNamedTuple"; expected "NamedTuple" [arg-type]
% pip install pandas-stubs==2.0.3.230814
% mypy foo.py
Success: no issues found in 1 source file
So I'm not sure if we'll be able to address this for 2 reasons:
The special feature of a raw NamedTuple “pseudo-class” that mypy has for NamedTuple has the comment "Note that this behavior is highly experimental, non-standard, and may not be supported by other type checkers and IDEs."
The python typing system doesn't really support having a subclass of NamedTuple with undefined fields, which is what we would need to return in this case.
From a static typing perspective, we know we are returning a kind of "generic" named tuple, but we don't know what the fields are, since it has to work for any DataFrame
Describe the bug
Code that passed the output of itertuples to functions expecting
NamedTuple
fails in mypy withincompatible type "_PandasNamedTuple"; expected "NamedTuple"
(Maybe related to the fix for issue #834)
To Reproduce
Please complete the following information:
pandas-stubs
: 2.2.0.240218Additional context
mypy has special casing for NamedTuple - https://mypy.readthedocs.io/en/latest/kinds_of_types.html#named-tuples
The text was updated successfully, but these errors were encountered: