python / python/mypy

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

Offen
#16,262 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Erstelle die vier im Bericht gezeigten Dateien: shared.py, a.py, b.py und c.py, und führe mypy -m c aus, um den Fehler wegen inkompatibler Importe zu reproduzieren. Verfolge die Verarbeitung von Sternimporten und die abgeleiteten anonymen Schnittmengentypen; abgeschlossen ist die Aufgabe, wenn identische Schnittmengen keinen Fehler mehr erzeugen und der Repro weiterhin korrekt typgeprüft wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.