Jes*_*sse 11 python python-typing
我想做这样的事情:
from typing import TypeVar, Generic, TypedDict
T = TypeVar("T")
class Foo(Generic[T], TypedDict):
bar: T
...
foo: Foo[int] = {"bar": 42}
Run Code Online (Sandbox Code Playgroud)
但这会产生类型错误(“不能同时从 TypedDict 和非 TypedDict 基类继承”)。
有什么方法可以达到这个结果吗?
Abd*_*P M 14
目前这是不可能的。您不能同时继承 TypedDict 和非 TypedDict 基类。但cpython 存储库上存在活跃的问题,并且正在进行支持此功能的讨论。
编辑:根据最新更新,此功能现已在python 3.11中提供。
| 归档时间: |
|
| 查看次数: |
3395 次 |
| 最近记录: |