Class Reference
This module contains methods for adding properties to dataclasses that provides the ability to add strong type checking, control the internal storage type of a property, enforce assign-before-use policies, validator callbacks, etc.
The work is based on the pattern presented in 9.21 Avoiding Repetitive Property Methods from the book O’Reilly Python Cookbook, 3rd Edition.
We implemented this instead of using dataclass
due to the limitations in dataclass for strong type checking, internal storage type-casting, etc.
A class definition using StronglyTypedProperty might look like:
1class MyClass(object):
2 section_root = typed_property("section_root", (str), default=None)
3 raw_option = typed_property("raw_option", tuple, default=(None,None), validator=value_len_eq_2)
Please see the Quick Start guide for some examples to get started using Strongly Typed Property.
Class Guide
- class stronglytypedproperty.StronglyTypedProperty.NO_VALIDATOR[source]
Bases:
objectInternal class used to indicate
validatoris not set.
- class stronglytypedproperty.StronglyTypedProperty.SENTINEL[source]
Bases:
objectInternal class used to indicate
defaultis not set.
- stronglytypedproperty.StronglyTypedProperty.strongly_typed_property(name: str, expected_type=(<class 'int'>, <class 'str'>), default=<class 'stronglytypedproperty.StronglyTypedProperty.SENTINEL'>, default_factory=<function <lambda>>, req_assign_before_use=False, internal_type=None, validator=<class 'stronglytypedproperty.StronglyTypedProperty.NO_VALIDATOR'>, transform=None)[source]
Implements a strongly typed property in a class using the pattern “9.21 Avoiding Repetitive Property Methods” from the O’Reilly Python Cookbook, 3rd Edition.
- Parameters:
name (str) – The name of the property to create.
expected_type (type,tuple) – The type or a tuple of types enumerating allowable types to be assigned to the property. Default is
(int,str)default – A default value to be assigned to the tuple. Default is
None. This will be assigned without type checking.default_factory – Default factory method. This must be callable but is used when we need a complex type that can’t use
deepcopy. Default:lambda: None.req_assign_before_use (bool) – If
Truethen raise an exception if the value is used before assigned. Otherwise, the default value is used. Default:Falseinternal_type (<type>) – Sets the
<type>that the value is stored as (via typecast) internally. This is done during assignment.validator (func) – A special validation function that can be called during assignment to provide additional checks such as list size, allowable values, etc. If the validator’s return value is truthy the check suceeds, otherwise the check has failed and a
ValueErrorwill be raised. Default=None (i.e., no extra validation).transform (func) – A function that can be used to transform the value before assignment.
- Raises:
TypeError – if the assigned value is of the wrong type on assigmment.
ValueError – if a validator is provided and the check fails (is Falsy).
UnboundLocalError – If
req_assign_before_useis True and an attempt to read the property is made before it’s been assigned.