Skip to content

Add warning to old/classic class inheritance problem#48

Open
Eddy114514 wants to merge 7 commits intosoftdevteam:migrationfrom
Eddy114514:mro_warning
Open

Add warning to old/classic class inheritance problem#48
Eddy114514 wants to merge 7 commits intosoftdevteam:migrationfrom
Eddy114514:mro_warning

Conversation

@Eddy114514
Copy link
Copy Markdown
Collaborator

@Eddy114514 Eddy114514 commented Apr 22, 2026

Python 2 classic classes use depth-first, left-to-right method lookup. Python 3 only has new-style classes, which use C3 MRO. In classic multiple inheritance, this can silently change which base class provides an attribute after migration.

Example:

class A:
    def do_this(self): return "A"

class B(A): pass

class C(A):
    def do_this(self): return "C"

class D(B, C): pass

In Python 2 classic classes, D().do_this() resolves to A.
Under Python 3 / C3 MRO, the same hierarchy resolves to C.

Behavior Difference:
Python 2 classic lookup:
D -> B -> A -> C
Python 3 / C3 lookup:
D -> B -> C -> A
So the same attribute name can resolve to a different provider.

To implement the warning I only consider the classic classes and classes with multiple inheritance. The code compare class MRO against hypothetical C3 MRO, and warn only when a real existing attribute would resolve to a different provider. It also warns when the hierarchy has no valid C3 linearization and would fail in python3.

@Eddy114514 Eddy114514 self-assigned this Apr 22, 2026
@ltratt ltratt assigned ltratt and unassigned Eddy114514 Apr 23, 2026
@ltratt
Copy link
Copy Markdown
Member

ltratt commented Apr 23, 2026

Can you fix the PR description (the formatting has gone wrong) and squash the commits to remove the ones that aren't relevant for review (note: I'm not necessarily saying squash down to one commit!).

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.

2 participants