考虑以下代码:
from typing import List, TypeVar, Callable
_T = TypeVar('_T')
# A generic function that takes a string and a list of stuff and that returns one of the stuff
Prompter = Callable[[str, List[_T]], _T]
# A function that takes such a Prompter and do things with it
ActionDef = Callable[[Prompter[_T]], None]
# A register of all ActionDef's
ACTION_DEFS: List[ActionDef[_T]] = []
我从 pylance 收到错误消息List[ActionDef[_T]]
:
Type variable "_T" has no meaning in this context
如果我这样做List[ActionDef]
,它也会抱怨:
Expected type arguments for generic type alias "ActionDef"
基本上,它希望我做类似的事情ACTION_DEFS: List[ActionDef[int]] = []
,这会破坏整个观点。
问题1:如何定义写ACTION_DEFS
类型声明?
问题2(标题来自哪里):有没有一种方法可以定义Prompter
我不需要随身携带的方式[_T]
?