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).

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

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
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

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov
apt-get update apt-get install -y gcc
3.9
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
coverage==7.8.0
exceptiongroup==1.2.2
iniconfig==2.1.0
-e git+https://github.com/coddingtonbear/python-measurement.git@b6e1c9f4f1d24906cfc2106d5d9bf5c866a6e775#egg=measurement
packaging==24.2
pluggy==1.5.0
pytest==8.3.5
pytest-cov==6.0.0
tomli==2.2.1

name: python-measurement
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - coverage==7.8.0
      - exceptiongroup==1.2.2
      - iniconfig==2.1.0
      - packaging==24.2
      - pluggy==1.5.0
      - pytest==8.3.5
      - pytest-cov==6.0.0
      - tomli==2.2.1
prefix: /opt/conda/envs/python-measurement

tests/test_base.py::TestAbstractMeasure::test_mul__raise_for_same_type
tests/test_base.py::test_qualname tests/test_base.py::TestImmutableKeyDict::test_setitem tests/test_base.py::TestUnit::test_post_init tests/test_base.py::TestUnit::test_get_symbols tests/test_base.py::TestUnit::test_to_si tests/test_base.py::TestUnit::test_from_si tests/test_base.py::TestMetricUnit::test_get_symbols tests/test_base.py::TestMetricUnit::test_get_symbols__unique_names tests/test_base.py::TestAbstractMeasure::test_repr tests/test_base.py::TestAbstractMeasure::test_str tests/test_base.py::TestAbstractMeasure::test_format tests/test_base.py::TestAbstractMeasure::test_getitem tests/test_base.py::TestAbstractMeasure::test_custom_string tests/test_base.py::TestAbstractMeasure::test_eq tests/test_base.py::TestAbstractMeasure::test_eq__not_implemented tests/test_base.py::TestAbstractMeasure::test_tl tests/test_base.py::TestAbstractMeasure::test_lt__not_implemented tests/test_base.py::TestAbstractMeasure::test_gt tests/test_base.py::TestAbstractMeasure::test_gt__not_implemented tests/test_base.py::TestAbstractMeasure::test_add tests/test_base.py::TestAbstractMeasure::test_add__raise__type_error tests/test_base.py::TestAbstractMeasure::test_iadd tests/test_base.py::TestAbstractMeasure::test_sub tests/test_base.py::TestAbstractMeasure::test_sub__raise__type_error tests/test_base.py::TestAbstractMeasure::test_isub tests/test_base.py::TestAbstractMeasure::test_mul tests/test_base.py::TestAbstractMeasure::test_mul__raise__type_error tests/test_base.py::TestAbstractMeasure::test_imul tests/test_base.py::TestAbstractMeasure::test_rmul tests/test_base.py::TestAbstractMeasure::test_truediv tests/test_base.py::TestAbstractMeasure::test_truediv__raise__type_error tests/test_base.py::TestAbstractMeasure::test_itruediv tests/test_base.py::TestAbstractMeasure::test_rtruediv tests/test_base.py::TestAbstractMeasure::test_bool tests/test_base.py::TestAbstractMeasure::test_getattr
measurement/base.py
measurement/base.py