Skip to main content

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

dataclassabc-0.0.13.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dataclassabc-0.0.13-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file dataclassabc-0.0.13.tar.gz.

File metadata

  • Download URL: dataclassabc-0.0.13.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for dataclassabc-0.0.13.tar.gz
Algorithm Hash digest
SHA256 461469b6bd113ce673a78910621e26d631c166ebdbf82c2a265c56394b66513e
MD5 052807fb7363888dc4e6ee91eec923c5
BLAKE2b-256 f1a93d37691b75d31d68dd0cb814d0c452cb1ef0f912119152bdc6d64144ae3f

See more details on using hashes here.

File details

Details for the file dataclassabc-0.0.13-py3-none-any.whl.

File metadata

  • Download URL: dataclassabc-0.0.13-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for dataclassabc-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 fa453adb91f077c21cacf09b2ecce00433ea9c49bb4835139f6ab7490f19f566
MD5 c912e98a90cee72441fe88de954e4152
BLAKE2b-256 30d4b4dfeb4d15649bf01641b85904daf70324748ac36271870484f9e239435f

See more details on using hashes here.

Supported by

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