You can create a AbstractDataclass class which guarantees this behaviour, and you can use this every time you have a situation like the one you described.

@dataclass 
class AbstractDataclass(ABC): 
    def __new__(cls, *args, **kwargs): 
        if cls == AbstractDataclass or cls.__bases__[0] == AbstractDataclass: 
            raise TypeError("Cannot instantiate abstract class.") 
        return super().__new__(cls)

So, if Identifier inherits from AbstractDataclass instead of from ABC directly, modifying the __post_init__ will not be needed.

@dataclass
class Identifier(AbstractDataclass):
    sub_tokens: List[str]

    @staticmethod
    def from_sub_tokens(sub_tokens):
        return SimpleIdentifier(sub_tokens) if len(sub_tokens) == 1 else CompoundIdentifier(sub_tokens)


@dataclass
class SimpleIdentifier(Identifier):
    pass


@dataclass
class CompoundIdentifier(Identifier):
    pass

Instantiating Identifier will raise TypeError but not instantiating SimpleIdentifier or CompountIdentifier. And the AbstractDataclass can be re-used in other parts of the code.

Answer from Jundiaius on Stack Overflow
🌐
Ivergara
ivergara.github.io › ABC-and-dataclasses.html
Having fun with dataclasses and abstract base classes - On data, programming, and technology
January 28, 2019 - In this article we’re going to explore how to combine dataclases with the abc and collections.abc modules of the standard library in Python. I’ll assume that you know/understand what abc, collections.abc and dataclases. With the last two one could get a lot of behavior for free! If you don’t know about abstract base classes then I strongly recommend to check articles like this and this, for abc, and abc.collections, respectivelly. Likewise, if you don’t know why dataclasses are interesting and about their advantages, you should check this other article.
Discussions

Provide a canonical way to declare an abstract class variable - Ideas - Discussions on Python.org
There’s a recent help post of Abstract variables in abc that asks about how an “abstract variable” can be declared such that it is required for a subclass to override the variable, to which @drmason13 replied: Although this approach of abusing an abstract property as an abstract variable ... More on discuss.python.org
🌐 discuss.python.org
10
October 28, 2024
DataClass through decorator vs base class
Decorators have been part of the language since 2003. dataclass's functionality could have been provided by a function or metaclass, but not a base class with the typical metaclass. I think its simpler to go the function route, and since that function takes a class the decorator form looks nice. If you hate it you can always do this instead: class A: a: str b: int A = dataclass(A) More on reddit.com
🌐 r/Python
22
35
May 12, 2023
Is there a such thing as declaring an attribute of an abstract class in Python?
You can do this with @property and @abstractmethod Py:3.3+ (@abstractproperty for earlier versions of python): from abc import ABC, abstractmethod class Base(ABC): @property @abstractmethod def name(self): pass class Concrete(Base): def __init__(self, name): self._name = name @property def name(self): return self._name In []: Base() Out[]: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In [56], line 1 ----> 1 Base() TypeError: Can't instantiate abstract class Base with abstract method name In []: Concrete("Name").name Out[]: 'Name' More on reddit.com
🌐 r/learnpython
24
9
February 9, 2023
Understanding Python's new Dataclasses
This was a good post but I still dont know when i would use these ? What real world situations would warrant the use of these? More on reddit.com
🌐 r/Python
92
414
July 3, 2018
🌐
GitHub
github.com › MichaelSchneeberger › dataclass-abc
GitHub - MichaelSchneeberger/dataclass-abc: A Python library that allows you to define abstract properties for dataclasses, bridging the gap between abstract base classes (ABCs) and dataclasses.
A Python library that allows you to define abstract properties for dataclasses, bridging the gap between abstract base classes (ABCs) and dataclasses. - MichaelSchneeberger/dataclass-abc
Starred by 17 users
Forked by 4 users
Languages   Python 100.0% | Python 100.0%
🌐
Python
docs.python.org › 3 › library › abc.html
abc — Abstract Base Classes
Source code: Lib/abc.py This module provides the infrastructure for defining abstract base classes(ABCs) in Python, as outlined in PEP 3119; see the PEP for why this was added to Python. (See also ...
🌐
Plain English
python.plainenglish.io › data-classes-abstraction-interfaces-in-python-ea107d235d3e
Data Classes, Abstraction, and Interfaces in Python | Python in Plain English
October 11, 2022 - In this article, I am going to ... view image in full size · Classes · from dataclasses import dataclass · They are a data-oriented variant of classes, usually preferred for storing data....
🌐
Python.org
discuss.python.org › ideas
Provide a canonical way to declare an abstract class variable - Ideas - Discussions on Python.org
October 28, 2024 - There’s a recent help post of Abstract variables in abc that asks about how an “abstract variable” can be declared such that it is required for a subclass to override the variable, to which @drmason13 replied: Although…
🌐
GeeksforGeeks
geeksforgeeks.org › python › abstract-classes-in-python
Abstract Classes in Python - GeeksforGeeks
An Abstract Base Class (ABC) defines methods that must be implemented by its subclasses, ensuring that the subclasses follow a consistent structure. ABCs allow you to define common interfaces that various subclasses can implement while enforcing ...
Published   September 3, 2025
Find elsewhere
🌐
Reddit
reddit.com › r/python › dataclass through decorator vs base class
r/Python on Reddit: DataClass through decorator vs base class
May 12, 2023 -

This is not a protest but a question about the principles and philosophy behind the decision to use decorators in Python. Although personally I don't like decorators, I do not object to their inclusion in Python.

I am happily open to hearing arguments FOR decorators, however it seems to me that, for example, the DataClass could have been implemented as a base class and that the use of decorator is more about adding a cool new feature.

My concern is that in the pursuit of cool, Python will go down the same death spiral as the monstrosity that Java has become.

So what are the good words for decorators in Python?

🌐
Python
peps.python.org › pep-0557
PEP 557 – Data Classes | peps.python.org
This PEP describes an addition to the standard library called Data Classes. Although they use a very different mechanism, Data Classes can be thought of as “mutable namedtuples with defaults”. Because Data Classes use normal class definition ...
🌐
Real Python
realpython.com › ref › stdlib › dataclasses
dataclasses | Python Standard Library – Real Python
In this example, the dataclasses module streamlines the creation and management of employee records. ... Learn how a Python dataclass reduces boilerplate, adds type hints and defaults, supports ordering and frozen instances, and still plays well with inheritance.
🌐
DataCamp
datacamp.com › tutorial › python-abstract-classes
Python Abstract Classes: A Comprehensive Guide with Examples | DataCamp
January 22, 2025 - You can make sure that any subclass, such as Dog or Cat, implements its implementation of the speak method, for instance, if you have an abstract class Animal with an abstract method speak. Your code becomes reliable and predictable as a result of this uniformity. Master Python for data science and gain in-demand skills.
🌐
Python
docs.python.org › 3 › library › dataclasses.html
dataclasses — Data Classes
3 weeks ago - Source code: Lib/dataclasses.py This module provides a decorator and functions for automatically adding generated special methods such as__init__() and__repr__() to user-defined classes. It was ori...
🌐
Rednafi
rednafi.com › python › banish state-mutating methods from data classes
Banish state-mutating methods from data classes | Redowan's Reflections
December 16, 2023 - When you decorate a class with the @dataclass decorator without changing any of the default parameters, Python automatically generates __init__, __eq__, and __repr__ methods. If you set @dataclass(order=True), it’ll also generate __lt__, __le__, __gt__, and __ge__ special methods that enable you to compare and sort the data class instances. All of this implicates that the construct was specifically designed to contain rich data that provides the means for you to create nice abstractions around lower-level primitives.
🌐
The Mail Archive
mail-archive.com › python-ideas@python.org › msg33086.html
[Python-ideas] Abstract dataclasses and dataclass fields
December 21, 2023 - I'm thinking that a field would be made abstract by passing `abstract=True` as an argument to `dataclasses.field()`. _______________________________________________ Python-ideas mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/TFDJDTM7ZOYKBOPAYSDCM3T7SYD2RIJL/ Code of Conduct: http://python.org/psf/codeofconduct/
🌐
DevPress
devpress.csdn.net › mongodb › 6304ae0cc67703293080d821.html
Abstract classes and metaclasses with dataclasses in python_python_芒果数据-MongoDB
An easier way to think about this, is to imagine that the signature of the __init__() method that dataclasses generates, actually looks like this: def __init__(self, name: str, quantity: int, description: str, *, _id: Optional[int] = None): In Python (not necessarily in 3.10 alone), the appearance of * in a function signifies that all the parameters that follow it are then declared as keyword-only arguments.
🌐
Medium
medium.com › swlh › python-dataclass-inheritance-finally-686eaf60fbb5
Python dataclass inheritance, finally ! | by Anis Campos
October 25, 2021 - So the reason is simple: the constructor, the __init__ method, that the dataclass conveniently generates for us is very straightforward: it simply takes the field in order of declaration and adds them one after the other in the method’s definition. Because in Python (initially, more about that later), default-valued arguments must always come after all positional arguments, the dataclass field declaration must also follow this logic and always define the fields without default value before any field with one.
Top answer
1 of 2
4

The correct solution is to abandon the DataclassMixin classes and simply make the abstract classes into dataclasses, like this:

@dataclass  # type: ignore[misc]
class A(ABC):
    a_field: int = 1

    @abstractmethod
    def method(self):
        pass

@dataclass  # type: ignore[misc]
class B(A):
    b_field: int = 2

@dataclass
class C(B):
    c_field: int = 3

    def method(self):
        return self

The reason for the failures is that, as explained in the documentation on dataclasses, the complete set of fields in a dataclass is determined when the dataclass is compiled, not when it is inherited from. The internal code that generates the dataclass's __init__ function can only examine the MRO of the dataclass as it is declared on its own, not when mixed in to another class.

It's necessary to add # type: ignore[misc] to each abstract dataclass's @dataclass line, not because the solution is wrong but because mypy is wrong. It is mypy, not Python, that requires dataclasses to be concrete. As explained by ilevkivskyi in mypy issue 5374, the problem is that mypy wants a dataclass to be a Type object and for every Type object to be capable of being instantiated. This is a known problem and awaits a resolution.

The behavior in the question and in the solution is exactly how dataclasses should behave. And, happily, abstract dataclasses that inherit this way (the ordinary way) can be mixed into other classes willy-nilly no differently than other mix-ins.

2 of 2
1

Putting the mixin as the last base class works without error:

@dataclass
class ADataclassMixin:
    a_field: int = 1


class A(ABC, ADataclassMixin):

    @abstractmethod
    def method(self):
        pass


@dataclass
class BDataclassMixin:  
    b_field: int = 2


class B(A, BDataclassMixin):

    def method(self):
        return self


o = B(a_field=5)
print((o.a_field, o.b_field))  # (5,2)
🌐
CodeSignal
codesignal.com › learn › courses › revisiting-oop-concepts-in-python › lessons › understanding-abstract-classes-and-abstract-methods-in-python
Understanding Abstract Classes and Abstract Methods in ...
Think of it as a blueprint for other classes. It often includes one or more abstract methods. A class that inherits from an abstract class must implement all its abstract methods. In Python, the abc (Abstract Base Classes) module provides tools for defining abstract base classes.
🌐
Real Python
realpython.com › ref › glossary › abstract-base-class
abstract base class (ABC) | Python Glossary – Real Python
In Python, an abstract base class (ABC) is a class that can’t be instantiated on its own and is designed to be a blueprint for other classes, allowing you to define a common interface for a group of related classes.