使用 MyPy 对照“TypedDict”验证“Dict”
我有几个数据处理器,它们实现了一个接口,其方法“process”接管处理部分并返回结果。结果应该是符合给定结构的字典。这就是为什么我为每个处理器结果定义了 TypedDict。 也许我没有正确理解 TypedDict,但我想实现这样的目标:
import abc
from typing import TypedDict
class AProcessorResult(TypedDict):
param1: str
param2: int
class BProcessorResult(TypedDict):
paramA: str
paramB: str
paramC: float
class IProcessor(abc.ABC):
def process(self) -> <Dictionary mypy-checked against processor result structure>:
raise NotImplementedError
class AProcessor(IProcessor):
def process(self) -> <Dictionary mypy-checked against AProcessorResult structure>:
result: AProcessorResult = {
'param1': 'value1',
'param2': 0
}
return result
class BProcessor(IProcessor):
def process(self) -> <Dictionary mypy-checked against BProcessorResult structure>:
result: BProcessorResult = {
'param1': 'value1',
'param2': 1
}
return result
def main() -> None:
aProcessor: AProcessor = AProcessor()
aProcessor.process() # <- Should be successful during the MyPy check
bProcessor: BProcessor = BProcessor()
bProcessor.process() # <- Should fail during the MyPy check
if __name__ == '__main__':
main()
如果我从“process”方法返回普通 Dict,MyPy 会抱怨类型不兼容:
不兼容的返回值类型(得到“AProcessorResult”,预期 “听写[任意,任意]”)
我此时不想使用数据类,因为无论如何字典将在稍后阶段进行“数据分类”,并且之前需要一些额外的字典处理。
有没有一种巧妙的方法以最抽象和通用的方式实现这一目标?
我正在使用Python 3.8。
I have a couple of data processors that have implemented an interface whose method "process" takes over the processing part with a result to be returned. The result should be a dictionary that is compliant to a given structure. That's why I have defined TypedDict's for each processor result.
Maybe I haven't understood TypedDict correctly, but I want to achieve something like this:
import abc
from typing import TypedDict
class AProcessorResult(TypedDict):
param1: str
param2: int
class BProcessorResult(TypedDict):
paramA: str
paramB: str
paramC: float
class IProcessor(abc.ABC):
def process(self) -> <Dictionary mypy-checked against processor result structure>:
raise NotImplementedError
class AProcessor(IProcessor):
def process(self) -> <Dictionary mypy-checked against AProcessorResult structure>:
result: AProcessorResult = {
'param1': 'value1',
'param2': 0
}
return result
class BProcessor(IProcessor):
def process(self) -> <Dictionary mypy-checked against BProcessorResult structure>:
result: BProcessorResult = {
'param1': 'value1',
'param2': 1
}
return result
def main() -> None:
aProcessor: AProcessor = AProcessor()
aProcessor.process() # <- Should be successful during the MyPy check
bProcessor: BProcessor = BProcessor()
bProcessor.process() # <- Should fail during the MyPy check
if __name__ == '__main__':
main()
If I return ordinary Dict's from the "process" methods, MyPy complains about the type incompatibility:
Incompatible return value type (got "AProcessorResult", expected
"Dict[Any, Any]")
I don't want to use dataclasses at this point, because the dictionaries will be "dataclassified" in a later stage anyway and require some additional dict processing before.
Is there a neat way to achieve this in a most abstract and generic way?
I am using Python 3.8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用泛型来实现这一点。基本上,您定义 IProcess 返回一些泛型类型,并使用 IProcessor[ConcreteType] 定义它是哪个具体类型。
如果您想更严格并强制返回类型始终为映射类型,您可以将类型 var 绑定为
您可以使用这些解决方案 此处
You can use generics to achieve that. Basically, you define IProcess to return some generic type and define which concrete type it is using IProcessor[ConcreteType].
if you want to be more strict and force the return type to be always a mapping type, you can bound the type var as
You can playaround with those solutions here