Description
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