MCPcopy Index your code
hub / github.com/python/mypy / analyze_name_lvalue

Method analyze_name_lvalue

mypy/semanal.py:4428–4505  ·  view source on GitHub ↗

Analyze an lvalue that targets a name expression. Arguments are similar to "analyze_lvalue".

(
        self,
        lvalue: NameExpr,
        explicit_type: bool,
        is_final: bool,
        escape_comprehensions: bool,
        has_explicit_value: bool,
        is_index_var: bool,
    )

Source from the content-addressed store, hash-verified

4426 self.fail("Invalid assignment target", lval)
4427
4428 def analyze_name_lvalue(
4429 self,
4430 lvalue: NameExpr,
4431 explicit_type: bool,
4432 is_final: bool,
4433 escape_comprehensions: bool,
4434 has_explicit_value: bool,
4435 is_index_var: bool,
4436 ) -> None:
4437 """Analyze an lvalue that targets a name expression.
4438
4439 Arguments are similar to "analyze_lvalue".
4440 """
4441 if lvalue.node:
4442 # This has been bound already in a previous iteration.
4443 return
4444
4445 name = lvalue.name
4446 if self.is_alias_for_final_name(name):
4447 if is_final:
4448 self.fail("Cannot redefine an existing name as final", lvalue)
4449 else:
4450 self.msg.cant_assign_to_final(name, self.type is not None, lvalue)
4451
4452 kind = self.current_symbol_kind()
4453 names = self.current_symbol_table(escape_comprehensions=escape_comprehensions)
4454 existing = names.get(name)
4455
4456 outer = self.is_global_or_nonlocal(name)
4457 if (
4458 kind == MDEF
4459 and isinstance(self.type, TypeInfo)
4460 and self.type.is_enum
4461 and not name.startswith("__")
4462 ):
4463 # Special case: we need to be sure that `Enum` keys are unique.
4464 if existing is not None and not isinstance(existing.node, PlaceholderNode):
4465 self.fail(
4466 'Attempted to reuse member name "{}" in Enum definition "{}"'.format(
4467 name, self.type.name
4468 ),
4469 lvalue,
4470 )
4471
4472 if explicit_type and has_explicit_value:
4473 self.fail("Enum members must be left unannotated", lvalue)
4474 self.note(
4475 "See https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members",
4476 lvalue,
4477 )
4478
4479 if (not existing or isinstance(existing.node, PlaceholderNode)) and not outer:
4480 # Define new variable.
4481 var = self.make_name_lvalue_var(
4482 lvalue, kind, not explicit_type, has_explicit_value, is_index_var
4483 )
4484 added = self.add_symbol(name, var, lvalue, escape_comprehensions=escape_comprehensions)
4485 # Only bind expression if we successfully added name to symbol table.

Callers 1

analyze_lvalueMethod · 0.95

Calls 15

failMethod · 0.95
current_symbol_kindMethod · 0.95
current_symbol_tableMethod · 0.95
is_global_or_nonlocalMethod · 0.95
noteMethod · 0.95
make_name_lvalue_varMethod · 0.95
add_symbolMethod · 0.95
is_func_scopeMethod · 0.95
store_declared_typesMethod · 0.95
is_final_redefinitionMethod · 0.95

Tested by

no test coverage detected