从基类继承python类型结构

2024-09-28 17:27:36 发布

您现在位置:Python中文网/ 问答频道 /正文

from typing import Dict

class MyDict(dict):
   pass

def f() -> Dict[int, int]: # works
    return {}

def g() -> MyDict[int, int]: # expected no type argument
    return MyDict()

g()

$ python som.py
TypeError: 'type' object is not subscriptable

还有派林返回 E1136: Value 'MyDict' is unsubscriptable (unsubscriptable-object)

如何从Dict继承用于键入的结构?或者至少如何允许键入MyClass[Any]和MyClass[Any,Any]


Tags: fromimporttyping键入returnobjectisdef