Skip to content

typing.NamedTuple collides based on field order and type, doesn't consider class name or field names #6623

@neilvyas

Description

@neilvyas

I expect this code to typecheck, since A and B are nominally different. Note that changing the field type or adding additional fields results in A and B no longer being considered overlapping, but changing the field name does not, so it appears that type-identity for NamedTuples is determined solely by field type and order.

This is python 3.6.4 and mypy 0.700. I have not attempted to reproduce using git-master.

repro:

# foobar.py
from typing import NamedTuple, overload


A = NamedTuple("A", (("f", int),))
B = NamedTuple("B", (("q", int),))


@overload
def plain(a: A) -> int:
    ...

@overload
def plain(a: B) -> str:
    ...

def plain(a):
    if isinstance(a, A):
        return 4
    elif isinstance(a, B):
        return "4"
    else:
        raise TypeError()

resulting mypy error:

foobar.py:9: error: Overloaded function signatures 1 and 2 overlap with incompatible return types

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions