typed-descriptors: Descriptor classes with typechecking and validation
======================================================================
.. image:: https://img.shields.io/badge/python-3.8+-green.svg
:target: https://docs.python.org/3.8/
:alt: Python versions
.. image:: https://img.shields.io/pypi/v/typed-descriptors.svg
:target: https://pypi.python.org/pypi/typed-descriptors/
:alt: PyPI version
.. image:: https://img.shields.io/pypi/status/typed-descriptors.svg
:target: https://pypi.python.org/pypi/typed-descriptors/
:alt: PyPI status
.. image:: http://www.mypy-lang.org/static/mypy_badge.svg
:target: https://github.com/python/mypy
:alt: Checked with Mypy
.. image:: https://readthedocs.org/projects/typed-descriptors/badge/?version=latest
:target: https://typed-descriptors.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
.. image:: https://github.com/hashberg-io/typed-descriptors/actions/workflows/python-pytest.yml/badge.svg
:target: https://github.com/hashberg-io/typed-descriptors/actions/workflows/python-pytest.yml
:alt: Python package status
.. image:: https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square
:target: https://github.com/RichardLitt/standard-readme
:alt: 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 <https://www.python.org/dev/peps/pep-0484/>`_, and runtime typechecking is performed by the `typing-validation <https://github.com/hashberg-io/typing-validation>`_ library.
.. contents::
Install
-------
You can install the latest release from `PyPI <https://pypi.org/project/typed-descriptors/>`_ as follows:
.. code-block::
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 <https://www.python.org/dev/peps/pep-0484/>`_.
Runtime typechecking is performed by the `typing-validation <https://github.com/hashberg-io/typing-validation>`_ library.
Below is a simple example displaying all features listed above:
.. code-block:: python
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. <LICENSE>`_
Raw data
{
"_id": null,
"home_page": "https://github.com/hashberg-io/typed-descriptors",
"name": "typed-descriptors",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "",
"author": "hashberg",
"author_email": "sg495@users.noreply.github.com",
"download_url": "https://files.pythonhosted.org/packages/be/dc/0eaaf74622e7de37e0d10da3a7dae78fc67793c8c96f8db9bf3ab2b4b63c/typed-descriptors-1.2.2.post3.tar.gz",
"platform": null,
"description": "\r\ntyped-descriptors: Descriptor classes with typechecking and validation\r\n======================================================================\r\n\r\n.. image:: https://img.shields.io/badge/python-3.8+-green.svg\r\n :target: https://docs.python.org/3.8/\r\n :alt: Python versions\r\n\r\n.. image:: https://img.shields.io/pypi/v/typed-descriptors.svg\r\n :target: https://pypi.python.org/pypi/typed-descriptors/\r\n :alt: PyPI version\r\n\r\n.. image:: https://img.shields.io/pypi/status/typed-descriptors.svg\r\n :target: https://pypi.python.org/pypi/typed-descriptors/\r\n :alt: PyPI status\r\n\r\n.. image:: http://www.mypy-lang.org/static/mypy_badge.svg\r\n :target: https://github.com/python/mypy\r\n :alt: Checked with Mypy\r\n\r\n.. image:: https://readthedocs.org/projects/typed-descriptors/badge/?version=latest\r\n :target: https://typed-descriptors.readthedocs.io/en/latest/?badge=latest\r\n :alt: Documentation Status\r\n\r\n.. image:: https://github.com/hashberg-io/typed-descriptors/actions/workflows/python-pytest.yml/badge.svg\r\n :target: https://github.com/hashberg-io/typed-descriptors/actions/workflows/python-pytest.yml\r\n :alt: Python package status\r\n\r\n.. image:: https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square\r\n :target: https://github.com/RichardLitt/standard-readme\r\n :alt: standard-readme compliant\r\n\r\n\r\ntyped-descriptors is a small library of descriptor classes featuring static and runtime typechecking, runtime validation, and other useful features.\r\n\r\nStatic typechecking is compatible with `PEP 484 type hints <https://www.python.org/dev/peps/pep-0484/>`_, and runtime typechecking is performed by the `typing-validation <https://github.com/hashberg-io/typing-validation>`_ library.\r\n\r\n.. contents::\r\n\r\n\r\nInstall\r\n-------\r\n\r\nYou can install the latest release from `PyPI <https://pypi.org/project/typed-descriptors/>`_ as follows:\r\n\r\n.. code-block::\r\n\r\n pip install --upgrade typed-descriptors\r\n\r\n\r\nUsage\r\n-----\r\n\r\nClasses from the ``typed_descriptors`` module can be used to create statically typechecked descriptors which implement the following features:\r\n\r\n- attributes with runtime typechecking on write\r\n- attributes with validation on write\r\n- readonly attributes (set once)\r\n- cached properties\r\n\r\nTypechecking is compatible with `PEP 484 type hints <https://www.python.org/dev/peps/pep-0484/>`_.\r\nRuntime typechecking is performed by the `typing-validation <https://github.com/hashberg-io/typing-validation>`_ library.\r\n\r\nBelow is a simple example displaying all features listed above:\r\n\r\n.. code-block:: python\r\n\r\n from collections.abc import Sequence\r\n import networkx as nx # type: ignore\r\n from typed_descriptors import Attr, Prop\r\n\r\n class LabelledKn:\r\n r\"\"\"\r\n A complete graph :math:`K_n` with readonly size and mutable labels,\r\n where the NetworkX graph object is computed lazily and cached.\r\n \"\"\"\r\n\r\n n = Attr(int, lambda self, n: n >= 0, readonly=True)\r\n # type ^^^ validation ^^^^^^ ^^^^^^^^^^^^^ attribute is readonly\r\n\r\n labels = Attr(Sequence[str], lambda self, labels: len(labels) == self.n)\r\n # type ^^^^^^^^^^^^^ validation ^^^^^^^^^^^^^^^^^^^^^\r\n\r\n graph = Prop(nx.Graph, lambda self: nx.complete_graph(self.n))\r\n # type ^^^^^^^^ prop value ^^^^^^^^^^^^^^^^^^^^^^^^^\r\n\r\n def __init__(self, n: int, labels: Sequence[int]):\r\n # Setters for Attr instances take care of runtime typechecking and validation\r\n # for the arguments 'n' and 'labels' which have been passed to the constuctor.\r\n self.n = n\r\n self.labels = labels\r\n\r\n myobj = LabelledKn(3, [\"a\", \"b\", \"c\"]) # OK\r\n myobj.labels = (\"x\", \"y\", \"z\") # OK\r\n print(myobj.graph.edges) # OK: EdgeView([(0, 1), (0, 2), (1, 2)])\r\n\r\n myobj.x = 5 # AttributeError (readonly descriptor)\r\n myobj.y = [\"a\", \"b\", \"c\", \"d\"] # ValueError (lenght of y is not 3)\r\n myobj.y = 5 # TypeError (type of y is not 'Sequence')\r\n myobj.y = [2, 3, 5] # TypeError (type of y is not 'Sequence[str]')\r\n\r\n\r\nAPI\r\n---\r\n\r\nFor the full API documentation, see https://typed-descriptors.readthedocs.io/\r\n\r\n\r\nContributing\r\n------------\r\n\r\nPlease see `<CONTRIBUTING.md>`_.\r\n\r\n\r\nLicense\r\n-------\r\n\r\n`MIT \u00a9 Hashberg Ltd. <LICENSE>`_\r\n",
"bugtrack_url": null,
"license": "",
"summary": "Descriptor classes for typed attributes and properties.",
"version": "1.2.2.post3",
"project_urls": {
"Bug Tracker": "https://github.com/hashberg-io/typed-descriptors/issues",
"Homepage": "https://github.com/hashberg-io/typed-descriptors"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d169236f77caf52681e5865f25183a9823cdde4e2a3db1fadbe3cf471d7d70c6",
"md5": "5e2355c78c97b78e2107665dcc9dcbcb",
"sha256": "f7bce2b45bb4803632a20015059bb9689f831d595b33b448027a8b42903e3173"
},
"downloads": -1,
"filename": "typed_descriptors-1.2.2.post3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5e2355c78c97b78e2107665dcc9dcbcb",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 15548,
"upload_time": "2024-03-09T16:59:17",
"upload_time_iso_8601": "2024-03-09T16:59:17.611851Z",
"url": "https://files.pythonhosted.org/packages/d1/69/236f77caf52681e5865f25183a9823cdde4e2a3db1fadbe3cf471d7d70c6/typed_descriptors-1.2.2.post3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "bedc0eaaf74622e7de37e0d10da3a7dae78fc67793c8c96f8db9bf3ab2b4b63c",
"md5": "04437c7b08efd40f99b4b22e697ef90f",
"sha256": "2b0cd8efca3a0d476cd6792f76dfcf7da04c1be4043a1a286296677bbc395c90"
},
"downloads": -1,
"filename": "typed-descriptors-1.2.2.post3.tar.gz",
"has_sig": false,
"md5_digest": "04437c7b08efd40f99b4b22e697ef90f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 764270,
"upload_time": "2024-03-09T16:59:20",
"upload_time_iso_8601": "2024-03-09T16:59:20.319159Z",
"url": "https://files.pythonhosted.org/packages/be/dc/0eaaf74622e7de37e0d10da3a7dae78fc67793c8c96f8db9bf3ab2b4b63c/typed-descriptors-1.2.2.post3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-09 16:59:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hashberg-io",
"github_project": "typed-descriptors",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"tox": true,
"lcname": "typed-descriptors"
}