Type checking on properties in the Python API
Created by: paulromano
In lots of places in our Python API, we have classes with properties that perform some kind of validation on the
setter. Since many of these checks are similar across the API (make sure this is non-negative, make sure this an iterable of type x, etc.), it would make sense to implement the properties as descriptors that perform validation and can be composed. This pattern is discussed in Beazley's Python Cookbook (recipe 8.13). You can see the code examples here.
The first step would be to look through the API to see all the different types of checks we are doing and decide which can be formalized through descriptors.
My guess is that this could remove a substantial number of lines of code since it would take something like:
class Foo: @property def bar(self): return self._bar @bar.setter def bar(self, bar): # validation self._bar = bar
and turn it into something like:
class Foo(Validated): bar = CheckedAttribute(...)