Fix false positive when @property getter and setter are non-adjacent#20976
Open
PGrayCS wants to merge 2 commits intopython:masterfrom
Open
Fix false positive when @property getter and setter are non-adjacent#20976PGrayCS wants to merge 2 commits intopython:masterfrom
PGrayCS wants to merge 2 commits intopython:masterfrom
Conversation
`fix_function_overloads` in fastparse.py groups consecutive same-named decorators into an `OverloadedFuncDef`. When a property getter and its setter/deleter are separated by other method definitions, the setter was left as an isolated `Decorator` instead of being folded into the property's `OverloadedFuncDef`. This caused mypy to report: error: "Callable[[A], T]" has no attribute "setter" [attr-defined] error: Name "x" already defined on line N [no-redef] error: Property "x" defined in "A" is read-only [misc] Add a second pass `_merge_non_adjacent_property_overloads` that runs after the existing loop. It scans the output list for any `@x.setter` or `@x.deleter` `Decorator` nodes whose property getter was seen earlier in the same scope, and merges them into the getter's `OverloadedFuncDef` (promoting a lone getter `Decorator` to an `OverloadedFuncDef` when needed). The adjacent case is unaffected because the setter/deleter is already inside the `OverloadedFuncDef` after the first pass. Fixes python#1465.
for more information, see https://pre-commit.ci
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: discord.py (https://github.com/Rapptz/discord.py)
- discord/member.py:509: error: Name "status" already defined on line 496 [no-redef]
- discord/member.py:509: error: "Callable[[Member], Status]" has no attribute "setter" [attr-defined]
- discord/shard.py:634: error: Property "status" defined in "Member" is read-only [misc]
- discord/client.py:2168: error: Property "status" defined in "Member" is read-only [misc]
|
Contributor
|
The original issue surprisingly looks extremely popular. Forcing this through
|
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 #1465.
Problem
fix_function_overloadsinfastparse.pygroups consecutive same-named decorators into anOverloadedFuncDef. When a@propertygetter and its@x.setteror@x.deleterare separated by other method definitions, the setter was left as an isolatedDecoratorinstead of being part of the property'sOverloadedFuncDef. This caused mypy to report false positives:This has been open since 2015 and affects any class that has non-trivial method ordering (e.g. setter after a helper method, or after a class variable).
Fix
Add a second pass
_merge_non_adjacent_property_overloadsthat runs after the existing consecutive-grouping loop. It:@property) and records their positions.@x.setteror@x.deleterDecoratornodes whose getter was seen earlier in the same scope.OverloadedFuncDef(promoting a lone getterDecoratorto anOverloadedFuncDefwhen needed) and removes them from their original positions.The adjacent case is completely unaffected: if getter and setter are consecutive, the setter is already inside the
OverloadedFuncDefafter the first pass and won't appear as a standaloneDecoratorto be found by the second pass.Tests
Three new test cases in
test-data/unit/check-classes.test:testPropertyWithNonAdjacentSetter— basic setter separated by a methodtestPropertyWithNonAdjacentDeleter— deleter separated by a methodtestPropertyWithNonAdjacentSetterAndDeleter— both setter and deleter non-adjacentAll 130 existing property-related tests continue to pass.