Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[mypyc] Fix vtable construction for deep trait inheritance#20917

Merged
JukkaL merged 2 commits intopython:masterfrom
VaggelisD:vtable_trait
Mar 10, 2026
Merged

[mypyc] Fix vtable construction for deep trait inheritance#20917
JukkaL merged 2 commits intopython:masterfrom
VaggelisD:vtable_trait

Conversation

@VaggelisD
Copy link
Copy Markdown
Contributor

@VaggelisD VaggelisD commented Feb 27, 2026

Fixes mypyc/mypyc#1189

The core issue: compute_vtable iterates cls.traits (direct parents only) instead of the full trait MRO, so when an intermediate trait is empty, grandparent trait methods are never added to the vtable.

The difference between cls.traits and all_traits for the issue's repro is the following:

Class Child
-----------

 - cls.traits:
    •  Middle  --> {}

 - all_traits:
    •  Child   --> {'derived': <FuncIR Child.derived>}
    •  Middle  --> {}
    •  Base    --> {'value': <FuncIR Base.value>}

@VaggelisD VaggelisD changed the title Fix vtable construction for deep trait inheritance [mypyc] Fix vtable construction for deep trait inheritance Feb 27, 2026
Copy link
Copy Markdown
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, just one question, otherwise looks good.

from typing import Any
from mypy_extensions import trait, mypyc_attr

@mypyc_attr(allow_interpreted_subclasses=True)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the fix specific to allow_interpreted_subclasses? If not, it would be good to have a test that doesn't use it in addition to one that uses it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question; I think the issue is not specific to allow_interpreted_subclasses, we just need to force the use of a vtable.

I'll upload a new test case for this that does not use allow_interpreted_subclasses but still fails on master

@VaggelisD
Copy link
Copy Markdown
Contributor Author

VaggelisD commented Mar 2, 2026

I think the bug is hit whenever we rely on a vtable, i.e:

  • Test 1 (non allow_interpreted_subclasses): Sub overrides value making it non-final → vtable dispatch needed → Child.vtable["value"] is missing → Error

  • Test 2 (allow_interpreted_subclasses): Method is non-final because Python subclasses could override it → Rest of the Test 1 logic

The vtable machinery is tricky, I hope the above check out.

@JukkaL JukkaL merged commit 33bc712 into python:master Mar 10, 2026
17 checks passed
@VaggelisD VaggelisD deleted the vtable_trait branch March 10, 2026 16:20
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.

Trait vtable computation misses methods from non-direct ancestor traits

2 participants