Comparable¶
This page documents the Comparable checker.
- class argscheck.comparable.Comparable(*args, lt=None, le=None, ne=None, eq=None, ge=None, gt=None, other_type=<class 'object'>, **kwargs)¶
Check if x correctly compares to other values using any of the following binary operators: <, <=, !=, ==, >= or >.
Comparison need not necessarily be between numeric types, as can be seen in the first example below.
Also, comparison shorthands can be used, as can be seen in the second example below.
- Parameters
lt – Optional[Any] – Check if x < lt.
le – Optional[Any] – Check if x <= le.
ne – Optional[Any] – Check if x != ne.
eq – Optional[Any] – Check if x == eq.
ge – Optional[Any] – Check if x >= ge.
gt – Optional[Any] – Check if x > gt.
other_type – Optional[Union[Type, Tuple[Type]]] – If provided, restricts the types to which x can be compared, e.g. other_type=int with ne=1.0 will raise a TypeError (because 1.0 is not an int).
- Example
from argscheck import check, Comparable # Check if a strict subset of {'a', 'b'} checker = Comparable(lt={'a', 'b'}) check(checker, set()) # Passes, an empty set is returned check(checker, {'a'}) # Passes, {'a'} is returned check(checker, {'a', 'b'}) # Fails, a ValueError is raised ({'a', 'b'} is equal to {'a', 'b'}) check(checker, 'a') # Fails, a TypeError is raised (< is not supported between set and str)
- Example
from argscheck import check, Comparable # Check if between 3 (inclusive) and 10 (exclusive) checker = 3 <= (Comparable < 10) check(checker, 7.0) # Passes, 7.0 is returned check(checker, 3) # Passes, 3 is returned check(checker, 1) # Fails, a ValueError is raised check(checker, 10) # Fails, a ValueError is raised (< is not supported between set and str)
Warning
When using a chained comparison shorthand like in the second example, one of the comparisons must always be surrounded by parentheses. i.e. the following will not work: 3 <= Comparable < 10.
This is due to how Python’s chained comparison handling is implemented.