Add properties for defining aliases and type enforcement.

Review Request #10495 — Created April 2, 2019 and submitted — Latest diff uploaded

Information

Djblets
release-2.0.x

Reviewers

This introduces a couple of specialized properties that help with
deprecating older APIs and enforcing type safety with minimal effort.

AliasProperty forwards on any property access (sets and gets) to
another attribute, optionally converting the new or existing value, and
optionally emitting a deprecation warning.

TypedProperty takes one or more value types that are allowed to be
set, and checks any new values against those types, optionally allowing
or disallowing None values.

These make use of a new function, get_descriptor_attr_name(), which
can locate the attribute of a descriptor/property in a class and return
the attribute name. It's used by these properties for error reporting
and for setting an attribute on a class instance based on the property's
name.

Unit tests passed.

Made use of these in some upcoming changes.

Diff Revision 2 (Latest)

orig
1
2

Commits

First Last Summary ID Author
Add properties for defining aliases and type enforcement.
This introduces a couple of specialized properties that help with deprecating older APIs and enforcing type safety with minimal effort. `AliasProperty` forwards on any property access (sets and gets) to another attribute, optionally converting the new or existing value, and optionally emitting a deprecation warning. `TypedProperty` takes one or more value types that are allowed to be set, and checks any new values against those types, optionally allowing or disallowing `None` values. These make use of a new function, `get_descriptor_attr_name()`, which can locate the attribute of a descriptor/property in a class and return the attribute name. It's used by these properties for error reporting and for setting an attribute on a class instance based on the property's name.
7ad4bdb7b2fbb3f495227bd843019e62c93ef845 Christian Hammond
djblets/util/properties.py
djblets/util/tests/test_properties.py
docs/djblets/coderef/index.rst
Loading...