Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias fix up #53

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions changelog/53.fix.md
Original file line number Diff line number Diff line change
@@ -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
<Quantity(1, 'a')>
```

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
<Quantity(1, 'yr')>
```
3 changes: 2 additions & 1 deletion src/openscm_units/_unit_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Comment on lines +235 to +236
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very subtle change. It wasn't obvious to me straight away that 'everything on the right is an aliases for the first thing on the left'

self.define("h = hour")
self.define("d = day")
self.define("degreeC = degC")
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading