Closed TylerYep closed 3 years ago
Hmm, this is a problem caused by both typing and dataclassy using metaclasses to do their magic. The TypeError
is a little cryptic, but what Python means is that both the metaclasses must be in the same inheritance chain, which obviously they are not.
>>> from typing import Mapping
>>> type(Mapping)
<class 'typing._SpecialGenericAlias'>) # recall that the type of a data class is DataClassMeta
typing.Generic
is just a regular class, so works, which is nice because generic data classes can be useful.
There are other examples of metaclasses in the standard library that would cause the same issue. For example, if you tried to create an abstract dataclass with collections.ABC
:
from abc import ABC
from dataclassy import dataclass
@dataclass
class AbstractDataClass(ABC):
pass
you would get the same error, because ABC
has the metaclass ABCMeta
.
Sadly, this is not easily solvable. In theory, you can merge the metaclasses by inheriting them to create a new metaclass, and then use the meta
option to tell dataclassy to use that. But this is often difficult, as it seems to be here (though I tried only very briefly). If you absolutely need to subclass Mapping
here, you can't use a dataclassy data class, sorry.
I encountered this issue while testing dataclassy. From reading the documentation, it looks like I should pass something into the meta= parameter to dataclasses? I'm not quite sure how to fix this. If this is easily solvable, maybe you could edit to the error message to explain what to do.
This was while testing the performance branch, but I suspect it applies to the current main branch as well.
or a simpler version:
Running this code gives:
Thanks!