Repository navigation
Fix augmented assignment on properties with asymmetric getter and setter - #22312
Open
quetzal-china wants to merge 1 commit into
Open
quetzal-china wants to merge 1 commit into
quetzal-china wants to merge 1 commit into
Conversation
The runtime evaluates obj.prop += x by reading the attribute (getter type), mutating it in place, and storing the result through the setter. Mypy resolved the operator method from the lvalue type, which for a member is the setter type: a setter without the in-place method fell back to plain + on the getter type, and a union setter skipped the in-place path entirely, producing false positives. Resolve the operator on the getter type and keep the setter type for the read-only/final diagnostics and the final assignment check.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #22273
obj.prop += xevaluates astmp = obj.prop(getter type),tmp = tmp.__iadd__(x)(ortmp = tmp + x), thenobj.prop = tmp(checked against the setter type). Mypy resolved the operator from the lvalue type, which for a member lvalue is the setter type:+on the getter type —Unsupported operand types for +even though the getter defines__iadd__Instance/TypedDictType), which is the exact repro from the issue__set__value type differs from their__get__return type hit the same problemThis resolves the operator on the getter type and keeps the setter type for the read-only/final diagnostics and for the final
Result type of + incompatible in assignmentcheck.Test cases in
check-statements.test:testInplaceOperatorAsymmetricProperty— the issue repro, now cleantestInplaceOperatorAsymmetricPropertyBadResult— a__iadd__result not assignable to the setter still errorstestInplaceOperatorReadOnlyProperty— read-only property diagnostics preservedThe affected check suites pass on this Windows machine with the same six pre-existing stub-related failures as an unmodified checkout; self-check on
mypy/checker.pyis clean.