是否可以让MyPy检查至少一个联合项目中存在属性(而不是每个联合项目)?

发布于 2025-02-13 17:49:38 字数 952 浏览 0 评论 0原文

我正在尝试将mypy应用于已经具有类型注释的大型代码库(由Pycharm检查)。运行mypy时,我会得到一个bazzilion错误,例如:

error: Item "Bar" of "Union[Foo, Bar]" has no attribute "foo_func"

这是因为mypy检查属性foo_func 每个都存在 工会成员,如下所述: https://mypy.readthedocs.io/en/stable/error_code_list.html#check-check-that-att-tribute-exists-in-each-each-each-union-item-union-union-union-union-attr

为了避免被这样的错误消息超载,我想修改mypy的行为,以便检查属性foo_func至少在 中存在一个 工会成员。

当然,我可以在代码中添加断言以告诉mypy哪个对象是专门输入对象的。但是目前,我只是在寻找一种修改mypy本身的行为的方法。

这可能吗?

I'm trying to apply mypy on a large codebase that already has type annotations (which are checked by PyCharm). When running mypy, I get a bazzilion errors like:

error: Item "Bar" of "Union[Foo, Bar]" has no attribute "foo_func"

This is because mypy checks that the attribute foo_func exists in each union member, as described here:
https://mypy.readthedocs.io/en/stable/error_code_list.html#check-that-attribute-exists-in-each-union-item-union-attr.

To avoid being overloaded with error messages like this, I want to modify the behavior of mypy such that it checks that the attribute foo_func exists in at least one union member.

Of course, I can add assertions in the code to tell mypy which type the object specifically is. But for now, I'm just looking for a way to modify the behavior of mypy itself.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

听,心雨的声音 2025-02-20 17:49:38

尝试添加Inline评论#忽略:[UNIOM-ATTR]在我的情况下

func.coalesce(flight_trip_report.id.isnot(None), false()),  # ignore: [union-attr]

try to add inline comment # ignore: [union-attr] in my case like

func.coalesce(flight_trip_report.id.isnot(None), false()),  # ignore: [union-attr]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文