【问题标题】:How to direct mypy to ignore type checking for multi-line imports如何指示 mypy 忽略多行导入的类型检查
【发布时间】:2022-07-23 03:44:52
【问题描述】:

目前我有一个这样的多行导入语句:

from my_module import (
    My_custom_class_1, My_custom_class_2, My_custom_class_3, 
    My_custom_class_4, My_custom_class_5, My_custom_class_6, 
)

在这种情况下,我想使用from my_module import *。我也想忽略这个文件进行类型检查。

对于单行导入,可以简单地使用from my_module import * # type: ignore,但这不适用于多行情况。我尝试在最后一行、最后一个括号、每一行等之后添加。

最后,我不想在my_module 的顶部添加# type: ignore

那么,有没有办法告诉 mypy 忽略这样的多行导入?还是我被 135 个字符的行困住了?

【问题讨论】:

  • 作为中途解决方案,您可以将导入拆分为多行,可能是 2 或 3 行,然后键入 ignore each

标签: python type-hinting mypy


【解决方案1】:

一般来说,当 mypy 由于括号而遇到带有隐式延续的行时,它会将该语句的所有错误附加到该行。所以(playground

from nothing import (  # type: ignore
    foo,
    bar, baz
)

通过类型检查。

同样适用于多行定义,例如:

class Base:
    def foo(self, x: int) -> None: pass

class Child(Base):
    def foo(  # type: ignore[override]
        self,
        this_is_a_very_long_and_incompatible_argument: str,
    ) -> None: ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2011-10-31
    相关资源
    最近更新 更多