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,
)
| 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. |
no test coverage detected