Dataclasses
In Chapter 4, Pythonic Design Patterns, we already saw the dataclasses module, which makes it possible to implement easy type hinting and even enforce some structure in your classes.
Now let’s look at how we can implement our own version using a metaclass. The actual dataclasses module mostly relies on a class decorator, but that is no issue. Metaclasses can be seen as a more powerful version of a class decorator, so they will work fine. With metaclasses, you can use inheritance to reuse them, or make the class inherit other classes, but above all, they allow you to modify the class object, instead of the instance with decorators.
The dataclasses module has several tricks up its sleeve that are non-trivial to replicate. Beyond adding documentation and some utility methods, it also generates an __init__ method with a signature that matches the fields of the dataclass. Since the entire dataclasses module is roughly 1,300 lines, we will not get close with...