Skip to content

[mypyc] Keep Generic bases of non-native generic classes - #22183

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-non-native-generic-bases
Oct 13, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-non-native-generic-bases

Conversation

@JukkaL

@JukkaL JukkaL commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

A non-native class declared with Generic[T] lost its Generic base when compiled, because mypyc used mypy's resolved base types, and mypy removes Generic[...] and erases type arguments there. The compiled class couldn't be subscripted at runtime and had no __parameters__. Non-native PEP 695 classes such as class G[T] had similar problems. Both cases now work similar to interpreted Python.

Generic NamedTuple and TypedDict classes are not covered here.

Since subscripted bases are now evaluated, the bases match interpreted code. For example, class C(Dict[str, int]) now gets (dict, Generic) as its bases. A base that is generic only in stubs (e.g. email.message.Message[str, str]) now fails at import, like it does in interpreted code.

A non-native class declared with Generic[T] lost its Generic base when
compiled, because mypyc used mypy's resolved base types, and mypy
removes Generic[...] and erases type arguments there. The compiled
class couldn't be subscripted at runtime and had no
`__parameters__`. Non-native PEP 695 classes such as class G[T] had
similar problems. Both cases now work similar to interpreted Python.

Generic NamedTuple and TypedDict classes are not covered here.

Since subscripted bases are now evaluated, the bases match interpreted
code. For example, class C(Dict[str, int]) now gets (dict, Generic) as
its bases. A base that is generic only in stubs (e.g.
email.message.Message[str, str]) now fails at import, like it does in
interpreted code.
@JukkaL
JukkaL merged commit a7f88bb into master Oct 13, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-non-native-generic-bases branch October 13, 2026 08:03
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