Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not report syntax error for global/nonlocal declaration after the name is used. #12747

Open
mrolle45 opened this issue May 8, 2022 · 1 comment
Labels
bug mypy got something wrong topic-variable-scope

Comments

@mrolle45
Copy link

mrolle45 commented May 8, 2022

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
@mrolle45 mrolle45 added the bug mypy got something wrong label May 8, 2022
@erictraut
Copy link

erictraut commented May 8, 2022

I think this is a duplicate of #686.

Edit: Oh, I guess you're saying something different (but related) here. Never mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-variable-scope
Projects
None yet
Development

No branches or pull requests

3 participants