Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Description

@mqnc

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions