Skip to content

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

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

Closed
neilvyas opened this issue Apr 4, 2019 · 2 comments · Fixed by #18564
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.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-named-tuple topic-overloads labels Apr 5, 2019
@JukkaL JukkaL closed this as completed in e2b821b Jan 30, 2025
x612skm pushed a commit to x612skm/mypy-dev that referenced this issue Feb 24, 2025
…ython#18564)

Fixes python#18562.

Fixes python#6623.

Fixes python#18520. (Only incidentally
and I'm not exactly sure why.)

I was investigating what input mypy thought satisfied both overloads and
I found that mypy is missing a check on the fallback type.
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

Successfully merging a pull request may close this issue.

2 participants