1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】mypy: 複数行にわたるimport文で#type: ignoreが効かないときの対処法

Posted at

要点

ここに書きましょう。開き丸括弧の直後です。

# 例: some_library.some_module から複数のクラスをインポートする場合
from some_library.some_module import ( # type: ignore
    ClassA,
    ClassB,
    ClassC,
    # ...
)

経緯

Pythonでコードを書いていて、途中からPylint(コード整形ツール)を導入した。
すると、下記の行で C0301: Line too long と怒られた。

from vertexai.generative_models import GenerationConfig, ChatSession, Content, Part, ResponseValidationError, GenerativeModel # type: ignore

そこで、import文の部分を、()を使って複数行に書き直した。

from vertexai.generative_models import (
    GenerationConfig,
    ChatSession,
    Content,
    Part,
    ResponseValidationError,
    GenerativeModel,
) # type: ignore

しかし、この書き方ではダメで、今度はmypy(型チェック用ライブラリ)から以下のようにお叱りを受けた。

Skipping analyzing "vertexai.generative_models": module is installed, but missing library stubs or py.typed marker

vertexaiモジュールには型情報があまり書かれていないため、そこの型チェックをスキップするように指示する# type: ignoreを外すというのは現実的な解決策ではなかった。

解決

冒頭でも書いた通り、開き丸括弧の直後に # type: ignore を書いたところ、Pylintの警告もmypyの警告も消え、無事に解決した。

from vertexai.generative_models import ( # type: ignore
    GenerationConfig,
    ChatSession,
    Content,
    Part,
    ResponseValidationError,
    GenerativeModel,
)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?