Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
neilvyas opened this issue Apr 4, 2019 · 2 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-named-tuple topic-overloads

Comments

@neilvyas
Copy link

neilvyas commented Apr 4, 2019

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
@neilvyas
Copy link
Author

neilvyas commented Apr 4, 2019

I discovered this issue while using @overload, hence its use.

@ilevkivskyi
Copy link
Member

Yes, this looks like a bug, named tuples should be considered as nominal.

Probably the overlap checks are missing the tuple fallback somewhere. If this is the case, then this should be not hard to fix.

cc @Michael0x2a who previously helped a lot with overloads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-named-tuple topic-overloads
Projects
None yet
Development

No branches or pull requests

2 participants