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

Adjust for pint 0.18 #35

Merged
merged 2 commits into from
Nov 12, 2021
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
13 changes: 13 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ on:
jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
pint-version:
# Force specific version of pint
- "==0.17"
- "==0.18"
# No change, i.e. latest
- ""

name: pint${{ matrix.pint-version }}

steps:
- uses: actions/checkout@v2

Expand All @@ -21,6 +33,7 @@ jobs:
run: |
pip install numpy pytest pytest-cov setuptools-scm
pip install --editable .
pip install pint${{ matrix.pint-version }}
pytest -rA --verbose --color=yes --cov=iam_units --cov-report=xml

- name: Upload test coverage to Codecov.io
Expand Down
17 changes: 11 additions & 6 deletions iam_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def format_mass(obj, info, spec=None):
info : str
Any information, e.g. the symbol of a GHG species.
spec : str, optional
Pint formatting specifier such as ':H' (HTML format), ':~' (compact
format with symbols), etc.
Pint formatting specifier such as "H" (HTML format), "~C" (compact format with
symbols), etc.
"""
spec = spec or obj.default_format

Expand All @@ -122,15 +122,20 @@ def format_mass(obj, info, spec=None):
except AttributeError:
pass # Already a Unit object

# Use the symbol for a ':~' spec
# Use the symbol if the modifier "~" is in `spec`; else the canonical name. cf.
# pint.Unit.__format__()
method = registry._get_symbol if "~" in spec else lambda k: k
# Collect the pieces of the unit expression
units = [[method(key), value] for key, value in obj._units.items()]

# Index of the mass component
mass_index = list(obj.dimensionality.keys()).index("[mass]")
# Append the information to the mass component
# Append the information (e.g. species) to the mass component
units[mass_index][0] += f" {info}"

# Hand off to pint's formatting
return format_unit(to_units_container(dict(units), registry=registry), spec)
# Prepare pint.util.UnitsContainer and hand off to pint's formatting. Discard "~"
# (used above) and ":" (invalid in pint ≥0.18).
return format_unit(
to_units_container(dict(units), registry=registry),
spec.replace("~", "").lstrip(":"),
)