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

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.

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:

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

Standard dataclass

Here are known issues when using the standard dataclass decorator in combination with abc library:

  • AttributeError: "Property object has not setter"

    from abc import abstractmethod
    from dataclasses import dataclass
    
    class A:
        @property
        @abstractmethod
        def name(self) -> str:
            ...
    
    @dataclass(frozen=True)
    class B(A):
        name: str
    
    # AttributeError: property 'name' of 'B' object has no setter
    b = B(name='A')
    
  • TypeError: "Can't instantiate abstract class"

    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 with slots=True

    from abc import ABC, abstractmethod
    from dataclasses import dataclass
    
    class A(ABC):
        @property
        @abstractmethod
        def name(self) -> str:
            ...
    
    @dataclass(frozen=True, 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"

    from abc import ABC, abstractmethod
    from dataclasses import dataclass
    
    class A(ABC):
        @property
        @abstractmethod
        def name(self) -> str:
            ...
    
    @dataclass(frozen=True, slots=True)
    class B(A):
        name: str
        age: int
    
    # TypeError: non-default argument 'age' follows default argument 'name'
    b = B(age=12, name='A')
    

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.14.tar.gz (8.4 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.14-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dataclassabc-0.0.14.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dataclassabc-0.0.14.tar.gz
Algorithm Hash digest
SHA256 c302d9a9942a266e3c0411c4604e3dee6aef17101b88b32f2ef2a001f2a6793a
MD5 d4d975949931a3b7cc48d1c647f92bb1
BLAKE2b-256 2da8e291d944a5dcabd7700ade37bfd797be0252378e07c3a297ca655e3c37ed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dataclassabc-0.0.14-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dataclassabc-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 3504de2cf46f7a0dfb3f4cd8d25c90630a6a23fbf8c45d27daad2d77a525d629
MD5 87e3030fc7f8db816f2f4a06ad1578c8
BLAKE2b-256 79484f72c5ac2a6220d895424a836b590cd54a00fb71dcd18bd410ab5200a97f

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