Search code examples
pythontype-hintingmypy

How to direct mypy to ignore type checking for multi-line imports


Currently I have a multi-line import statement like this:

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, 
)

In this case, I do not want to use from my_module import *. I also would like to ignore this file for type checking.

For single line imports, one can simply do from my_module import * # type: ignore, however this does not work for the multi-line case. I have tried adding after the last line, last parenthesis, after each line, etc.

Lastly, I don't want to add # type: ignore to the top of my_module.

So, is there a way to tell mypy to ignore multi-line imports like this? Or am I stuck with a 135 character line?


Solution

  • In general, when mypy encounters line with implicit continuation because of parenthesis, it attaches all errors of that statement to that line. So (playground)

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

    passes type checking.

    Same holds for multi-line def, for example:

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