Repository navigation
[mypyc] Reject unknown bases for native classes - #22204
Merged
Merged
Conversation
Fixes mypyc/mypyc#1200. Reject native classes with an `Any` base, since their instance layout is not known during compilation. Suggest using `@mypyc_attr(native_class=False)` instead. Preserve the original base expressions for non-native classes with unknown bases so Python determines their inheritance and instance layout at runtime.
Extract the unknown-base check into a helper and include the relevant base name in the diagnostic. Resolve known type aliases before selecting the unknown base to report. Extend the existing runtime and error cases with known and unknown bases in both orders, two unknown bases, and an alias of a known base. Explicitly check inherited attributes, methods, and initializers on direct and transitive subclasses.
The base class handling is shared with other generic non-native classes: Any bases now use the same path that evaluates base expressions as written, resolves __mro_entries__ and sets __orig_bases__. Extend the unknown-base test with Generic[T] in both base orders and generic metadata checks. Also cover a subscripted compiled generic base (KnownGeneric[int]) next to an Any base, which used to fail at import with "type 'KnownGeneric' is not subscriptable". Also narrow the unknown-base diagnostic guard to expression types that declare a name.
p-sawicki
approved these changes
Oct 14, 2026
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 mypyc/mypyc#1200.
Reject native classes with an
Anybase, since their instance layout isnot known during compilation. Suggest using
@mypyc_attr(native_class=False)instead.
Preserve the original base expressions for non-native classes with unknown
bases so Python determines their inheritance and instance layout at runtime.