A Python library that allows you to define abstract properties for dataclasses, bridging the gap between abstract base classes (ABCs) and dataclasses.
Project description
Dataclass-abc
A Python library that allows you to define abstract properties for dataclasses, bridging the gap between abstract base classes (ABCs) and dataclasses.
Installation
You can 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 a base class and implement it in a derived 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
Using the standard dataclass
decorator from the dataclasses
module to implement abstract properties will result in a TypeError, as shown below:
from abc import ABC, abstractmethod
from dataclasses import dataclass
@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')
Define mutable variables
You can define mutable abstract properties by using the @property
and @name.setter
decorators in the abstract class. The following example demonstrates how to define and set a mutable property:
Example
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
b = B(name='A')
# modify the mutable variable
b.name = 'B'
# Output will be b=B(name='B')
print(f'{b=}')
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for dataclassabc-0.0.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | acb9f975867fc9f1b4fc3ac387c05ccd4db76384bf72c4a2ace9ebdae93100c5 |
|
MD5 | 3ca0a26a9a9fa26051dcab15f0d9c6ca |
|
BLAKE2b-256 | 8f65a7fb74a4e7d8cff6b5efe4a7f317f91f01b53e429679c97d8afd9610d858 |