KEMBAR78
arg-type error when inheriting from NamedTuple · Issue #11160 · python/mypy · GitHub
Skip to content

arg-type error when inheriting from NamedTuple #11160

@lgiraldi

Description

@lgiraldi

Bug Report

mypy does not identify NamedTuple as a base type when passing objects from a subclass in function calls.
However, everything works as expected when using a custom base class.

To Reproduce

(Write your steps here:)

from typing import NamedTuple


class Base:
    def __repr__(self):
        return "Base"


class Foo(Base):
    def __repr__(self):
        return "Foo"


class Bar(NamedTuple):
    name: str = "Bar"


def print_base(obj: Base) -> None:
    print(obj)


def print_namedtuple(obj: NamedTuple) -> None:
    print(obj._asdict())


print_base(Foo())
print_namedtuple(Bar())

Expected Behavior

No error.

Actual Behavior

tmp\test.py:27:18: error: Argument 1 to "print_namedtuple" has incompatible type "Bar"; expected "NamedTuple" [arg-type]

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: NA
  • Mypy configuration options from mypy.ini (and other config files):
    pyproject.toml:
[tool.mypy]
exclude = "build|doc|tests"
show_column_numbers = true
show_error_codes = true
strict = false
warn_return_any = true
warn_unused_configs = true

[[tool.mypy.overrides]]
module = ["numpy", "setuptools"]
ignore_missing_imports = true
  • Python version used: miniconda environment, python 3.9.7
  • Operating system and version: Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions