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
8 changes: 6 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5502,12 +5502,16 @@ 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 runtime reads the attribute (getter type) to resolve the
# operator; only the resulting value must satisfy the setter.
value_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)
value_type = lvalue_type
inplace, method = infer_operator_assignment_method(value_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, value_type, s.rvalue, s)
if not is_subtype(rvalue_type, lvalue_type):
self.msg.incompatible_operator_assignment(s.op, s)
else:
Expand Down
50 changes: 50 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,56 @@ a += '' # E: Argument 1 to "__iadd__" of "A" has incompatible type "str"; expect
a *= 1 # E: Argument 1 to "__imul__" of "A" has incompatible type "int"; expected "str"
a @= 1 # E: Argument 1 to "__imatmul__" of "A" has incompatible type "int"; expected "str"

[case testInplaceOperatorAsymmetricProperty]
from collections.abc import Iterable

class P:
def __iadd__(self, x: int | Iterable[int]) -> 'P':
return self

class N:
@property
def b(self) -> P:
return P()

@b.setter
def b(self, v: P | list[int]) -> None: ...

n = N()
n.b += 1
[builtins fixtures/property.pyi]

[case testInplaceOperatorAsymmetricPropertyBadResult]
class S:
def __iadd__(self, x: int) -> int:
return x

class M:
@property
def m(self) -> S:
return S()

@m.setter
def m(self, v: S) -> None: ...

m = M()
m.m += 1 # E: Result type of + incompatible in assignment
[builtins fixtures/property.pyi]

[case testInplaceOperatorReadOnlyProperty]
class P:
def __iadd__(self, x: int) -> 'P':
return self

class N:
@property
def ro(self) -> P:
return P()

n = N()
n.ro += 1 # E: Property "ro" defined in "N" is read-only
[builtins fixtures/property.pyi]

[case testInplaceSetitem]
class A(object):
def __init__(self) -> None:
Expand Down
Loading