You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Augmented assignment on asymmetric property uses setter type to resolve __iadd__ #22273
For an augmented assignment like obj.prop += x on a property whose setter type differs from its getter type, mypy picks and checks the in-place operator using the setter type instead of the getter type.
At runtime, obj.prop += x does:
tmp=obj.prop# getter -> Gtmp=tmp.__iadd__(x) # G.__iadd__ (or G.__add__ if there is no __iadd__)obj.prop=tmp# setter: result must be assignable to the setter type S
mypy instead looks up __iadd__ on S and calls S's __iadd__. This causes false positives when S has no __iadd__ but G does (for example, when S is a union). Descriptors whose __set__ value type differs from their __get__ return type have the same problem.
No errors. P.__iadd__(1) returns P, and P is assignable to P | list[int].
Actual Behavior
main.py:27: error: Unsupported operand types for + ("P" and "int") [operator]
main.py:27: error: Incompatible types in assignment (expression has type "int", variable has type "P | list[int]") [assignment]
Your Environment
Mypy version used: 1.20.2; also reproduced on master (0cdcbfd)
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): none
Activity
Use getter type for augmented assignment to asymmetric properties