python / python/mypy

Identical, anonymous intersection types causes incompatible import [assignment] error when imported via *

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

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

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

説明

Bug Report

If you import * from two modules that infer an identical intersection type, mypy will report an error along the lines of

Incompatible import of "<intersection>" (imported name has type "type[a.<intersection>]", local name has type "type[b.<intersection>]")  [assignment]

To Reproduce

This repro requires multiple files. They are delineated by comments.

# shared.py
class Foo: ...
class Bar: ...

# a.py
from shared import Foo, Bar
a_foos: list[Foo]
a_bars = [f for f in a_foos if isinstance(f, Bar)]

# b.py
from shared import Foo, Bar
b_foos: list[Foo]
b_bars = [f for f in b_foos if isinstance(f, Bar)]

# c.py
from a import *
from b import *

The offending mypy invocation is as follows:

$ mypy -m c

Expected Behavior

This should not emit an error because no runtime locals are actually assigned, or at the very least, because the intersections are identical.

Actual Behavior

c.py:2: error: Incompatible import of "<subclass of "Foo" and "Bar">" (imported name has type "type[b.<subclass of "Foo" and "Bar">]", local name has type "type[a.<subclass of "Foo" and "Bar">]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

This bug was reproduced in a fresh venv on Debian GNU/Linux 10 (buster):

$ python --version
Python 3.10.13
$ mypy --version
mypy 1.6.0 (compiled: yes)

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

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

はじめの一歩

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

調査の方向性

レポートに示されている4つのファイル、shared.py、a.py、b.py、c.pyを作成し、mypy -m cを実行して互換性のないインポートのエラーを再現します。ワイルドカードインポートの処理と、推論された匿名の交差型を追跡します。同一の交差型でエラーが発生しなくなり、再現コードが引き続き正しく型チェックされれば完了です。

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

評価

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

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

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