Skip to main content

Add the ability to use properties with dataclasses.

Project description

Add the ability to use properties with dataclasses. This allows for better input validation on dataclasses.

Most of the code base is a direct copy from dataclasses with modifications. Unfortunately, dataclasses were not written in a way where you can extend the functionality easily. I changed the code to use a class based system, so the functionality could be extended. It’s still not fun to work with the dataclasses library.

Examples

from typing import Union
from dataclass_property import dataclass

@dataclass
class TimeDelta:
    hours: int = 0
    minutes: int = 0
    milliseconds: int = 0

    @property
    def seconds(self) -> int:  # default_factory is set to `int()` from the annotation
        return self._seconds

    @seconds.setter
    def seconds(self, value: Union[int, float]):
        if isinstance(value, float):
            mill = int((value % 1) * 1000)
            if mill:
                self.milliseconds = mill
        self._seconds = int(value)

td = TimeDelta(minutes=1)
td.seconds = 0.5
assert td.seconds == 0
assert td.milliseconds == 500

td = TimeDelta(seconds=0.1)
assert td.seconds == 0
assert td.milliseconds == 100

field_property example with custom defaults

from dataclass_property import dataclass, field_property

@dataclass
class Point:
    # X
    @field_property(default=1)
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, value: int):
        self._x = value

    # Y
    y: int = field_property(default=1)

    @y.getter
    def y(self) -> int:
        return self._y

    @y.setter
    def y(self, value: int):
        self._y = value

    # Z
    @field_property
    def z(self) -> int:
        return self._z

    z.default(5)

    @z.default_factory
    @classmethod
    def z(cls):
        return 5

    @z.setter
    def z(self, value: int):
        self._z = value

p = Point()
assert p.x == 1
assert p.y == 1
assert p.z == 5

p = Point(x=2, y=2, z=6)
assert p.x == 2
assert p.y == 2
assert p.z == 6

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_property-1.4.1.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

dataclass_property-1.4.1-py3-none-any.whl (60.9 kB view details)

Uploaded Python 3

File details

Details for the file dataclass_property-1.4.1.tar.gz.

File metadata

  • Download URL: dataclass_property-1.4.1.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for dataclass_property-1.4.1.tar.gz
Algorithm Hash digest
SHA256 cba5e5de79da2ef12bae56502011bda3c1b339b1b2ac84a41b3838ef42c8484f
MD5 045f46ba2fabf89892957d7b8a952e17
BLAKE2b-256 3f0aed012b94891bc9ef6cdfebe4f14f2af433479b4e238892d0485a793bbb14

See more details on using hashes here.

File details

Details for the file dataclass_property-1.4.1-py3-none-any.whl.

File metadata

  • Download URL: dataclass_property-1.4.1-py3-none-any.whl
  • Upload date:
  • Size: 60.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for dataclass_property-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 523d8af143830244a7e5b113f2b34af62d9dc469fea95c5a96cfb1c86a53cb7c
MD5 3a83629e99ae5813e532d24f637c0dea
BLAKE2b-256 32ed74f7cc13636a4c534c41bbab94a01ca43d37d88896d59d5f67bbd9292c37

See more details on using hashes here.

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