我有以下(打印的)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
将总是抛出一个错误。我该如何修改使其正常工作?