Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5501,12 +5501,17 @@ def visit_operator_assignment_stmt(self, s: OperatorAssignmentStmt) -> None:
# Special case, some additional errors may be given for
# assignments to read-only or final attributes.
lvalue_type = self.expr_checker.visit_member_expr(s.lvalue, True)
# The operator is applied to the value read from the attribute, not
# to the type accepted by a setter. These can differ for properties
# and descriptors.
operator_type = self.expr_checker.visit_member_expr(s.lvalue, False)
else:
lvalue_type = self.expr_checker.accept(s.lvalue)
inplace, method = infer_operator_assignment_method(lvalue_type, s.op)
operator_type = lvalue_type
inplace, method = infer_operator_assignment_method(operator_type, s.op)
if inplace:
# There is __ifoo__, treat as x = x.__ifoo__(y)
rvalue_type, _ = self.expr_checker.check_op(method, lvalue_type, s.rvalue, s)
rvalue_type, _ = self.expr_checker.check_op(method, operator_type, s.rvalue, s)
if not is_subtype(rvalue_type, lvalue_type):
self.msg.incompatible_operator_assignment(s.op, s)
else:
Expand Down
23 changes: 23 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,29 @@ class A:

[builtins fixtures/float.pyi]

[case testAugmentedAssignmentUsesGetterTypeForProperty]
class P:
def __iadd__(self, other: int) -> P: ...

class Q:
def __iadd__(self, other: int) -> int: ...

class C:
@property
def p(self) -> P: ...
@p.setter
def p(self, value: P | str) -> None: ...

@property
def q(self) -> Q: ...
@q.setter
def q(self, value: Q) -> None: ...

c = C()
c.p += 1
c.q += 1 # E: Result type of + incompatible in assignment
[builtins fixtures/property.pyi]

[case testAugmentedAssignmentIntFloatDict]
from typing import Dict
d = {'weight0': 65.5}
Expand Down