Library that lets you define abstract properties in dataclasses.
Project description
Dataclass ABC
Library that lets you define abstract properties in dataclasses.
Installation
pip install dataclass-abc
Usage
from abc import ABC, abstractmethod
from dataclasses import dataclass
from dataclass_abc import resolve_abc_prop
class A(ABC):
@property
@abstractmethod
def val(self) -> str:
...
@resolve_abc_prop
@dataclass
class B(A):
val: str
Example
The example takes some code snippets from https://realpython.com/python-data-classes/ and implements them with abstract properties.
Design pattern
This library suggests the design pattern as implemented in the example:
- mixins - a mixin is an abstract class that implements data as abstract properties and methods based on the abstract properties.
- classes - an abstract class inherits from one or more mixins
(see
City
orCapitalCity
in the example). This class is used for pattern matching, e.g. usingisinstance
method. - impl - an implementation class implements the abstract properties.
(see
CityImpl
orCapitalCityImpl
in the example). This class is decorated withdataclass
andresolve_abc_prop
and should always be called through an initialize function. - init - initialize functions (or constructor functions) initialize an implementation class.
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
dataclass-abc-0.0.1.tar.gz
(2.2 kB
view hashes)
Built Distribution
Close
Hashes for dataclass_abc-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8800bcd7ca4562bdd158acc086e1e46dd4f59484870de5c6c5b8a7982d079673 |
|
MD5 | d61ba3f82df961f3b3bcf77e77a48900 |
|
BLAKE2b-256 | e70781d51d32a66d5380c445b754346f432c2eb72f10aade7ae9f6eb9424411e |