Verified Commit 003fbb3c authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add normal multiplication and division operators

parent 0fa339f7
Loading
Loading
Loading
Loading
+14
Changes for kodo/quantities/__init__.py: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -194,6 +194,7 @@ from typing import TYPE_CHECKING
from typing import Generic
from typing import Self
from typing import TypeVar
from typing import overload

U = TypeVar("U", bound="QuantityUnit")

@@ -216,8 +217,15 @@ if TYPE_CHECKING:
		def __mul__(self, other: int|float, /) -> Quantity[U]: ...

		# Quantities can be divided by unitless values to produce a new quantity
		@overload
		def __truediv__(self, other: int|float, /) -> Quantity[U]: ...

		# Dividing by a unit of measurement coverts to a unitless value
		@overload
		def __truediv__(self, other: U) -> float: ...

		def __truediv__(self, other: object) -> float: ...  # type: ignore[misc]

		# Quantities can be divided by (into) quantities of the same unit to produce
		# a unitless value (the number of whole divisions of the quantity)
		def __floordiv__(self, other: Quantity[U], /) -> int: ...
@@ -261,9 +269,15 @@ class QuantityUnit(enum.Enum):
	def __rmatmul__(self, scalar: float|int) -> Quantity[Self]:
		return Quantity(self.value * scalar)

	def __rmul__(self, scalar: float|int) -> Quantity[Self]:
		return Quantity(self.value * scalar)

	def __rrshift__(self, quantity: Quantity[Self]) -> float:
		return self.value.__rtruediv__(quantity)  # type: ignore[operator]

	def __rtruediv__(self, quantity: Quantity[Self]) -> float:
		return self.value.__rtruediv__(quantity)  # type: ignore[operator]

	def __rfloordiv__(self, quantity: Quantity[Self]) -> int:
		return quantity // self.value  # type: ignore[operator,no-any-return]

+5
Changes for test.py: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -51,6 +51,11 @@ def test_order() -> None:
	assert (1 @ Time.S) != (2 @ Time.S)


def test_new_operators() -> None:
	assert (1 * Time.S) == (1 @ Time.S)
	assert (1 * Time.S) / Time.MS == 1000


if TYPE_CHECKING:
	def type_checks() -> None:
		# Checks for type checker false negatives