Utility Classes :: IsAllOf
Overview
The IsAllOf class is used to specify that the object must meet all of any number of criterion, e.g., be an instance of certain types or user-defined classes, or be validated by other extension classes [1].
The IsAllOf() constructor must be passed at least two ([2]) distinct ([3]) types, user-defined classes or registered extension classes. For example:
@accepts(int, int, IsAllOf(Red, Green))
def my_func(a, b, c):
pass
The following code snippet matches the signature:
class Christmas_tree(Red, Green): pass oh_tannenbaum = Christmas_tree() my_func(3, 3, oh_tannenbaum)
The requirement that there are two distinct conditions stems from the fact that the following call is almost certainly an error:
IsAllOf(Red, Red)
Details
-
If the object being checked by the
IsAllOfinstance does not match all of the provided conditions, a_TC_TypeErrorwill be raised; therightattribute will be theIsAllOfinstance, and thewrongattribute will be the type of the object. [4]This exception is to be caught and converted to a
TypeCheckErrorbefore it reaches the level of the user. -
IsAllOfobjects will only compare equal (i.e.,==) if all the conditions in the left-hand side are found in the right-hand side, and vice versa [5].All
IsAllOfinstances that compare equal with==will produce the same value when run through the built-inhash()function [6]. -
The
IsAllOfclass has been given some intelligence about boolean equality, meaning that it will optimise the creation of certain instances. For instance:IsAllOf(IsAllOf(A, B), IsAllOf(C, D))
is equivalent to
IsAllOf(A, B, C, D)
In fact, the first will be coerced into the second, meaning that the two will compare and hash as equals [5, 6].
-
Another coercion-related intelligence is this:
IsAllOf(int, int, float, float)
will be simplified to
IsAllOf(int, float)
Again, these two will compare and hash as equals [5, 6]. To expand on the boolean-equality example:
IsAllOf(IsAllOf(A, B), IsAllOf(B, C))
is equivalent to
IsAllOf(A, B, C)
-
The order in which the
IsNoneOfinstance's conditions will be checked is undefined.