【发布时间】:2023-03-17 11:13:01
【问题描述】:
我正在尝试准确地注释从 mixin 方法返回的类型。
下面的小例子:
from dataclasses import dataclass, field
from typing import Dict, Protocol
class HasStore(Protocol):
store: dict
class StoreGetMixin:
def __getitem__(self: HasStore, key: str): # what to put in -> type?
return self.store[key]
@dataclass
class ThingOne:
size: int
@dataclass
class ThingTwo:
name: str
@dataclass
class Ones(StoreGetMixin):
store: Dict[str, ThingOne] = field(default_factory=dict)
@dataclass
class Twos(StoreGetMixin):
store: Dict[str, ThingTwo] = field(default_factory=dict)
ones = Ones()
ones.store = {"a": ThingOne(1), "b": ThingOne(2)}
one = ones["a"] # <- this should be typed as a ThingOne
twos = Twos()
twos.store = {"a": ThingTwo("one"), "b": ThingTwo("two")}
two = twos["a"] # <- this should be typed as a ThingTwo
所以我正在尝试编写一个可重用的 mixin,它可以访问各种数据类中的 store 字典。但我不知道如何告诉__getitem__ 方法它返回的是什么类型。
我见过输入self 的示例,但在这种情况下我需要输入self.store。 mypy 可以看到self.store 的类型为dict(我假设这来自HasStore),但它不知道键或值类型。
这可能吗?
我是否需要更好地注释HasStore 协议的store?我尝试过,例如dict[str, T],但我无法让它工作,因为T 不会在这样使用时被实例化。
我这样做的原因是使用 Mashumaro 将我的数据类序列化为 JSON,我无法弄清楚如何存储一个以 str 为键的普通字典,而不在名为 @987654334 的数据类中创建单个属性@。所以其中有一些,它们都有一个名为store 的dict,所以我想我会使用collections.abc.MutableMapping 来允许我通过下标访问store,例如twos["a"]。因此,我编写了 mixin 来做到这一点,但如果我不需要,很遗憾失去所有类型。
感谢大家的指点 :)
【问题讨论】:
标签: python python-3.x mypy