Create a class that can be passed into `attr.ib`'s ``eq``, ``order``, and ``cmp`` arguments to customize field comparison. The resulting class will have a full set of ordering methods if at least one of ``{lt, le, gt, ge}`` and ``eq`` are provided. :param Optional[callable] e
(
eq=None,
lt=None,
le=None,
gt=None,
ge=None,
require_same_type=True,
class_name="Comparable",
)
| 12 | |
| 13 | |
| 14 | def cmp_using( |
| 15 | eq=None, |
| 16 | lt=None, |
| 17 | le=None, |
| 18 | gt=None, |
| 19 | ge=None, |
| 20 | require_same_type=True, |
| 21 | class_name="Comparable", |
| 22 | ): |
| 23 | """ |
| 24 | Create a class that can be passed into `attr.ib`'s ``eq``, ``order``, and |
| 25 | ``cmp`` arguments to customize field comparison. |
| 26 | |
| 27 | The resulting class will have a full set of ordering methods if |
| 28 | at least one of ``{lt, le, gt, ge}`` and ``eq`` are provided. |
| 29 | |
| 30 | :param Optional[callable] eq: `callable` used to evaluate equality |
| 31 | of two objects. |
| 32 | :param Optional[callable] lt: `callable` used to evaluate whether |
| 33 | one object is less than another object. |
| 34 | :param Optional[callable] le: `callable` used to evaluate whether |
| 35 | one object is less than or equal to another object. |
| 36 | :param Optional[callable] gt: `callable` used to evaluate whether |
| 37 | one object is greater than another object. |
| 38 | :param Optional[callable] ge: `callable` used to evaluate whether |
| 39 | one object is greater than or equal to another object. |
| 40 | |
| 41 | :param bool require_same_type: When `True`, equality and ordering methods |
| 42 | will return `NotImplemented` if objects are not of the same type. |
| 43 | |
| 44 | :param Optional[str] class_name: Name of class. Defaults to 'Comparable'. |
| 45 | |
| 46 | See `comparison` for more details. |
| 47 | |
| 48 | .. versionadded:: 21.1.0 |
| 49 | """ |
| 50 | |
| 51 | body = { |
| 52 | "__slots__": ["value"], |
| 53 | "__init__": _make_init(), |
| 54 | "_requirements": [], |
| 55 | "_is_comparable_to": _is_comparable_to, |
| 56 | } |
| 57 | |
| 58 | # Add operations. |
| 59 | num_order_functions = 0 |
| 60 | has_eq_function = False |
| 61 | |
| 62 | if eq is not None: |
| 63 | has_eq_function = True |
| 64 | body["__eq__"] = _make_operator("eq", eq) |
| 65 | body["__ne__"] = _make_ne() |
| 66 | |
| 67 | if lt is not None: |
| 68 | num_order_functions += 1 |
| 69 | body["__lt__"] = _make_operator("lt", lt) |
| 70 | |
| 71 | if le is not None: |
nothing calls this directly
no test coverage detected