Repository navigation
Resolve inherited class attributes along the C3 method resolution order - #962
Open
marlon-costa-dc wants to merge 2 commits into
Open
marlon-costa-dc wants to merge 2 commits into
marlon-costa-dc wants to merge 2 commits into
Conversation
Class attributes were inherited by a depth-first walk of the bases, so in a diamond a class could inherit the member of a base that Python's `__mro__` places later. Inherited docstring and assignment type hints used the same walk. Linearize the bases with C3, as Python does, and take each attribute from the first ancestor in that order that defines it; type hint inheritance follows the same order. `AbstractClass.get_mro()` returns the linearization and raises the new `InconsistentHierarchyError` for cyclic, repeated and unmergeable bases; `get_consistent_mro()` returns `None` for those classes, which keep today's bases walk, so existing APIs never raise anything new. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #962 +/- ##
==========================================
- Coverage 95.44% 95.43% -0.02%
==========================================
Files 134 134
Lines 26910 27013 +103
==========================================
+ Hits 25685 25780 +95
- Misses 1225 1233 +8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Author
|
The upstream test workflows wait for maintainer approval on fork PRs. For reference, the same head (944e523) passed the fork's full CI test matrix: https://github.com/marlon-costa-dc/rope/actions/runs/44975313541 |
This branch has not been deployed
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.
Class attributes are inherited by a depth-first walk of the bases, so in a diamond a class can inherit the member of a base that Python's
__mro__places later:The same walk picks inherited docstring and assignment type hints, so a hint can come from the wrong base.
This linearizes the bases with C3, as Python does, and takes each attribute from the first ancestor in that order that defines it. Type hint inheritance follows the same order.
AbstractClass.get_mro()returns the linearization, starting with the class. It raises a newInconsistentHierarchyErrorfor cyclic, repeated and unmergeable bases, which Python refuses to create.AbstractClass.get_consistent_mro()returnsNonefor those classes instead of raising. Rope keeps analysing them along their bases exactly as it does today, so attributes, completions and refactorings on such code are unchanged and nothing new raises in existing APIs.Tests: the MRO of a nested hierarchy against Python's own
__mro__, the diamond attribute, docstring and assignment hints along the MRO, and inconsistent, inherited-inconsistent, duplicate-base and cyclic hierarchies. The diamond and hint tests fail on master.This supersedes #920, which was opened from a branch that carried unrelated work. This PR contains only the MRO change (7 files).
Local run (CPython 3.13):
pytest2191 passed, 7 skipped, 5 xfailed; the one error,test_get_package_tuple_compiled, comes from that interpreter's statically linked_sqlite3(no__file__) and is unrelated. pre-commit (black, isort, whitespace) passes.🤖 Generated with Claude Code