python / python/mypy

Covariance error inferred for lambda has invalid line number associated with it

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

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

bug priority-0-high topic-error-reporting
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Versions

mypy 0.770
python 3.8.2

Description

This is probably related to the https://github.com/python/mypy/issues/4270

Problem is triggered when using recipe everything_except from hypothesis documentation:

https://hypothesis.readthedocs.io/en/latest/data.html#hypothesis.strategies.from_type

I have a file with following content (this is the whole file):

from typing import TypeVar

from hypothesis import strategies as st

_Ex = TypeVar("_Ex", covariant=True)


def everything_except(*types: type) -> st.SearchStrategy[_Ex]:
    strategy = st.from_type(type).flatmap(st.from_type)

    return strategy.filter(lambda x: not isinstance(x, types))  # type: ignore

Running mypy on first clean run (without .mypy_cache) has the following output:

example.py:1276: error: Cannot use a covariant type variable as a parameter
Found 1 error in 1 file (checked 1 source file)

For some reason, mypy sees error on line 1276 which does not exist. On the other hand, running mypy for the second time gives:

Success: no issues found in 1 source file

What's more, when you have pretty set to True, first run gives:

Traceback (most recent call last):
  File "/home/krprzyby/.local/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/krprzyby/.local/lib/python3.8/site-packages/mypy/__main__.py", line 8, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 89, in main
  File "mypy/build.py", line 180, in build
  File "mypy/build.py", line 249, in _build
  File "mypy/build.py", line 2649, in dispatch
  File "mypy/build.py", line 2956, in process_graph
  File "mypy/build.py", line 3073, in process_stale_scc
  File "mypy/errors.py", line 476, in file_messages
  File "mypy/errors.py", line 449, in format_messages
IndexError: list index out of range

Running mypy for the second time works correctly with pretty set to True because the line number is correct and # type: ignore works.

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

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

はじめの一歩

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

調査の方向性

空の .mypy_cache と pretty 出力を有効にした状態で、提示された例を再現します。まず mypy/errors.py と、トレースバックに示されている関連する build.py のパスを読みます。完了条件は、初回実行で有効なソース行が報告され、IndexError が発生せず、その後の実行でも type-ignore の動作が一貫していることです。

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

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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