MCPcopy Create free account
hub / github.com/python/mypy / analyze_identity_global_assignment

Method analyze_identity_global_assignment

mypy/semanal.py:3375–3414  ·  view source on GitHub ↗

Special case 'X = X' in global scope. This allows supporting some important use cases. Return true if special casing was applied.

(self, s: AssignmentStmt)

Source from the content-addressed store, hash-verified

3373 self.process__slots__(s)
3374
3375 def analyze_identity_global_assignment(self, s: AssignmentStmt) -> bool:
3376 """Special case 'X = X' in global scope.
3377
3378 This allows supporting some important use cases.
3379
3380 Return true if special casing was applied.
3381 """
3382 if not isinstance(s.rvalue, NameExpr) or len(s.lvalues) != 1:
3383 # Not of form 'X = X'
3384 return False
3385 lvalue = s.lvalues[0]
3386 if not isinstance(lvalue, NameExpr) or s.rvalue.name != lvalue.name:
3387 # Not of form 'X = X'
3388 return False
3389 if self.type is not None or self.is_func_scope():
3390 # Not in global scope
3391 return False
3392 # It's an assignment like 'X = X' in the global scope.
3393 name = lvalue.name
3394 sym = self.lookup(name, s)
3395 if sym is None:
3396 if self.final_iteration:
3397 # Fall back to normal assignment analysis.
3398 return False
3399 else:
3400 self.defer()
3401 return True
3402 else:
3403 if sym.node is None:
3404 # Something special -- fall back to normal assignment analysis.
3405 return False
3406 if name not in self.globals:
3407 # The name is from builtins. Add an alias to the current module.
3408 self.add_symbol(name, sym.node, s)
3409 if not isinstance(sym.node, PlaceholderNode):
3410 for node in s.rvalue, lvalue:
3411 node.node = sym.node
3412 node.kind = GDEF
3413 node.fullname = sym.node.fullname
3414 return True
3415
3416 def should_wait_rhs(self, rv: Expression) -> bool:
3417 """Can we already classify this r.h.s. of an assignment or should we wait?

Callers 1

visit_assignment_stmtMethod · 0.95

Calls 6

is_func_scopeMethod · 0.95
lookupMethod · 0.95
deferMethod · 0.95
add_symbolMethod · 0.95
isinstanceFunction · 0.85
lenFunction · 0.85

Tested by

no test coverage detected