Analyze an lvalue or assignment target. Args: lval: The target lvalue nested: If true, the lvalue is within a tuple or list lvalue expression explicit_type: Assignment has type annotation escape_comprehensions: If we are inside a comprehension
(
self,
lval: Lvalue,
nested: bool = False,
explicit_type: bool = False,
is_final: bool = False,
escape_comprehensions: bool = False,
has_explicit_value: bool = False,
is_index_var: bool = False,
)
| 4376 | self.fail(msg, ctx) |
| 4377 | |
| 4378 | def analyze_lvalue( |
| 4379 | self, |
| 4380 | lval: Lvalue, |
| 4381 | nested: bool = False, |
| 4382 | explicit_type: bool = False, |
| 4383 | is_final: bool = False, |
| 4384 | escape_comprehensions: bool = False, |
| 4385 | has_explicit_value: bool = False, |
| 4386 | is_index_var: bool = False, |
| 4387 | ) -> None: |
| 4388 | """Analyze an lvalue or assignment target. |
| 4389 | |
| 4390 | Args: |
| 4391 | lval: The target lvalue |
| 4392 | nested: If true, the lvalue is within a tuple or list lvalue expression |
| 4393 | explicit_type: Assignment has type annotation |
| 4394 | escape_comprehensions: If we are inside a comprehension, set the variable |
| 4395 | in the enclosing scope instead. This implements |
| 4396 | https://www.python.org/dev/peps/pep-0572/#scope-of-the-target |
| 4397 | is_index_var: If lval is the index variable in a for loop |
| 4398 | """ |
| 4399 | if escape_comprehensions: |
| 4400 | assert isinstance(lval, NameExpr), "assignment expression target must be NameExpr" |
| 4401 | if isinstance(lval, NameExpr): |
| 4402 | self.analyze_name_lvalue( |
| 4403 | lval, |
| 4404 | explicit_type, |
| 4405 | is_final, |
| 4406 | escape_comprehensions, |
| 4407 | has_explicit_value=has_explicit_value, |
| 4408 | is_index_var=is_index_var, |
| 4409 | ) |
| 4410 | elif isinstance(lval, MemberExpr): |
| 4411 | self.analyze_member_lvalue(lval, explicit_type, is_final, has_explicit_value) |
| 4412 | if explicit_type and not self.is_self_member_ref(lval): |
| 4413 | self.fail("Type cannot be declared in assignment to non-self attribute", lval) |
| 4414 | elif isinstance(lval, IndexExpr): |
| 4415 | if explicit_type: |
| 4416 | self.fail("Unexpected type declaration", lval) |
| 4417 | lval.accept(self) |
| 4418 | elif isinstance(lval, TupleExpr): |
| 4419 | self.analyze_tuple_or_list_lvalue(lval, explicit_type) |
| 4420 | elif isinstance(lval, StarExpr): |
| 4421 | if nested: |
| 4422 | self.analyze_lvalue(lval.expr, nested, explicit_type) |
| 4423 | else: |
| 4424 | self.fail("Starred assignment target must be in a list or tuple", lval) |
| 4425 | else: |
| 4426 | self.fail("Invalid assignment target", lval) |
| 4427 | |
| 4428 | def analyze_name_lvalue( |
| 4429 | self, |
no test coverage detected