Skip to main content

typedattr

build 3.7 status build 3.9 status coverage version

Deprecated:

  • Attribute parsing capabilities have been moved to typedparser
  • Other utilities have been moved to packg
  • This code and pypi package may be deleted at some point.

Typechecking and conversion utility for attrs

Parses a dictionary into an attrs instance. Contains other generic object, type and cache utilities.

Install

Requires python>=3.7

Note: 0.2 breaks some backwards compatibility. Use 0.1 or update your code.

pip install typedattr

Quickstart

Define the class hierarchy and parse the input using attrs_from_dict:

from attrs import define
from typing import Optional
from typedattr import attrs_from_dict

@define
class Cfg:
    foo: int = 12
    bar: Optional[int] = None

print(attrs_from_dict(Cfg, {"foo": 1, "bar": 2}))
# Cfg(foo=1, bar=2)


@define
class CfgNested:
    sub_cfg: Cfg = None

print(attrs_from_dict(CfgNested, {"sub_cfg": {"foo": 1, "bar": 2}}))
# CfgNested(sub_cfg=Cfg(foo=1, bar=2))

Features

  • Nested checking and conversion of python standard types
  • Supports old and new style typing (e.g. typing.List and list)
  • Supports positional and keyword arguments in classes
  • Can also typecheck existing attrs instances
  • Allows custom conversions, by default converts source type str to target type Path and int to float
  • Allows to redefine which objects will be recursed into, by default recurses into standard containers (list, dict, etc.)
  • @definenumpy decorator for equality check if the instances contains numpy arrays

Strict mode (default)

  • Convert everything to the target type, e.g. if the input is a list and the annotation is a tuple, the output will be a tuple
  • Raise errors if types cannot be matched, there are unknown fields in the input or abstract annotation types are used (e.g. Sequence)

Non-strict mode

Enabled by calling attrs_from_dict with strict=False

  • No conversion except for creating the attrs instance from the dict
  • Ignore silently if types cannot be matched or abstract annotation types are used
  • Unknown fields in the input will be added to the attrs instance if possible (see the hint below about slots)

Skip unknowns

Set skip_unknowns=True to ignore all unknown input fields.

Hints

The following behaviour stems from the attrs package:

  • New attributes cannot to be added after class definition to an attrs instance, unless it is created with @define(slots=False) Explanation
  • Untyped fields or "ClassVar" typed fields will be ignored by @attrs.define and therefore also by this library.

Other utilities in the package

  • Const: An alternative to enum.Enum for defining constants
  • cacheutils: Cache objects to disk / to memory
  • objutils: Various utilities like nested modification of dicts
  • Type definitions and other utilities

Install locally and run tests

Clone repository and cd into, then:

pip install -e .
pip install -U pytest pytest-cov pylint
pylint typedattr

# run tests for python>=3.7
python -m pytest --cov
pylint tests

# run tests for python>=3.9
python -m pytest tests tests_py39 --cov
pylint tests 
pylint tests_py39

Alternatives

This library should be useful for off-the-shelf typechecking and conversion of dicts to attrs instances.

For more complex or other related use cases there are many alternatives: cattrs, attrs-strict, pydantic, dataconf, omegaconf to name a few.

Metadata

Release files for typedattr 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typedattr 0.2.6
File Size Uploaded Provenance Actions
typedattr-0.2.6.tar.gz 23.6 kB Without provenance Details for typedattr-0.2.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for typedattr 0.2.6
File Interpreter ABI Platform Actions
typedattr-0.2.6-py3-none-any.whl Python 3 none any Details for typedattr-0.2.6-py3-none-any.whl

Total release size: 48.0 kB

Release files / typedattr-0.2.6.tar.gz

Download URL typedattr-0.2.6.tar.gz
Size 23.6 kB
Tags Source
SHA-256 checksum
How to use checksums
bcbc4db6c1d5cab84732cdf74aebb489abcbaa40dcc2ca3b4acb6fce62a98018
BLAKE2b-256 checksum
How to use checksums
2231a7af06aa5b585ce881e95dd737453a3d85fbf29b25d75f1f0c40372f90d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / typedattr-0.2.6-py3-none-any.whl

Download URL typedattr-0.2.6-py3-none-any.whl
Size 24.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
863cf7ea4fe5aea9f07bc650a004fc9d258075ae71b521bb01f03412b4e652cd
BLAKE2b-256 checksum
How to use checksums
d4091c478ad82068070203fd548e5a32232070be05b49d64b16acc713a06de8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for typedattr 0.2.6

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page