typed-descriptors: Descriptor classes with typechecking and validation
typed-descriptors is a small library of descriptor classes featuring static and runtime typechecking, runtime validation, and other useful features.
Static typechecking is compatible with PEP 484 type hints, and runtime typechecking is performed by the typing-validation library.
Install
You can install the latest release from PyPI as follows:
pip install --upgrade typed-descriptors
Usage
Classes from the typed_descriptors module can be used to create statically typechecked descriptors which implement the following features:
attributes with runtime typechecking on write
attributes with validation on write
readonly attributes (set once)
cached properties
Typechecking is compatible with PEP 484 type hints. Runtime typechecking is performed by the typing-validation library.
Below is a simple example displaying all features listed above:
from collections.abc import Sequence
import networkx as nx # type: ignore
from typed_descriptors import Attr, Prop
class LabelledKn:
r"""
A complete graph :math:`K_n` with readonly size and mutable labels,
where the NetworkX graph object is computed lazily and cached.
"""
n = Attr(int, lambda self, n: n >= 0, readonly=True)
# type ^^^ validation ^^^^^^ ^^^^^^^^^^^^^ attribute is readonly
labels = Attr(Sequence[str], lambda self, labels: len(labels) == self.n)
# type ^^^^^^^^^^^^^ validation ^^^^^^^^^^^^^^^^^^^^^
graph = Prop(nx.Graph, lambda self: nx.complete_graph(self.n))
# type ^^^^^^^^ prop value ^^^^^^^^^^^^^^^^^^^^^^^^^
def __init__(self, n: int, labels: Sequence[int]):
# Setters for Attr instances take care of runtime typechecking and validation
# for the arguments 'n' and 'labels' which have been passed to the constuctor.
self.n = n
self.labels = labels
myobj = LabelledKn(3, ["a", "b", "c"]) # OK
myobj.labels = ("x", "y", "z") # OK
print(myobj.graph.edges) # OK: EdgeView([(0, 1), (0, 2), (1, 2)])
myobj.x = 5 # AttributeError (readonly descriptor)
myobj.y = ["a", "b", "c", "d"] # ValueError (lenght of y is not 3)
myobj.y = 5 # TypeError (type of y is not 'Sequence')
myobj.y = [2, 3, 5] # TypeError (type of y is not 'Sequence[str]')
API
For the full API documentation, see https://typed-descriptors.readthedocs.io/
Contributing
Please see CONTRIBUTING.md.
License
Metadata
Release files for typed-descriptors 1.2.2.post4
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 |
|---|---|---|---|---|
| typed_descriptors-1.2.2.post4.tar.gz | 174.0 kB | Without provenance | Details for typed_descriptors-1.2.2.post4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| typed_descriptors-1.2.2.post4-py3-none-any.whl | Python 3 | none | any | Details for typed_descriptors-1.2.2.post4-py3-none-any.whl |
Total release size: 189.7 kB
Release files / typed_descriptors-1.2.2.post4.tar.gz
| Download URL | typed_descriptors-1.2.2.post4.tar.gz |
|---|---|
| Size | 174.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
35d05e75312bfa23a9cc1c5d6274b672e667fb64be112b628db550313737c65a
|
|
BLAKE2b-256 checksum How to use checksums |
2966eec887dde5f7bac2a75e433cce0d5876d78a17dc1b34a5dafd4663a8b0fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.3
|
Release files / typed_descriptors-1.2.2.post4-py3-none-any.whl
| Download URL | typed_descriptors-1.2.2.post4-py3-none-any.whl |
|---|---|
| Size | 15.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
67e3d00337ce458596b0f01134659bdd198acc72919db2e962c305f39735539d
|
|
BLAKE2b-256 checksum How to use checksums |
3c80e477c742b05562cc460e492013e03bb054609669e892511f81b4d7743c8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.3
|
Security for typed-descriptors 1.2.2.post4
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.