Skip to content

[mypyc] Fix __init_subclass__ running before ClassVar instantiations#20916

Merged
JukkaL merged 2 commits intopython:masterfrom
VaggelisD:init_subclass_fix
Mar 6, 2026
Merged

[mypyc] Fix __init_subclass__ running before ClassVar instantiations#20916
JukkaL merged 2 commits intopython:masterfrom
VaggelisD:init_subclass_fix

Conversation

@VaggelisD
Copy link
Contributor

Fixes mypyc/mypyc#1086

We also ran into this in SQLGlot; To compile our AST we swapped any metaclasses for __init_subclass__ in which we dynamically set some attributes in it like so:

  class Expression:                                                                                                                                                                                                                                  
      arg_types: ClassVar[...] = {}                                                                                                                                                                                                      
                                                            
      def __init_subclass__(cls, **kwargs: object) -> None:                                                                                                                                                                                          
          cls.required_args = {k for k, v in cls.arg_types.items() if v}

  class Select(Expression):
      arg_types: ClassVar[...] = {...}

I introduced the InitSubclass primitive to allow for fine-grained control of when this gets called and moved it from ExtClassBuilder::__init__ (i.e allocate_class > CPyType_FromTemplate) to ExtClassBuilder::finalize

@tobymao
Copy link

tobymao commented Mar 4, 2026

@JukkaL thanks for the quick reviews so far, any chance we can get this reviewed as well so that we can unblock sqlglots compilation with mypyc?

this along with #20917, we'll soon have a new PR that fixes another vtable bug that sets getters and setters to the same slot

Copy link
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, looks good. I have just one minor code quality suggestion.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you remove the | None part here, which would let you skip the is not None check below? It looks like this is always not-None.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review, done! Thought I had went over exactly that and deemed None necessary, not sure why..

@JukkaL JukkaL merged commit f3e2a97 into python:master Mar 6, 2026
17 checks passed
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.

Subclass missing own class variables inside __init_subclass__

3 participants