我有以下(键入)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 显然无法确定check
如果条件为False
. 我怎样才能按摩它以使其发挥作用?