Verified Commit c284b877 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add order comparison operators

parent e5254adb
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -226,6 +226,12 @@ if TYPE_CHECKING:
		# The remainder of a floor division with another quantity
		def __mod__(self, other: Quantity[U], /) -> Quantity[U]: ...

		# Quantities can be compared for order and equality
		def __lt__(self, other: Quantity[U], /) -> bool: ...
		def __le__(self, other: Quantity[U], /) -> bool: ...
		def __gt__(self, other: Quantity[U], /) -> bool: ...
		def __ge__(self, other: Quantity[U], /) -> bool: ...

else:
	# Although the runtime implementation does not use TypeVars, it is still generic to
	# allow type subscripting.
+17 −0
Original line number Diff line number Diff line
@@ -39,6 +39,18 @@ def test_modulus() -> None:
	assert (3600 @ Time.MS) % Time.S == 600 @ Time.MS


def test_order() -> None:
	assert (1 @ Time.S) > (999 @ Time.MS)
	assert (1 @ Time.S) >= (999 @ Time.MS)
	assert (1 @ Time.S) >= (1 @ Time.S)

	assert (999 @ Time.MS) < (1 @ Time.S)
	assert (999 @ Time.MS) <= (1 @ Time.S)
	assert (1 @ Time.S) <= (1 @ Time.S)

	assert (1 @ Time.S) != (2 @ Time.S)


if TYPE_CHECKING:
	def type_checks() -> None:
		# Checks for type checker false negatives
@@ -55,3 +67,8 @@ if TYPE_CHECKING:

		_ = (1 @ Time.S) // 2  # type: ignore[operator]
		_ = (1 @ Time.S) // 2.0  # type: ignore[operator]

		_ = (1 @ Time.S) > 10  # type: ignore[operator]
		_ = (1 @ Time.S) >= 10  # type: ignore[operator]
		_ = (1 @ Time.S) < 10  # type: ignore[operator]
		_ = (1 @ Time.S) <= 10  # type: ignore[operator]