StronglyTypedProperty

TL;DR

StronglyTypedProperty provides a Data Class like interface with the added ability to perform strong type checking as well as value checking with every assignment to a StronglyTypedProperty class property.

StronglyTypedProperty provides the following validation capabilities:

  • Restrict assigned data to a list of allowable types.

  • Specify the internal storage type of the property.

  • Provide value checking on assignment in addition to type checking.

Overview

Python is one of the most popular programming langauges in the world and it is very powerful in its ease of use and flexibility. One of the characteristics that makes Python both easy to use and flexible is its typing model. Generally speaking, part of this flexibility comes from how Python represents everything as objects and allows a variable to be assigned to anything, even if the type of the new value differs from an existing value when overwriting the contents of a variable. For example:

Example of assignment flexibility in Python
1 myvar = 1
2 type(myvar)
3 # >>> <class 'int'>
4 myvar = []
5 type(myvar)
6 # >>> <class 'list'>
7 myvar = {}
8 type(myvar)
9 # >>> <class 'dict'>

In this small example, we can assign our variable myvar to store different data types and Python happily allows this.

Sometimes though, we need to enforce that a value meets certain type or even value restrictions because the functions and methods in our code expect to see certain things. Say, if our code wishes to check for the keys in a dict by calling myvar.keys() but myvar is storing an int then the call to keys will fail and throw an error.

Of course we wish the error to be thrown when we try and use the value incorrectly but an error like this can be time consuming to debug in a complex application because we must track down the place in the code that the incorrect value was assigned to the variable. Often these situations occur when we encounter corner cases – some of them we aren’t even aware of until we see the error in operation. These fixes can be time consuming because the error manifests at the time of use rather than at assignment.

The StronglyTypedProperty class provides a sort of strong typing capability for class properties. Based on the Data Class concept StronglyTypedProperty allows configurable type checking and validation to be performed every time a value is assigned to a StronglyTypedProperty property in a class.

Though not really using Design by Contract directly, StronglyTypedProperty does borrow from the concept since it can be used to enforce both type and value checking.

When to use StronglyTypedProperty

StronglyTypedProperty adds overhead every time a property is assigned. This overhead could be considerable depending on how much validation is being done – especially when value checking is done in addition to type compatibility checking. It is important to take this into consideration when choosing to use StronglyTypedProperty.

Often the overall performance impact is negligable and/or the additional execution time is not as important as having errors detected on assignment rather than on use. For example, it can be difficult to trace a def-use chain to identify some corner case that causes a variable to take on the wrong data when the error manifests some time later in execution when a particular value or type is expected by a consumer and not found. Logic errors are harder to debug and cost more in developers’ time, concentration, and labour charging.

Reasons to use StronglyTypedProperty:

  • You wish to validate type and/or values of properties on assignment to ease debugging.

  • The performance cost of this overhead is not prohibitive for the application.

  • The property is assigned / overwritten relatively infrequently.

When to NOT use StronglyTypedProperty:

  • The property in question is frequently assigned, such as inside an inner loop.

  • Your application is performing some real-time operation.

Test Coverage

Coverage Report