Skip to content

Commit 9c8d7eb

Browse files
committed
Add comments and more tests
1 parent 24c8af6 commit 9c8d7eb

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,11 +1940,13 @@ def visit_operator_assignment_stmt(self,
19401940
lvalue_type = self.expr_checker.accept(s.lvalue)
19411941
inplace, method = infer_operator_assignment_method(lvalue_type, s.op)
19421942
if inplace:
1943+
# There is __ifoo__, treat as x = x.__ifoo__(y)
19431944
rvalue_type, method_type = self.expr_checker.check_op(
19441945
method, lvalue_type, s.rvalue, s)
19451946
if not is_subtype(rvalue_type, lvalue_type):
19461947
self.msg.incompatible_operator_assignment(s.op, s)
19471948
else:
1949+
# There is no __ifoo__, treat as x = x <foo> y
19481950
expr = OpExpr(s.op, s.lvalue, s.rvalue)
19491951
expr.set_line(s)
19501952
self.check_assignment(lvalue=s.lvalue, rvalue=expr,

test-data/unit/check-statements.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,5 +1524,36 @@ weight0 *= 'a' # E: Incompatible types in assignment (expression has type "str"
15241524
weight0 *= 0.5
15251525
reveal_type(weight0) # E: Revealed type is 'builtins.float'
15261526
weight0 *= object() # E: Unsupported operand types for * ("float" and "object")
1527+
reveal_type(weight0) # E: Revealed type is 'builtins.float'
15271528

15281529
[builtins fixtures/float.pyi]
1530+
1531+
[case testAugmentedAssignmentIntFloatMember]
1532+
class A:
1533+
def __init__(self) -> None:
1534+
self.weight0 = 65.5
1535+
reveal_type(self.weight0) # E: Revealed type is 'builtins.float'
1536+
self.weight0 = 65
1537+
reveal_type(self.weight0) # E: Revealed type is 'builtins.int'
1538+
self.weight0 *= 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "float")
1539+
self.weight0 *= 0.5
1540+
reveal_type(self.weight0) # E: Revealed type is 'builtins.float'
1541+
self.weight0 *= object() # E: Unsupported operand types for * ("float" and "object")
1542+
reveal_type(self.weight0) # E: Revealed type is 'builtins.float'
1543+
1544+
[builtins fixtures/float.pyi]
1545+
1546+
[case testAugmentedAssignmentIntFloatDict]
1547+
from typing import Dict
1548+
d = {'weight0': 65.5}
1549+
reveal_type(d['weight0']) # E: Revealed type is 'builtins.float*'
1550+
d['weight0'] = 65
1551+
reveal_type(d['weight0']) # E: Revealed type is 'builtins.float*'
1552+
d['weight0'] *= 'a' # E: Unsupported operand types for * ("float" and "str") # E: Incompatible types in assignment (expression has type "str", target has type "float")
1553+
d['weight0'] *= 0.5
1554+
reveal_type(d['weight0']) # E: Revealed type is 'builtins.float*'
1555+
d['weight0'] *= object() # E: Unsupported operand types for * ("float" and "object")
1556+
reveal_type(d['weight0']) # E: Revealed type is 'builtins.float*'
1557+
1558+
[builtins fixtures/floatdict.pyi]
1559+

0 commit comments

Comments
 (0)