class penzai.core.struct.AbstractStructMetaclass[source]#

Bases: ABCMeta

The metaclass for penzai.Struct and its (possibly abstract) subclasses.

In addition to ensuring that abstract methods are overridden before a class is instantiated, AbstractStructMetaclass also makes sure that the pytree_dataclass decorator was used. This means any instance of a subclass of Struct is guaranteed to be both a frozen dataclass and a PyTree node class.

It’s allowed to create a subclass of penzai.Struct without using pytree_dataclass, but this subclass will then be considered an abstract base class, and will not be possible to instantiate.

Note that pytree-ness is NOT inherited, so it is not sufficient to inherit from some other class that used pytree_dataclass; every concrete Struct type must be transformed directly before it can be instantiated.

Other than performing this safety check during initialization, we intentionally avoid actually changing the behavior of the class here, since metaclass trickery can be difficult to understand. We instead do the heavy lifting in pytree_dataclass. (This also means you are free to use pytree_dataclass without AbstractStructMetaclass, if you want.)


__call__(*args, **kwargs)

Creates a new instance of the class.

Inherited Methods

(expand to view inherited methods)

__init__(*args, **kwargs)


Return a type's method resolution order.


Register a virtual subclass of an ABC.

__call__(*args, **kwargs)[source]#

Creates a new instance of the class.

  • *args – Arguments to __init__.

  • **kwargs – Keyword arguments to __init__.


A new instance of the class.


TypeError – If pytree_dataclass wasn’t called on this class, indicating that it’s abstract (or that the user forgot @pytree_dataclass).