Does not report syntax error for global/nonlocal declaration after the name is used.
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
In this example:
def f():
print(x)
x = 2
global x
print(y)
global y
def f2():
x = y = 0
def g():
print(x)
x = 2
nonlocal x
print(y)
nonlocal y
Running mypy reports no errors. However, the uses of x and y are all syntax errors.
SemanticAnalyzer.visit_global_decl does not check for prior use of the name at all. It would find x the self.locals[-1] table. SemanticAnalyzer.visit_global_decl does check this table. However looking here is not sufficient for y, which is a free variable at this point in the program.
I would suggest that the analyzer keep track of free variables in the current scope, just as it does for global and nonlocal declarations. Every name appearing in the outer block is exactly one of (global, nonlocal, free, or local). If a name is bound (meaning it is not nonlocal or global), it is discarded from the free variables if it is there. If any simple name lookup doesn't find the name in the current scope, it will be added to the free variables, along with the node (if any) where the lookup found it in some other scope. Then, any global/nonlocal declaration can look in any of these 4 places; the declaration is valid if it is not found there, or is found as the same kind of name.
- Mypy version used: 0.950
- Python version used: 3.7
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne bei SemanticAnalyzer.visit_global_decl und untersuche, wie die aktuellen Tabellen locals, global und nonlocal geprüft werden. Verfolge die Namensauflösung für die Beispiele x und y, einschließlich des Falls mit der freien Variable, und bestimme, wie Deklarationen frühere Verwendungen erkennen sollten. Die Aufgabe ist erledigt, wenn mypy die demonstrierten ungültigen global- und nonlocal-Deklarationen meldet.
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
- 35/100