最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论