Skip to main content

Descriptor classes for typed attributes and properties.

Project description

Python versions PyPI version PyPI status Checked with Mypy Documentation Status Python package status standard-readme compliant

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

MIT © Hashberg Ltd.

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

typed-descriptors-1.1.0.tar.gz (757.7 kB view details)

Uploaded Source

Built Distribution

typed_descriptors-1.1.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file typed-descriptors-1.1.0.tar.gz.

File metadata

  • Download URL: typed-descriptors-1.1.0.tar.gz
  • Upload date:
  • Size: 757.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for typed-descriptors-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6e2083ec15163abdb099b4d3ee42d6c3376a96561c1207547018e9224da18181
MD5 6a2ff76fbfe50d29fbcb679674dc8ba8
BLAKE2b-256 23041b96694a166ce289585f09462e245dca879b6492b3be3884cd5530f21fd3

See more details on using hashes here.

File details

Details for the file typed_descriptors-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for typed_descriptors-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a7d2d9634f0ebf22cb22e77a5bc3acd09ee7d6edf0f3411fc5e5a167cbc57a0
MD5 8f77c5577ad077719eacfdbbc3e57c3e
BLAKE2b-256 ed109ba2fe1327dfc1446d5b102627957a7c85af45d2b4c03d6f1e3a18207836

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