Skip to content

Inheriting from NamedTuple causes "Overloaded function signatures 1 and 2 overlap with incompatible return types" #18562

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
mqnc opened this issue Jan 29, 2025 · 0 comments · Fixed by #18564
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code topic-named-tuple topic-overloads

Comments

@mqnc
Copy link

mqnc commented Jan 29, 2025

Bug Report

When two classes inherit from NamedTuple, I get an overlap error when I want to overload based on their type.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=38e025ef049b6a1121cdd92c998e84a9

from typing import overload, NamedTuple

class A(NamedTuple):
    pass

class B(NamedTuple):
    pass

@overload
def frobnicate(arg: A) -> A: ...

@overload
def frobnicate(arg: B) -> B: ...

def frobnicate(arg: A | B) -> A | B:
    if isinstance(arg, A):
        return A()
    elif isinstance(arg, B):
        return B()
    else:
        raise TypeError()

Expected Behavior

I expect this to not cause an error. It does not report an error if A and B inherit from tuple, dict or object instead.

Actual Behavior

error: Overloaded function signatures 1 and 2 overlap with incompatible return types [overload-overlap]

Your Environment

  • Mypy version used: 1.14.1
  • Mypy command-line flags: (playground)
  • Mypy configuration options from mypy.ini (and other config files): (playground)
  • Python version used: 3.12
@mqnc mqnc added the bug mypy got something wrong label Jan 29, 2025
@A5rocks A5rocks added topic-named-tuple topic-overloads topic-overlap Overlapping equality check false-positive mypy gave an error on correct code and removed topic-overlap Overlapping equality check labels Jan 29, 2025
JukkaL pushed a commit that referenced this issue Jan 30, 2025
…18564)

Fixes #18562.

Fixes #6623.

Fixes #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.
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 topic-named-tuple topic-overloads
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants