Treat variable, declared global and assigned a value in a class or function, as a member of the module.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Feature
If a global variable is created solely via an assignment in a class or function to a name declared as global, then treat that name as though that assignment was at module level.
Currently, importing of such a name reports an error.
As a related matter, if the name is annotated at the module level, but there is no assignment, treat that name as missing from the module.
Pitch
This will reflect the runtime behavior.
Mypy will have to assume that the assignment might or might not occur, just like if an assignment at module level is located in a conditional block. In the latter case, the type may be inferred from the first assignment that is seen, and later assignments can check for compatible types.
I propose that mypy treat all assignments to global variables be treated like at module level, for purpose of establishing the existence and types of module level variables. The order that the assignments are analyzed should be in a breadth-first traversal of class and function defs (i.e. process an entire body excluding nested bodies, then process these bodies).
You may want to label this as a bug?.
I am working on writing such breadth-first module traverser which will identify all names in all scopes in a file and where they are first assigned. It will handle assigns to variables declared global just as though they appeared at module level. So that will take care of this proposed feature, as well as other issues with local names that are referenced before they are assigned.
Here's an example:
X.py:
from Y import y1, y2, y3, y4
Y.py:
global y0
y0: float
y2: str
def f():
global y1, y2
y1 = 1
class C:
global y3, y4
y3 = 3
Output from mypy:
X.py:3:1: error: Module "Y" has no attribute "y1"
X.py:3:1: error: Module "Y" has no attribute "y3"
X.py:3:1: error: Module "Y" has no attribute "y4"
The imports of y1 and y3 should succeed. The imports of y0, y2 and y4 should fail because no assignment appears anywhere. mypy is correct for y4 but incorrect for the others.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Partite dal comportamento di importazione dei moduli e analisi dei nomi illustrato nell’issue, quindi esaminate la visita dei moduli in ampiezza proposta per le assegnazioni a nomi globali. Convalidate il risultato usando l’esempio X.py/Y.py: le importazioni di y1 e y3, a cui è stato assegnato un valore, dovrebbero riuscire, mentre le importazioni di y0, y2 e y4, a cui non è stato assegnato alcun valore, dovrebbero fallire.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100