使用 Pyright 检查以下代码:
from typing import Union, TypeVar
T = TypeVar('T')
X_or_RecurListOf = Union[T, list['X_or_RecurListOf']]
x: X_or_RecurListOf[str] = ['asd']
产生错误:
5:28 - error: Expression of type "list[str]" cannot be assigned to declared type "X_or_RecurListOf[str]"
Type "list[str]" cannot be assigned to type "X_or_RecurListOf[str]"
"list[str]" is incompatible with "str"
TypeVar "_T@list" is invariant
Type "str" cannot be assigned to type "X_or_RecurListOf[Type[T@X_or_RecurListOf]]"
Type "str" cannot be assigned to type "T@X_or_RecurListOf"
"str" is incompatible with "list[X_or_RecurListOf]" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 infos
Completed in 0.677sec
难道我做错了什么?
还是我误解了Pyright 中支持递归类型的公告?