Dataclass-ABC
Dataclass-ABC is a Python library that bridges the gap between abstract base classes (ABCs) and dataclasses. It allows you to define and automatically implement abstract properties in dataclasses when these properties are overridden by fields.
Features
- Abstract properties: Implement abstract properties by inheritence using dataclasses.
- Trait Behavior: Allows to reproduce Trait-like behavior (as in Scala or Rust) using Python ABC classes.
Installation
Install Dataclass-ABC using pip:
pip install dataclassabc
Usage
The dataclassabc decorator enables the use of abstract properties within dataclasses.
It resolves abstract properties defined in an abstract base class (ABC) and enforces their implementation through fields in the derived dataclass.
Example
Here's how you can define an abstract property in an abstract class and implement it in a dataclass:
from abc import ABC, abstractmethod
from dataclassabc import dataclassabc
# Define an abstract base class with an abstract property
class A(ABC):
@property
@abstractmethod
def name(self) -> str: ...
# Use the dataclassabc decorator to implement the abstract property in a dataclass
@dataclassabc(frozen=True)
class B(A):
# Implementing the abstract property 'name'
name: str
# Works as expected
b1 = B(name='A')
# TypeError: B.__init__() missing 1 required positional argument: 'name'
b2 = B()
Define mutable variables
The dataclassabc library also supports defining mutable abstract properties. Use the @property decorator alongside a setter to define mutable properties in the abstract class.
from abc import ABC, abstractmethod
from dataclassabc import dataclassabc
class A(ABC):
@property
@abstractmethod
def name(self) -> str: ...
@name.setter
@abstractmethod
def name(self, val: str): ...
@dataclassabc
class B(A):
name: str
Comparison with the standard dataclass
Here are known issues when using the standard dataclass decorator in combination with abc library:
-
TypeError: "Can't instantiate abstract class" when trying to override an abstract property with a dataclass field.
from abc import ABC, abstractmethod from dataclasses import dataclass class A(ABC): @property @abstractmethod def name(self) -> str: ... @dataclass(frozen=True) class B(A): name: str # TypeError: Can't instantiate abstract class B without an implementation for abstract method 'name' b = B(name='A')
-
Unexpected Default Value
<property object at ...>when using dataclass optionslots=True.from abc import ABC, abstractmethod from dataclasses import dataclass class A(ABC): @property @abstractmethod def name(self) -> str: ... @dataclass(slots=True) class B(A): name: str # No exception is raised when name is not provided b = B() # The output will be <property object at ...> print(b.name)
-
TypeError: "Non-default argument follows default argument" when using dataclass option
slots=True.from abc import ABC, abstractmethod from dataclasses import dataclass class A(ABC): @property @abstractmethod def name(self) -> str: ... @dataclass(slots=True) class B(A): name: str age: int # TypeError: non-default argument 'age' follows default argument 'name' b = B(age=12, name='A')
Metadata
Release files for dataclassabc 0.0.18
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dataclassabc-0.0.18.tar.gz | 8.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dataclassabc-0.0.18-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.7 kB
Release files / dataclassabc-0.0.18.tar.gz
| Download URL | dataclassabc-0.0.18.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3de0c9c86066d9151d7dc1c5cba71d03b679904182cd7975fb80a12b7c63f791
|
|
BLAKE2b-256 checksum How to use checksums |
2c2347d32c0a05c55792b845d609ae02ad8ca242deac48139cb98c09f7f06315
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Release files / dataclassabc-0.0.18-py3-none-any.whl
| Download URL | dataclassabc-0.0.18-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a8538d366b38b812f1f1d08bd5d13548896478e377722a07eeeb6dddc240ff67
|
|
BLAKE2b-256 checksum How to use checksums |
8310b37db9c39e4e4d9eebfce491a67db3f715759766e99334184cf219a7e62e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|