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
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.
🌐
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%
🌐
GitHub
github.com › python › cpython › issues › 102699
Add a Dataclass abstract base class for instance checking and type annotations · Issue #102699 · python/cpython
March 14, 2023 - Feature or enhancement Add the class dataclasses.Dataclass that supports instance and subclass checks using similar logic as dataclasses.is_dataclass, and blocks inheritance in __init_subclass__. (Edit: removed "has an attribute __datacl...
Author   NeilGirdhar
🌐
GitHub
github.com › MichaelSchneeberger › dataclass-abc › blob › master › README.md
dataclass-abc/README.md at master · 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. - MichaelSchneeberger/dataclass-abc
Author   MichaelSchneeberger
🌐
GitHub
github.com › python › mypy › issues › 5374
abstract dataclass inheritance gives `Only concrete class can be given where "Type[Abstract]" is expected` · Issue #5374 · python/mypy
April 29, 2018 - import abc from dataclasses import dataclass @dataclass # error: Only concrete class can be given where "Type[Abstract]" is expected class Abstract(metaclass=abc.ABCMeta): a: str @abc.abstractmethod def c(self) -> None: pass @dataclass class Concrete(Abstract): b: str def c(self) -> None: pass instance = Concrete(a='hello', b='world')
Author   ojii
🌐
GitHub
github.com › MichaelSchneeberger › dataclass-abc › releases
Releases · 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. - MichaelSchneeberger/dataclass-abc
Author   MichaelSchneeberger
🌐
Emilearthur
emilearthur.github.io › fastblog › python › oop › data › 2021 › 08 › 18 › DataClass-Inheritance-and-Composition.html
Dataclass - Inheritance and Composition | Emile’s Blog
August 18, 2021 - from dataclasses import dataclass from abc import ABC, abstractmethod from typing import Union @dataclass class Employee_(ABC): id: int name: str @abstractmethod def calculate_payroll(self): pass @dataclass class SalaryEmployee(Employee_): weekly_salary: Union[float, int] def calculate_payroll(self) -> Union[float, int]: return self.weekly_salary @dataclass class HourlyEmployee(Employee_): hours_worked: Union[float, int] hour_rate: Union[float, int] def calculate_payroll(self) -> Union[float, int]: return self.hours_worked * self.hour_rate @dataclass class CommissionEmployee(SalaryEmployee): commission: int def calculate_payroll(self) -> Union[float, int]: fixed = super().calculate_payroll() return fixed + self.commission
Find elsewhere
🌐
Python
peps.python.org › pep-0557
PEP 557 – Data Classes | peps.python.org
This PEP and the initial implementation were drafted in a separate repo: https://github.com/ericvsmith/dataclasses.
🌐
GitHub
github.com › python › mypy › issues › 11868
Abstract dataclasses don't work · Issue #11868 · python/mypy
Consider this snippet, let's name it mypy-abstract-dataclass.py: from dataclasses import dataclass from abc import ABC, abstractmethod @dataclass(frozen=True) class Something(ABC): xyz: int @abstractmethod def abc(self) -> int: pass I th...
🌐
GitHub
github.com › python › mypy › issues › 18962
dataclasses with generated __hash__ subclassing Hashable are considered abstract · Issue #18962 · python/mypy
February 8, 2025 - Dataclasses that are hashable (e.g. frozen=True) that extend any abc/protocol requiring __hash__ (e.g. collections.abc.Hashable) is considered abstract and gives an error when trying to be instantiated.
Author   ralismark
🌐
GitHub
github.com › rnag › dataclass-wizard
GitHub - rnag/dataclass-wizard: Simple, elegant, wizarding tools for interacting with Python's dataclasses.
The example below demonstrates recursive dataclasses with cyclic dependencies, following the pattern A -> B -> A -> B. For more details, see the Cyclic or "Recursive" Dataclasses section in the documentation. from __future__ import annotations # This can be removed in Python 3.10+ from dataclasses import dataclass from dataclass_wizard import JSONWizard @dataclass class A(JSONWizard): class _(JSONWizard.Meta): # Enable support for self-referential / recursive dataclasses recursive_classes = True b: 'B | None' = None @dataclass class B: a: A | None = None # Confirm that `from_dict` with a recursive, self-referential # input `dict` works as expected.
Starred by 223 users
Forked by 30 users
Languages   Python 99.7% | Makefile 0.3% | Python 99.7% | Makefile 0.3%
🌐
Python.org
discuss.python.org › ideas
Allow overriding (abstract) properties with fields - Ideas - Discussions on Python.org
November 20, 2022 - I often find myself wanting to do someting like this: from abc import abstractmethod from dataclasses import dataclass class HasLength: @property @abstractmethod def len(self) -> int: ... def __len__(s…
🌐
GitHub
github.com › ericvsmith › dataclasses
GitHub - ericvsmith/dataclasses · GitHub
This is an implementation of PEP 557, Data Classes. It is a backport for Python 3.6. Because dataclasses will be included in Python 3.7, any discussion of dataclass features should occur on the python-dev mailing list at https://mail.python...
Starred by 593 users
Forked by 51 users
Languages   Python 99.4% | Makefile 0.6%
🌐
GitHub
github.com › mypyc › mypyc › issues › 1061
Anything to do with dataclass and abstract / ABC classes seems broken: AttributeError: attribute '__dict__' of 'type' objects is not writable · Issue #1061 · mypyc/mypyc
July 14, 2024 - Combining the use of dataclass with an abstract base class in any number of ways seems fundamentally broken: Simple test case: from abc import ABC from dataclasses import dataclass @dataclass class Base(ABC): def block_type(cls) -> int: ...
Author   JohnstonJ
🌐
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.
🌐
PyPI
pypi.org › project › dataclass-abc
dataclass-abc
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Real Python
realpython.com › ref › stdlib › dataclasses
dataclasses | Python Standard Library – Real Python
The Python dataclasses module provides functionality for creating and working with user-defined data classes.
🌐
Towards Data Science
towardsdatascience.com › home › latest › abstract base classes and how to use them in your data science project
Abstract base classes and how to use them in your data science project | Towards Data Science
March 5, 2025 - At the beginning of a new project, a developer should pause and consider which methods are absolutely necessary for a class to operate properly. These methods should be declared abstract methods to avoid unexpected runtime errors in the future. In my opinion this is especially important in non-compiled languages such as Python, which can be notoriously fragile to these errors.
🌐
Python
bugs.python.org › issue39134
Issue 39134: can't construct dataclass as ABC (or runtime check as data protocol) - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/83315