python - Inheriting from NamedTuple causes "Overloaded function signatures 1 and 2 overlap with incompatible return typ

I think this is a bug in mypy but I'm not sure:from typing import overload, NamedTupleclass A(Nam

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: /?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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745306855a4621761.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信