field-properties
Properties for dataclass fields
Example
from dataclasses import dataclass
from field_properties import field_property, unwrap_property
@dataclass(frozen=True)
class Foo:
bar: int = field_property(default=0) # Same parameter than dataclasses.field
@field_property(bar) # Equivalent to @field_property(bar).getter
def get_bar(self) -> int:
# unwrap_property(self).bar is equivalent to self._bar
# but it's type-checked and linter-friendly
return unwrap_property(self).bar
# When not declared, getter, setter and deleter are generated like the following:
# @field_property(bar).setter
# def set_bar(self, value: int):
# unwrap_property(self).bar = value
assert repr(Foo()) == repr(Foo(0)) == "Foo(bar=0)"
How does it works?
When a dataclass field has a default value, this value is assigned as a class attribute. field_property use this mechanism and create a field with a property as default value.
If a default value/factory is registered with field_property, the property setter will be called with it in __init__.
Default getter/setter/deleter
field_property generates default getter/setter/deleter as simple wrappers around an instance attribute whose name is the field name prefixed with an underscore _. unwrap_property allows accessing this attribute in a type-checked/linter-friendly way.
By the way, if all the getter/setter/deleter are declared (and thus not generated), the protected attribute will not be created.
Overriding
Field properties can be overridden, but the dataclass field must be overridden too — this is because a new field must be created, as property is declared as its default value (see previous section).
from dataclasses import dataclass
from field_properties import field_property, unwrap_property
@dataclass
class Foo:
bar: int = field_property(default=0)
@field_property(bar)
def get_bar(self):
return unwrap_property(self).bar + 1
class Foo2(Foo):
bar: int = field_property(default=0) # field property must be overridden
# field_property(inherit=True) is a shortcut to override a field
# and reusing all it's arguments
@field_property(bar)
def get_bar(self):
return unwrap_property(self).bar + 2
assert Foo() == 1
assert Foo2() == 2
In fact, because field is redeclared, it's also possible to override normal fields with a field property
from dataclasses import dataclass
from field_properties import field_property
@dataclass
class Foo:
bar: int = 0
class Foo2(Foo):
bar: int = field_property(default=1)
assert Foo2() == 1
Raw property
field_property comes with a default implementation for its getter/setter/deleter. This can be turned off with raw=False parameter. Here is an example of a read_only field:
from dataclasses import dataclass
from field_properties import field_property
@dataclass
class Foo:
bar: int = field_property(init=False, raw=True)
@field_property(bar)
def get_bar(self) -> int:
return 0
assert Foo().bar == 0
assert str(Foo()) == "Foo(bar=0)"
try:
Foo().bar = 1
except AttributeError:
assert True
else:
assert False
PEP 614
Decorator syntax @field_property(bar).setter is only valid in Python 3.9. Previous version can use the following hack:
from dataclasses import dataclass
from field_properties import field_property
@dataclass
class Foo:
bar: int = field_property()
def set_bar(self, value: int):
...
field_property(bar).setter(set_bar)
Metadata
Release files for field-properties 0.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 | |
|---|---|---|---|---|
| field-properties-0.1.tar.gz | 5.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| field_properties-0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.9 kB
Release files / field-properties-0.1.tar.gz
| Download URL | field-properties-0.1.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7be49d9532732f31f70cb6c0b8b860589c6d85dcdd10cdd3f9d27182adae7aa8
|
|
BLAKE2b-256 checksum How to use checksums |
2da79796fc7a39276298ec8a3121d3d6e45803ec6b2140ad7772efa43ed6cfa1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
|
Release files / field_properties-0.1-py3-none-any.whl
| Download URL | field_properties-0.1-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
39b9bc6893dc363f671f27bd3ed8b78597c00d0696e6e24f1093351d048df31d
|
|
BLAKE2b-256 checksum How to use checksums |
d4d05ee81b51e3108d679bcf98124e5a353efaababd47dd337b79912aa6c5e38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
|
Security for field-properties 0.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.