Repository: coddingtonbear/python-measurement

Issue:
Multiplying a measure by itself without specifying the measurement attribute results in TypeError
```python
>>> from measurement.measures import Mass
>>> m = Mass(lb=135)
>>> print(m)
135 lb
>>> print(m*m)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.8/site-packages/measurement/base.py", line 346, in __mul__
    return type(self)(value=value, unit=self.unit.org_name)
  File "/home/runner/.local/share/virtualenvs/python3/lib/python3.8/site-packages/measurement/base.py", line 254, in __init__
    value = decimal.Decimal(value)
TypeError: conversion from Mass to Decimal is not supported
```

It seems like this should work and return 135 lb * 135 lb = 18,225 lb

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/coddingtonbear/python-measurement/pull/41?src=pr&el=h1) Report
> Merging [#41](https://codecov.io/gh/coddingtonbear/python-measurement/pull/41?src=pr&el=desc) into [master](https://codecov.io/gh/coddingtonbear/python-measurement/commit/b6e1c9f4f1d24906cfc2106d5d9bf5c866a6e775&el=desc) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/coddingtonbear/python-measurement/pull/41/graphs/tree.svg?width=650&height=150&src=pr&token=G1f253j6u3)](https://codecov.io/gh/coddingtonbear/python-measurement/pull/41?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master       #41   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        10           
  Lines          440       440           
=========================================
  Hits           440       440           
```


| [Impacted Files](https://codecov.io/gh/coddingtonbear/python-measurement/pull/41?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [measurement/base.py](https://codecov.io/gh/coddingtonbear/python-measurement/pull/41/diff?src=pr&el=tree#diff-bWVhc3VyZW1lbnQvYmFzZS5weQ==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/coddingtonbear/python-measurement/pull/41?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/coddingtonbear/python-measurement/pull/41?src=pr&el=footer). Last update [b6e1c9f...4e3b516](https://codecov.io/gh/coddingtonbear/python-measurement/pull/41?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/measurement/base.py b/measurement/base.py
index 3dd1af0..236d30e 100644
--- a/measurement/base.py
+++ b/measurement/base.py
@@ -339,11 +339,11 @@ class AbstractMeasure(metaclass=MeasureBase):
     def __mul__(self, other):
         try:
             value = getattr(self, self.unit.org_name) * other
+            return type(self)(value=value, unit=self.unit.org_name)
         except TypeError as e:
             raise TypeError(
                 f"can't multiply type '{qualname(self)}' and '{qualname(other)}'"
             ) from e
-        return type(self)(value=value, unit=self.unit.org_name)
 
     def __imul__(self, other):
         return self * other


Test patch:
diff --git a/tests/test_base.py b/tests/test_base.py
index 70ffc34..dce1951 100644
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -3,7 +3,7 @@ import decimal
 import pytest
 
 from measurement.base import ImmutableKeyDict, MetricUnit, Unit, qualname
-from measurement.measures import Distance
+from measurement.measures import Distance, Mass
 
 
 def test_qualname():
@@ -165,6 +165,11 @@ class TestAbstractMeasure:
             str(e.value) == f"can't multiply type '{qualname(self.measure)}' and 'str'"
         )
 
+    def test_mul__raise_for_same_type(self):
+        with pytest.raises(TypeError) as e:
+            Mass("1 kg") * Mass("1 kg")
+        assert str(e.value) == f"can't multiply type 'Mass' and 'Mass'"
+
     def test_imul(self):
         d = self.measure(**{self.unit: 2})
         d *= 2