Skip to content

New semantic analyzer: fix issues with 'X = X' assignments #7144

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

Merged
merged 2 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions mypy/newsemanal/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,11 @@ def visit_import_all(self, i: ImportAll) -> None:

def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
self.statement = s

# Special case assignment like X = X.
if self.analyze_identity_global_assignment(s):
return

tag = self.track_incomplete_refs()
s.rvalue.accept(self)
if self.found_incomplete_ref(tag) or self.should_wait_rhs(s.rvalue):
Expand Down Expand Up @@ -1868,6 +1873,47 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
self.process_module_assignment(s.lvalues, s.rvalue, s)
self.process__all__(s)

def analyze_identity_global_assignment(self, s: AssignmentStmt) -> bool:
"""Special case 'X = X' in global scope.

This allows supporting some important use cases.

Return true if special casing was applied.
"""
if not isinstance(s.rvalue, NameExpr) or len(s.lvalues) != 1:
# Not of form 'X = X'
return False
lvalue = s.lvalues[0]
if not isinstance(lvalue, NameExpr) or s.rvalue.name != lvalue.name:
# Not of form 'X = X'
return False
if self.type is not None or self.is_func_scope():
# Not in global scope
return False
# It's an assignment like 'X = X' in the global scope.
name = lvalue.name
sym = self.lookup(name, s)
if sym is None:
if self.final_iteration:
# Fall back to normal assignment analysis.
return False
else:
self.defer()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name_not_defined() currently already calls defer(), is this additional defer actually needed?

Also this returns True, I would make it more clear by putting the return directly here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that name_not_defined calls defer requires some pretty subtle reasoning so I'd rather keep make it explicit here.

return True
else:
if sym.node is None:
# Something special -- fall back to normal assignment analysis.
return False
if name not in self.globals:
# The name is from builtins. Add an alias to the current module.
self.add_symbol(name, sym.node, s)
if not isinstance(sym.node, PlaceholderNode):
for node in s.rvalue, lvalue:
node.node = sym.node
node.kind = GDEF
node.fullname = sym.node.fullname()
return True

def should_wait_rhs(self, rv: Expression) -> bool:
"""Can we already classify this r.h.s. of an assignment or should we wait?

Expand Down
82 changes: 82 additions & 0 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2886,3 +2886,85 @@ def f() -> None:
x, (y, *z) = t
reveal_type(z) # N: Revealed type is 'builtins.list[builtins.str*]'
[builtins fixtures/list.pyi]

[case testNewAnalyzerIdentityAssignment1]
from foo import *

try:
X = X
except:
class X: # E: Name 'X' already defined (possibly by an import)
pass

reveal_type(X()) # N: Revealed type is 'foo.X'

[file foo.py]
class X: pass

[case testNewAnalyzerIdentityAssignment2]
try:
int = int
reveal_type(int()) # N: Revealed type is 'builtins.int'
except:
class int: # E: Name 'int' already defined (possibly by an import)
pass

reveal_type(int()) # N: Revealed type is 'builtins.int'

[case testNewAnalyzerIdentityAssignment3]
forwardref: C

try:
int = int
reveal_type(int()) # N: Revealed type is 'builtins.int'
except:
class int: # E: Name 'int' already defined (possibly by an import)
pass

reveal_type(int()) # N: Revealed type is 'builtins.int'

class C: pass

[case testNewAnalyzerIdentityAssignment4]
try:
C = C
C
except:
class C:
pass

reveal_type(C()) # N: Revealed type is '__main__.C'

[case testNewAnalyzerIdentityAssignment5]
forwardref: D

try:
C = C
C
except:
class C:
pass

class D: pass

reveal_type(C()) # N: Revealed type is '__main__.C'

[case testNewAnalyzerIdentityAssignment6]
x: C
class C:
pass
C = C

reveal_type(C()) # N: Revealed type is '__main__.C'
reveal_type(x) # N: Revealed type is '__main__.C'

[case testNewAnalyzerIdentityAssignment7]
C = C # E: Name 'C' is not defined

reveal_type(C) # N: Revealed type is 'Any' \
# E: Name 'C' is not defined

[case testNewAnalyzerIdentityAssignment8]
from typing import Final
x: Final = 0
x = x # E: Cannot assign to final name "x"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see so detailed test cases!

1 change: 0 additions & 1 deletion test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,6 @@ def foo(x: Bogus[int]) -> None:
[builtins fixtures/dict.pyi]

[case testOverrideByIdemAliasCorrectType]
# flags: --no-new-semantic-analyzer
C = C
class C: # type: ignore
pass
Expand Down