Skip to content

Treat non-inplace augmented assignment as a simple assignment #3110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1909,8 +1909,14 @@ def visit_operator_assignment_stmt(self,
"""Type check an operator assignment statement, e.g. x += 1."""
lvalue_type = self.expr_checker.accept(s.lvalue)
inplace, method = infer_operator_assignment_method(lvalue_type, s.op)
rvalue_type, method_type = self.expr_checker.check_op(
method, lvalue_type, s.rvalue, s)
if inplace:
rvalue_type, method_type = self.expr_checker.check_op(
method, lvalue_type, s.rvalue, s)
else:
lvalue_type, index_lvalue, inferred = self.check_lvalue(s.lvalue)
expr = OpExpr(s.op, s.lvalue, s.rvalue)
expr.set_line(s)
rvalue_type = self.expr_checker.accept(expr, lvalue_type)

if isinstance(s.lvalue, IndexExpr) and not inplace:
self.check_indexed_assignment(s.lvalue, s.rvalue, s.rvalue)
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -1514,3 +1514,10 @@ class A(): pass
class B(): pass
[out]
main:8: error: Incompatible types in assignment (expression has type "A", variable has type "B")

[case testAugmentedAssignmentIntFloat]
weight0 = 65.5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add reveal_type(weight0) after each assignment.

Copy link
Contributor Author

@pkch pkch Apr 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After I did it, I discovered another problem: I didn't update the binder (the last weight0 should go back to float, but it stayed as int in my patch). I have fixed it by using a higher level check_assignment method instead of copying/pasting a line from check_simple_assignment . I think this way, the code has become cleaner overall since I could get rid of a couple lines from the original implementation of visit_operator_assignment_stmt (they are taken care of in the check_assignment call).

weight0 = 65
weight0 *= 0.5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test also case where there is an error due to invalid operand types for *.


[builtins fixtures/float.pyi]
30 changes: 30 additions & 0 deletions test-data/unit/fixtures/float.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Any = 0

class object:
def __init__(self) -> None: pass

class type:
def __init__(self, x: Any) -> None: pass

class str:
def __add__(self, other: 'str') -> 'str': pass
class bytes: pass

class tuple: pass
class function: pass

class ellipsis: pass


class int:
def __float__(self) -> float: ...
def __int__(self) -> int: ...
def __mul__(self, x: int) -> int: ...
def __rmul__(self, x: int) -> int: ...

class float:
def __float__(self) -> float: ...
def __int__(self) -> int: ...
def __mul__(self, x: float) -> float: ...
def __rmul__(self, x: float) -> float: ...