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

Name in class def is resolved in enclosing function, instead of global, scope. #12764

Open
mrolle45 opened this issue May 10, 2022 · 1 comment
Labels
bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope

Comments

@mrolle45
Copy link

mrolle45 commented May 10, 2022

Bug Report

If a name x appearing in a class def C is not yet bound, and it is bound in an enclosing function def f, then mypy resolves x as f.x. However, the python doc says:

Class definition blocks ... are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace.

To Reproduce

X.py:
from typing import TYPE_CHECKING

x = 10
class C:
	print('C.x', x)
	if TYPE_CHECKING: reveal_type(x)
	x = "2"
	print(' ->', x)
def f() -> None:
	x = 20.0
	class C:
		print('f.C.x', x)
		if TYPE_CHECKING: reveal_type(x)
		x = "2"
		print(' ->', x)

python X.py

C.x 10   -- the global x
 -> 2    -- the local x
f.C.x 10   -- the global x
 -> 2    -- the local x

mypy X.py

X.py:6:32: note: Revealed type is "builtins.int"
X.py:13:33: note: Revealed type is "builtins.float"   -- Wrong.  It finds the local f.x!

Expected Behavior

X.py:6:32: note: Revealed type is "builtins.int"
X.py:13:33: note: Revealed type is "builtins.int"   -- It finds the global x

Actual Behavior

See above.

Your Environment

  • Mypy version used: 0.950
  • Python version used: 3.7

Cause of the bug and a fix for it

In SemanticAnalyzer.lookup() method semanal.py line 4419:

        # 3. Local (function) scopes
        for table in reversed(self.locals):
            if table is not None and name in table:
                return table[name]

This looks in enclosed functions even if in a class definition.
Change this to:

        # 3. Local (function) scopes (if within function definition)
        if self.is_func_scope():
            for table in reversed(self.locals):
                if table is not None and name in table:
                    return table[name]

Result from mypy with this change:

X.py:6:32: note: Revealed type is "builtins.int"
X.py:13:33: note: Revealed type is "builtins.int"
@mrolle45 mrolle45 added the bug mypy got something wrong label May 10, 2022
@ritikBhandari
Copy link

Can I take up this issue if possible?

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-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants