Forward class keyword arguments in mypyc - #21962
Open
MarthalaJagruthiReddy wants to merge 5 commits into
Open
Conversation
Fixes python#21938 by forwarding class-definition keywords to __init_subclass__ for native classes and to the metaclass for non-native classes. Adds regression coverage for both compiler paths.
Update the runtime declaration and C implementation so native classes can forward keyword arguments to __init_subclass__.
Expose the kwargs dictionary as an argument to the native __init_subclass__ operation.
Cover native and non-native class keyword forwarding and update the corresponding IR expectations.
p-sawicki
reviewed
Sep 10, 2026
Comment on lines
+271
to
275
| # Class header expressions are evaluated before invoking __prepare__. | ||
| self.class_keyword_values = load_class_keyword_values(self.builder, self.cdef) | ||
| non_ext_dict = setup_non_ext_dict( | ||
| self.builder, self.cdef, non_ext_metaclass, non_ext_bases | ||
| ) |
Collaborator
There was a problem hiding this comment.
setup_non_ext_dict calls __prepare__ of the metaclass which it looks like should also receive the keyword args.
This test works with interpreted python and fails when compiled:
from typing import Any, MutableMapping
from mypy_extensions import mypyc_attr
seen: dict[str, object] = {}
@mypyc_attr(native_class=False)
class Meta(type):
@classmethod
def __prepare__(
mcls, name: str, bases: tuple[type, ...], /, **kwargs: Any
) -> MutableMapping[str, object]:
global seen
seen = kwargs
return {}
@mypyc_attr(native_class=False)
class Base(metaclass=Meta):
def __init_subclass__(cls, **kwargs: object) -> None:
pass
@mypyc_attr(native_class=False)
class Child(Base, marker=1):
pass
def test_prepare_received_class_keywords() -> None:
assert seen == {"marker": 1}, seen
test_prepare_received_class_keywords()
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 #21938
Class-definition keyword arguments were preserved in mypy's AST but silently dropped by mypyc. This change forwards them through the native and non-native class-generation paths.
Specifically:
__init_subclass__for native extension classesmetaclass=because Python consumes it for metaclass selection__prepare__and the class bodyTests:
I used an AI coding assistant to help investigate the issue and draft the implementation; I reviewed the resulting code and test behavior locally.