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
Metadata
Release files for dataclass-property 1.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dataclass_property-1.4.1.tar.gz | 12.6 kB | Without provenance | Details for dataclass_property-1.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dataclass_property-1.4.1-py3-none-any.whl | Python 3 | none | any | Details for dataclass_property-1.4.1-py3-none-any.whl |
Total release size: 73.5 kB
Release files / dataclass_property-1.4.1.tar.gz
| Download URL | dataclass_property-1.4.1.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cba5e5de79da2ef12bae56502011bda3c1b339b1b2ac84a41b3838ef42c8484f
|
|
BLAKE2b-256 checksum How to use checksums |
3f0aed012b94891bc9ef6cdfebe4f14f2af433479b4e238892d0485a793bbb14
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / dataclass_property-1.4.1-py3-none-any.whl
| Download URL | dataclass_property-1.4.1-py3-none-any.whl |
|---|---|
| Size | 60.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
523d8af143830244a7e5b113f2b34af62d9dc469fea95c5a96cfb1c86a53cb7c
|
|
BLAKE2b-256 checksum How to use checksums |
32ed74f7cc13636a4c534c41bbab94a01ca43d37d88896d59d5f67bbd9292c37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for dataclass-property 1.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.