python / python/mypy

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

Aperta
#12,528 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-type-variables
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla gestione delle ridefinizioni in semanal.py intorno alla riga 3157 e riproduci l'esempio A.py/B.py dell'issue. Controlla la PEP pertinente o la semantica di typing prima di decidere se le definizioni identiche di TypeVar debbano essere accettate; il lavoro è completato quando il comportamento e la diagnostica corrispondono alla specifica definita.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.