【发布时间】:2018-10-10 19:27:04
【问题描述】:
我希望能够编写一个函数来检查字典是否与我的TypedDict 确认,但是我无法正确获取泛型类型。所以结果函数应该是这样的:
T = typing.Generic('T', bound=...) # This is `bound=...` something I want to find out
def check_typeddict(value: dict, to_type: typing.Type[T]) -> T:
# do some type checking
return typing.cast(T, value)
check_type(MyTypedDict, {'a': 5})
像使用 TypedDict 或 dict 作为 bound 值这样的事情不起作用,这根本不可能(还)还是我错过了其他东西?
【问题讨论】:
标签: python python-3.x mypy