Utility Classes :: IsOnlyOneOf
Overview
The IsOnlyOneOf() is related to the IsOneOf() class, but with a major difference:
IsOneof(IsIterable(), IsCallable())
IsOnlyOneOf(IsIterable(), IsCallable())
Where IsIterable() and IsCallable() are the IsIterable() and IsCallable() utility classes, respectively.
The first signature will accept the object if it's iterable, callable or both. In the second signature, however, the object must be either callable or iterable; if it's both iterable and callable, that's a no-go.
Details
-
If the object being checked by the
IsOnlyOneOfinstance does not match any of the provided conditions, or matches more than one of the provided conditions, a_TC_XorErrorwill be raised; thematched_condsattribute will be either 0 or 2, and theinnerattribute will be a_TC_TypeErrorinstance.This exception is to be caught and converted to a
TypeCheckErrorbefore it reaches the level of the user. -
IsOnlyOneOfobjects 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.All
IsOnlyOneOfinstances that compare equal with==will produce the same value when run through the built-inhash()function. -
The
IsOnlyOneOfclass has been given some intelligence about boolean equality, meaning that it will optimise the creation of certain instances. For instance:IsOnlyOneOf(IsOnlyOneOf(int, float), \ IsOnlyOneOf(str, set))
is equivalent to
IsOnlyOneOf(int, float, str, set)
In fact, the first will be coerced into the second, meaning that the two will compare and hash as equals.
-
Another coercion-related intelligence is this:
IsOnlyOneOf(int, int, float, float)
will be simplified to
IsOnlyOneOf(int, float)
-
The order in which the
IsOnlyOneOfinstance's conditions will be checked is undefined.