Skip to content

[mypyc] Fix __init_subclass__ running before ClassVar instantiations#20916

Open
VaggelisD wants to merge 1 commit intopython:masterfrom
VaggelisD:init_subclass_fix
Open

[mypyc] Fix __init_subclass__ running before ClassVar instantiations#20916
VaggelisD wants to merge 1 commit 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

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__

1 participant