ネイティブからサブクラス化しながらそれを行うことはできますがdict、型付きバージョンではできません。コードは次のとおりです。
from typing import TypedDict
class Typed(TypedDict):
x: int
def __str__(self):
return "str-typed"
class Native(dict):
def __str__(self):
return "str-native"
typed = Typed(x=1)
print("typed =", typed)
native = Native(x=1)
print("native =", native)
assert typed == native
そしてその結果:
$ python typed_dict.py
typed = {'x': 1}
native = str-native
$ mypy typed_dict.py
typed_dict.py:7: error: Invalid statement in TypedDict definition; expected "field_name: field_type"
Found 1 error in 1 file (checked 1 source file)
助言がありますか?