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

Method check_assignment

mypy/checker.py:3373–3561  ·  view source on GitHub ↗

Type check a single assignment: lvalue = rvalue.

(
        self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type: bool = True
    )

Source from the content-addressed store, hash-verified

3371 self.store_type(s.lvalues[-1], alias_type)
3372
3373 def check_assignment(
3374 self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type: bool = True
3375 ) -> None:
3376 """Type check a single assignment: lvalue = rvalue."""
3377 if isinstance(lvalue, (TupleExpr, ListExpr)):
3378 self.check_assignment_to_multiple_lvalues(
3379 lvalue.items, rvalue, rvalue, infer_lvalue_type
3380 )
3381 else:
3382 self.try_infer_partial_generic_type_from_assignment(lvalue, rvalue, "=")
3383 lvalue_type, index_lvalue, inferred = self.check_lvalue(lvalue, rvalue)
3384 # If we're assigning to __getattr__ or similar methods, check that the signature is
3385 # valid.
3386 if isinstance(lvalue, NameExpr) and lvalue.node:
3387 name = lvalue.node.name
3388 if name in ("__setattr__", "__getattribute__", "__getattr__"):
3389 # If an explicit type is given, use that.
3390 if lvalue_type:
3391 signature = lvalue_type
3392 else:
3393 signature = self.expr_checker.accept(rvalue)
3394 if signature:
3395 if name == "__setattr__":
3396 self.check_setattr_method(signature, lvalue)
3397 else:
3398 self.check_getattr_method(signature, lvalue, name)
3399
3400 if name == "__slots__" and self.scope.active_class() is not None:
3401 typ = lvalue_type or self.expr_checker.accept(rvalue)
3402 self.check_slots_definition(typ, lvalue)
3403 if name == "__match_args__" and inferred is not None:
3404 typ = self.expr_checker.accept(rvalue)
3405 self.check_match_args(inferred, typ, lvalue)
3406 if name == "__post_init__":
3407 active_class = self.scope.active_class()
3408 if active_class and dataclasses_plugin.is_processed_dataclass(active_class):
3409 self.fail(message_registry.DATACLASS_POST_INIT_MUST_BE_A_FUNCTION, rvalue)
3410
3411 if isinstance(lvalue, MemberExpr) and lvalue.name == "__match_args__":
3412 self.fail(message_registry.CANNOT_MODIFY_MATCH_ARGS, lvalue)
3413
3414 if lvalue_type:
3415 if isinstance(lvalue_type, PartialType) and lvalue_type.type is None:
3416 # Try to infer a proper type for a variable with a partial None type.
3417 rvalue_type = self.expr_checker.accept(rvalue)
3418 if isinstance(get_proper_type(rvalue_type), NoneType):
3419 # This doesn't actually provide any additional information -- multiple
3420 # None initializers preserve the partial None type.
3421 return
3422
3423 var = lvalue_type.var
3424 if is_valid_inferred_type(
3425 rvalue_type, self.options, is_lvalue_final=var.is_final
3426 ):
3427 partial_types = self.find_partial_types(var)
3428 if partial_types is not None:
3429 if not self.current_node_deferred:
3430 # Partial type can't be final, so strip any literal values.

Calls 15

check_lvalueMethod · 0.95
check_setattr_methodMethod · 0.95
check_getattr_methodMethod · 0.95
check_match_argsMethod · 0.95
failMethod · 0.95
find_partial_typesMethod · 0.95
set_inferred_typeMethod · 0.95
infer_partial_typeMethod · 0.95

Tested by

no test coverage detected