python / python/mypy

assigning a TypeVar to an identical value is reported as an error.

Offen
#12,528 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

In my project, I have multiple definitions of the same TypeVar. They originated from merging (with pytype) the original modules with their generated stub files. It may be that pytype was in error, but I'll look into that later.
In this example, the TypeVar Self appears several times in the stub files. The result is:

A.py:
from B import *
Self = TypeVar('Self')

B.py:
Self = TypeVar('Self')

mypy reports the redefinition of Self in A as an error.

I expect that a type checker should consider this as a benign redefinition. It would ignore it, and replace the existing symbol table node (called B.Self) with a new node called A.Self. On any subsequent reanalysis of A, I believe that mypy would treat this as already analyzed and do nothing.

You might disagree with me about this. Does the PEP have any specification about this?

In semanal.py line 3157 is the fail message. I would say that the existing node should first be compared with the new name, values, and bound for equality. When they are equal, skip the subsequent fail message and the return of False.

Your Environment

  • Mypy version used: 0.931
  • Python version used: 3.7

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

Beginne mit der Behandlung von Neudefinitionen in semanal.py um Zeile 3157 und reproduziere das Beispiel A.py/B.py aus dem Issue. Prüfe die relevante PEP oder die Semantik von typing, bevor du entscheidest, ob identische TypeVar-Definitionen akzeptiert werden sollten; abgeschlossen ist die Arbeit, wenn Verhalten und Diagnose der aufgelösten Spezifikation entsprechen.

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
25/100

Neue Issues direkt in Ihr Postfach

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