【发布时间】:2021-12-12 20:38:16
【问题描述】:
我有以下(键入的)Python 函数:
from typing import overload, Literal, NoReturn
@overload
def check(condition: Literal[False], msg: str) -> NoReturn:
pass
@overload
def check(condition: Literal[True], msg: str) -> None:
pass
def check(condition, msg):
if not condition:
raise Exception(msg)
Pyright 类型检查器抱怨:
Overloaded function implementation is not consistent with signature of overload 1
Function return type "NoReturn" is incompatible with type "None"
Type cannot be assigned to type "None"
我对此感到困惑——Pyright 显然无法确定如果条件为 False,check 将始终抛出错误。我怎样才能按摩它以使其发挥作用?
【问题讨论】:
-
对于上下文,此代码通过 mypy 的类型检查。
-
有趣,也许这是一个错误——我将发布到 Pyright 问题。