examples.02_value_ordering_same_class

Demonstrates how ordering between Value objects works within the same concrete subclass, and that the reflected comparison (__lt__) of the other object is not used when the left-hand operand implements ordering itself.

This ensures that ordering semantics are consistent and predictable: comparisons like x > y and x < y only rely on the appropriate methods from the same class, rather than accidentally invoking reflected operations.

Run directly:

python examples/02_value_ordering_same_class.py

Expected output:

x > y: True

 1"""Demonstrates how ordering between `Value` objects works **within** the same
 2concrete subclass, and that the reflected comparison (`__lt__`) of the other
 3object is *not* used when the left-hand operand implements ordering itself.
 4
 5This ensures that ordering semantics are consistent and predictable: comparisons
 6like `x > y` and `x < y` only rely on the appropriate methods from the same
 7class, rather than accidentally invoking reflected operations.
 8
 9Run directly:
10
11    python examples/02_value_ordering_same_class.py
12
13Expected output:
14
15    x > y: True
16"""
17
18import sys
19import pathlib
20
21# ---------------------------------------------------------------------------
22# Make repo root importable when running this file directly
23# ---------------------------------------------------------------------------
24sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
25
26from constrained_values import Value
27
28
29class BreakLtSame(Value[int]):
30    """A subclass of `Value[int]` that intentionally breaks `__lt__`.
31
32    This class is used to verify that when performing a comparison like
33    ``x > y``, the `Value` base class uses its own `__gt__` implementation
34    rather than falling back to the reflected ``__lt__`` on the other operand.
35
36    Attempting to use ``<`` with this class will raise a `RuntimeError` so
37    it’s obvious if the wrong comparison path is taken.
38    """
39
40    def __init__(self, v: int) -> None:
41        """Initialize a `BreakLtSame` instance.
42
43        Args:
44            v: The integer value to wrap.
45        """
46        super().__init__(v)
47
48    def __lt__(self, other) -> bool:  # pragma: no cover (sentinel)
49        """Deliberately raise an error if `__lt__` is called.
50
51        This sentinel ensures that the test only passes if the `Value`
52        base class handles `>` (greater than) comparisons internally,
53        without triggering this reflected method.
54        """
55        raise RuntimeError("Reflected __lt__ should not be used")
56
57
58def main() -> None:
59    """Run the ordering comparison demonstration.
60
61    Creates two instances of :class:`BreakLtSame` with different underlying
62    integer values, then compares them using ``>``.
63
64    The comparison should **not** call the subclass’s broken `__lt__`;
65    instead, it should use `Value.__gt__`, resulting in a successful and
66    correct comparison.
67
68    Prints:
69        The boolean result of `x > y`, which should be `True`.
70    """
71    x = BreakLtSame(2)
72    y = BreakLtSame(1)
73    print("x > y:", x > y)  # uses __gt__ on Value; won't call reflected __lt__
74
75
76if __name__ == "__main__":
77    main()
class BreakLtSame(constrained_values.value.Value[int]):
30class BreakLtSame(Value[int]):
31    """A subclass of `Value[int]` that intentionally breaks `__lt__`.
32
33    This class is used to verify that when performing a comparison like
34    ``x > y``, the `Value` base class uses its own `__gt__` implementation
35    rather than falling back to the reflected ``__lt__`` on the other operand.
36
37    Attempting to use ``<`` with this class will raise a `RuntimeError` so
38    it’s obvious if the wrong comparison path is taken.
39    """
40
41    def __init__(self, v: int) -> None:
42        """Initialize a `BreakLtSame` instance.
43
44        Args:
45            v: The integer value to wrap.
46        """
47        super().__init__(v)
48
49    def __lt__(self, other) -> bool:  # pragma: no cover (sentinel)
50        """Deliberately raise an error if `__lt__` is called.
51
52        This sentinel ensures that the test only passes if the `Value`
53        base class handles `>` (greater than) comparisons internally,
54        without triggering this reflected method.
55        """
56        raise RuntimeError("Reflected __lt__ should not be used")

A subclass of Value[int] that intentionally breaks __lt__.

This class is used to verify that when performing a comparison like x > y, the Value base class uses its own __gt__ implementation rather than falling back to the reflected __lt__ on the other operand.

Attempting to use < with this class will raise a RuntimeError so it’s obvious if the wrong comparison path is taken.

BreakLtSame(v: int)
41    def __init__(self, v: int) -> None:
42        """Initialize a `BreakLtSame` instance.
43
44        Args:
45            v: The integer value to wrap.
46        """
47        super().__init__(v)

Initialize a BreakLtSame instance.

Arguments:
  • v: The integer value to wrap.
Inherited Members
constrained_values.value.Value
value
def main() -> None:
59def main() -> None:
60    """Run the ordering comparison demonstration.
61
62    Creates two instances of :class:`BreakLtSame` with different underlying
63    integer values, then compares them using ``>``.
64
65    The comparison should **not** call the subclass’s broken `__lt__`;
66    instead, it should use `Value.__gt__`, resulting in a successful and
67    correct comparison.
68
69    Prints:
70        The boolean result of `x > y`, which should be `True`.
71    """
72    x = BreakLtSame(2)
73    y = BreakLtSame(1)
74    print("x > y:", x > y)  # uses __gt__ on Value; won't call reflected __lt__

Run the ordering comparison demonstration.

Creates two instances of BreakLtSame with different underlying integer values, then compares them using >.

The comparison should not call the subclass’s broken __lt__; instead, it should use Value.__gt__, resulting in a successful and correct comparison.

Prints:

The boolean result of x > y, which should be True.