Skip to content

Resolve inherited class attributes along the C3 method resolution order - #962

Open
marlon-costa-dc wants to merge 2 commits into
python-rope:masterfrom
marlon-costa-dc:pr/c3-mro-v2
Open

marlon-costa-dc wants to merge 2 commits into
python-rope:masterfrom
marlon-costa-dc:pr/c3-mro-v2

Conversation

@marlon-costa-dc

Copy link
Copy Markdown
Contributor

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:

class A:
    def method(self): ...
class B(A): pass
class C(A):
    def method(self): ...
class D(B, C): pass   # Python: D.method is C.method; rope: A.method

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 new InconsistentHierarchyError for cyclic, repeated and unmergeable bases, which Python refuses to create.
  • AbstractClass.get_consistent_mro() returns None for 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): pytest 2191 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

marlon-costa-dc and others added 2 commits November 21, 2026 23:15
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

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.23009% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.43%. Comparing base (ff8a306) to head (944e523).

Files with missing lines Patch % Lines
rope/base/oi/type_hinting/utils.py 85.71% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@marlon-costa-dc

Copy link
Copy Markdown
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant