python / python/mypy

appending `#type:ignore` to an import does more than skipping analyzing the import

オープン
#14,778 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

I ran into relatively low priority unexpected behavior. I have this code

from anytree import NodeMixin # type:ignore
from typing import *
from abc import abstractmethod

class IFoo(Protocol):
    @abstractmethod
    def foo(self) -> None:
        raise NotImplementedError()

class Foo(IFoo, NodeMixin):
     ...

def main() -> None:
    a = Foo()

if __name__ == '__main__':
    main()

anytree is a third-party package. When I run mypy on it, I expect that static analysis on anytree to be skipped, and static analysis on Foo to fail with something like "Foo needs to implement abstractmethod foo". What actually happens is that mypy exits with success saying 0 errors were found.

What I've tried to do to cause the expected behavior to occur was to use stubgen on the anytree install which generated stubs in the project folder, run mypy --install-types to install type hints for a dependency of anytree, and configure a mypy.ini in project root to point to the stubs. All of which was simple to do, but kind of tedious.

I also read https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports where it mentioned

This can result in mypy failing to warn you about errors in your code. Since operations on Any result in Any, these dynamic types can propagate through your code, making type checking less effective. See Dynamically typed code for more information.

So I guess the suppression of static checking is leaking from anytree.NodeMixin into Foo.

Actual Behavior

mypy exits with success, 0 errors

Your Environment

I'm on Windows.

  • Mypy version used: 0.990
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10.4

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

issue の再現例から始め、mypy 0.990 を実行して、import の抑制によって Foo の抽象メソッドエラーも隠れることを確認します。import に対する # type: ignore の処理を追跡し、import はスキップする一方で、import した基底クラスを使用するコードのエラーは抑制しないことを示す回帰テストを追加します。完了条件は、例で Foo の foo の実装不足が報告されることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers, devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。