Skip to content

Commit bbc0356

Browse files
committed
Fix #40 -- Raise type error with proper message
If two measures are multiplied that are not compatible – should not be multipled, like mass x mass -, the error message should indicate that incompatibility.
1 parent b6e1c9f commit bbc0356

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

measurement/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,11 @@ def __isub__(self, other):
339339
def __mul__(self, other):
340340
try:
341341
value = getattr(self, self.unit.org_name) * other
342+
return type(self)(value=value, unit=self.unit.org_name)
342343
except TypeError as e:
343344
raise TypeError(
344345
f"can't multiply type '{qualname(self)}' and '{qualname(other)}'"
345346
) from e
346-
return type(self)(value=value, unit=self.unit.org_name)
347347

348348
def __imul__(self, other):
349349
return self * other

tests/test_base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from measurement.base import ImmutableKeyDict, MetricUnit, Unit, qualname
6-
from measurement.measures import Distance
6+
from measurement.measures import Distance, Mass
77

88

99
def test_qualname():
@@ -165,6 +165,11 @@ def test_mul__raise__type_error(self):
165165
str(e.value) == f"can't multiply type '{qualname(self.measure)}' and 'str'"
166166
)
167167

168+
def test_mul__raise_for_same_type(self):
169+
with pytest.raises(TypeError) as e:
170+
Mass("1 kg") * Mass("1 kg")
171+
assert str(e.value) == f"can't multiply type 'Mass' and 'Mass'"
172+
168173
def test_imul(self):
169174
d = self.measure(**{self.unit: 2})
170175
d *= 2

0 commit comments

Comments
 (0)