Skip to main content

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 or CapitalCity in the example). This class is used for pattern matching, e.g. using isinstance method.
  • impl - an implementation class implements the abstract properties. (see CityImpl or CapitalCityImpl in the example). This class is decorated with dataclass and resolve_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)

Uploaded Source

Built Distribution

dataclass_abc-0.0.1-py3-none-any.whl (6.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page