小编Ped*_*tua的帖子

Python 键入:当签名不同时,Mypy 会出现重载“重叠”错误

以下代码似乎生成两个 mypy 错误:Overloaded function signatures 1 and 3 overlap with incompatible return typesOverloaded function signatures 2 and 3 overlap with incompatible return types; 但所有重载都有不同的签名 - Literal[True]、Literal[False] 和 None 不重叠。

@overload
def func_a(*, a: Literal[False] = ...) -> str:
    ...


@overload
def func_a(*, a: None = ...) -> str:
    ...


@overload
def func_a(*, a: Literal[True] = ...) -> int:
    ...


def func_a(*, a: Optional[bool] = None) -> str | int:
    if a:
        return 1
    return "foo"


var1 …
Run Code Online (Sandbox Code Playgroud)

python overloading mypy python-typing

2
推荐指数
1
解决办法
712
查看次数

标签 统计

mypy ×1

overloading ×1

python ×1

python-typing ×1