Skip to content

result of itertuples gives an error when passed to NamedTuple - incompatible type "_PandasNamedTuple"; expected "NamedTuple" #881

Open
@roj516

Description

@roj516

Describe the bug

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

Please complete the following information:

  • OS: MacOS
  • OS Version: 14.3.1
  • python version: 3.12
  • version of type checker: mypy 1.8.0
  • version of installed pandas-stubs: 2.2.0.240218

Additional context
mypy has special casing for NamedTuple - https://mypy.readthedocs.io/en/latest/kinds_of_types.html#named-tuples

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