Git Product home page Git Product logo

Comments (2)

femtomc avatar femtomc commented on May 20, 2024

To anyone reading this, I solved this problem by defining my own metaclass:

metaclass_of_struct = type(pz.Struct)


class PytreeMeta(metaclass_of_struct):
    def __new__(cls, name, bases, namespace):
        if pz.Struct not in bases and not any(
            issubclass(base, pz.Struct) for base in bases
        ):
            bases = (pz.Struct,) + bases
        new_cls = type.__new__(cls, name, bases, namespace)
        return pz.pytree_dataclass(new_cls)

which my "overwrite" class for Struct declares as its metaclass.

from penzai.

danieldjohnson avatar danieldjohnson commented on May 20, 2024

Thanks for the question! As the error message suggests, this was actually an explicit design choice in Penzai to not automatically use @pz.pytree_dataclass for all subclasses. The main reasons:

  • If you have multiple levels of Python object inheritance, it can be non-obvious which ones are dataclasses and which ones aren't. For instance, if you subclass an ordinary Python dataclass, you don't get a dataclass back. Requiring the explicit decorator makes it clear which classes are dataclasses and which aren't.
  • Inheritance between dataclasses is really tricky; it can silently reorder the attributes that you've declared, or overwrite methods in unexpected ways. The @pz.pytree_dataclass decorator takes extra arguments that make sure you (and readers of your code) can tell what the decorator is going to do.

I'd advise against directly subclassing the metaclass of Struct in this way, and I can't guarantee that this will always work. Instead I'd recommend just adding the one-line @pz.pytree_dataclass decorator for each of the classes you want to define. Or, if you really want automatic inheritance of "pytree-dataclassness", you can just write your own metaclass from scratch, and not inherit from Struct at all. (Or you could use something like eqx.Module, which has implicit inheritance semantics.)

from penzai.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.