biqqles / dataclassy

A fast and flexible reimplementation of data classes
https://pypi.org/project/dataclassy
Mozilla Public License 2.0
81 stars 9 forks source link

Decorating classes which derive from ABC throws an exception #51

Closed thisisrandy closed 3 years ago

thisisrandy commented 3 years ago
In [1]: from dataclassy import dataclass

In [2]: from abc import ABC, ABCMeta

In [3]: @dataclass
   ...: class AbstractBase(ABC):
   ...:     pass
   ...: 
---------------------------------------------------------------------------
...
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

In [4]: @dataclass
   ...: class AbstractBase(metaclass=ABCMeta):
   ...:     pass
   ...: 

In [5]: AbstractBase()
Out[5]: AbstractBase()

This isn't a bug, but I think the solution is probably going to be non-obvious to most of your users, who will likely not have played with metaclasses before (this was me before today). As such, I thought I'd mention it explicitly in the docs.

Two things:

  1. Your table raw formatting isn't very pretty anymore. Sorry. I at least ran it thought the markdown tables generator to get the spacing even.
  2. I'm unsure of the best wording for the first column, so please edit if you believe it is unclear or inaccurate. You may also think this belongs somewhere else entirely, which is fine by me.
biqqles commented 3 years ago

Thanks for this. I agree the docs are unclear. However, the first column is a bit misleading as currently written as for many metaclasses you have to use @dataclass(meta=MetaClass) instead. However, making a data class an abstract class is such a common scenario that this is still a very valuable thing to add.

thisisrandy commented 3 years ago

Your wording is much clearer, thanks. It's often difficult to concisely distill new concepts, so your edit is exactly what I was hoping for.