Python mypy / isort type ignore for long import

Suppose you have mypy + black + isort tools, and long import line, which you need to mark as ignored for mypy. You’ll probably start doing smth like this:

from example.artifact import Object # type: ignore

And after you run isort & black, you’ll end up in this situation:

from example.artifact import (
    Object, # type: ignore
)

Which will break mypy’s type: ignore annotation. To make things work we need to place it as follows:

from example.artifact import (  # type: ignore
    Object,
)

But guess what… isort and black tools will revert your changes back (annotation will be reside in the middle line).

Thus the solution is to disable isort for this particular import as follows:

# isort: off
from example.artifact import (  # type: ignore
    Object,
)
# isort: on

That’s all.

Telegram channel

If you still have any questions, feel free to ask me in the comments under this article or write me at promark33@gmail.com.

If I saved your day, you can support me 🤝