python / python/mypy

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

Open
#12,764 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-runtime-semantics topic-variable-scope
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with SemanticAnalyzer.lookup() in semanal.py, especially the local-scope lookup described in the issue, and reproduce the behavior using the provided X.py example. Verify the fix by running mypy on X.py and confirming that both reveal_type calls report the global x as builtins.int.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.