使用 MyPy 对照“TypedDict”验证“Dict”

发布于 2025-01-09 23:45:18 字数 1514 浏览 5 评论 0原文

我有几个数据处理器,它们实现了一个接口,其方法“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2025-01-16 23:45:18

您可以使用泛型来实现这一点。基本上,您定义 IProcess 返回一些泛型类型,并使用 IProcessor[ConcreteType] 定义它是哪个具体类型。

import abc

from typing import TypedDict, Any, Generic, TypeVar

T = TypeVar("T")


class AProcessorResult(TypedDict):
  param1: str
  param2: int

class BProcessorResult(TypedDict):
  paramA: str
  paramB: str
  paramC: float


class IProcessor(abc.ABC, Generic[T]):

  def process(self) -> T:
    raise NotImplementedError


class AProcessor(IProcessor[AProcessorResult]):

  def process(self) -> AProcessorResult:
    result: AProcessorResult = {
      'param1': 'value1',
      'param2': 0
    }
    return result


class BProcessor(IProcessor[BProcessorResult]):

  def process(self) -> BProcessorResult:
    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()

如果您想更严格并强制返回类型始终为映射类型,您可以将类型 var 绑定为

Mapping
from typing import Mapping
T = TypeVar("T", bound=Mapping[str, Any])

您可以使用这些解决方案 此处

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].

import abc

from typing import TypedDict, Any, Generic, TypeVar

T = TypeVar("T")


class AProcessorResult(TypedDict):
  param1: str
  param2: int

class BProcessorResult(TypedDict):
  paramA: str
  paramB: str
  paramC: float


class IProcessor(abc.ABC, Generic[T]):

  def process(self) -> T:
    raise NotImplementedError


class AProcessor(IProcessor[AProcessorResult]):

  def process(self) -> AProcessorResult:
    result: AProcessorResult = {
      'param1': 'value1',
      'param2': 0
    }
    return result


class BProcessor(IProcessor[BProcessorResult]):

  def process(self) -> BProcessorResult:
    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 you want to be more strict and force the return type to be always a mapping type, you can bound the type var as

Mapping
from typing import Mapping
T = TypeVar("T", bound=Mapping[str, Any])

You can playaround with those solutions here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文