diff --git a/changelog/53.fix.md b/changelog/53.fix.md new file mode 100644 index 0000000..2958bbb --- /dev/null +++ b/changelog/53.fix.md @@ -0,0 +1,20 @@ +Fixed accidental aliasing of "yr" to "a". + +We had accidentally assigned "yr" to be an alias for "a". +This meant we had the following behaviour + +```python +>>> from openscm_units import unit_registry +>>> val = unit_registry.Quantity(1, "yr") +>>> val + +``` + +This PR fixes this so that if you pass in "yr", it stays as yr i.e. you get + +```python +>>> from openscm_units import unit_registry +>>> val = unit_registry.Quantity(1, "yr") +>>> val + +``` diff --git a/src/openscm_units/_unit_registry.py b/src/openscm_units/_unit_registry.py index b213881..e6850b6 100644 --- a/src/openscm_units/_unit_registry.py +++ b/src/openscm_units/_unit_registry.py @@ -232,7 +232,8 @@ def add_standards(self) -> None: self._add_gases({x: x for x in MIXTURES}) - self.define("a = 1 * year = annum = yr") + self.define("yr = 1 * year") + self.define("a = 1 * year = annum") self.define("h = hour") self.define("d = day") self.define("degreeC = degC") diff --git a/tests/unit/test_units.py b/tests/unit/test_units.py index 4efbe11..3f26489 100644 --- a/tests/unit/test_units.py +++ b/tests/unit/test_units.py @@ -380,3 +380,31 @@ def test_split_invalid(): match="Mixture has dimensionality 2 != 1, which is not supported.", ): unit_registry.split_gas_mixture(1 * unit_registry("CFC400") ** 2) + + +@pytest.mark.parametrize("inp", ("yr",)) +def test_no_autoconversion(inp): + """ + Make sure that the units are not automatically converted to some other value + + The auto-conversion happens if pint thinks that the units are an alias + for something else. + See {py:func}`test_aliases`. + """ + res = unit_registry.Quantity(1, inp) + + assert str(res.units) == inp + + +@pytest.mark.parametrize( + "alias, exp", + ( + ("annum", "a"), + ("degC", "degree_Celsius"), + ), +) +def test_aliases(alias, exp): + """Test our aliases behave as expected""" + res = unit_registry.Quantity(1, alias) + + assert str(res.units) == exp