Reputation: 746
I think this is a bug in mypy but I'm not sure:
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()
This code causes mypy to emit:
error: Overloaded function signatures 1 and 2 overlap with incompatible return types [overload-overlap]
Play with it here: https://mypy-play.net/?mypy=latest&python=3.12&gist=38e025ef049b6a1121cdd92c998e84a9
This happens specifically when inheriting from NamedTuple
, not from tuple
or dict
or object
.
Should I file an issue on mypy or is that expected behavior due to something I did not consider?
EDIT:
In my actual code I have f(A)->B and f(B)->A, so I can't just use a TypeVar. I didn't want to make the minimal example here confusing.
Upvotes: 1
Views: 102