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

Fix unit power regex for parentheses handling #2380

Merged
merged 1 commit into from
Mar 8, 2022
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
2 changes: 1 addition & 1 deletion src/metpy/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
preprocessors=[
functools.partial(
re.sub,
r'(?<=[A-Za-z])(?![A-Za-z])(?<![0-9\-][eE])(?<![0-9\-])(?=[0-9\-])',
r'(?<=[A-Za-z\)])(?![A-Za-z\)])(?<![0-9\-][eE])(?<![0-9\-])(?=[0-9\-])',
'**'
),
lambda string: string.replace('%', 'percent')
Expand Down
22 changes: 20 additions & 2 deletions tests/units/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ def test_percent_units():
assert str(units('%').units) == 'percent'


def test_udunits_power_syntax():
@pytest.mark.parametrize(
'unit_str,pint_unit',
(
# Validated against cf-units (UDUNITS-2 wrapper)
('m s-1', units.m / units.s),
('m2 s-2', units.m ** 2 / units.s ** 2),
('kg(-1)', units.kg),
('kg-1', units.kg ** -1),
('W m(-2)', units.m ** 3 * units.kg * units.s ** -3),
('W m-2', units.kg * units.s ** -3),
('(W m-2 um-1)-1', units.m * units.kg ** -1 * units.s ** 3),
pytest.param(
'(J kg-1)(m s-1)-1', units.m / units.s,
marks=pytest.mark.xfail(reason='hgrecco/pint#1485')
),
('(J kg-1)(m s-1)(-1)', units.m ** 3 / units.s ** 3)
)
)
def test_udunits_power_syntax(unit_str, pint_unit):
"""Test that UDUNITS style powers are properly parsed and interpreted."""
assert units('m2 s-2').units == units.m ** 2 / units.s ** 2
assert units(unit_str).to_base_units().units == pint_unit