Skip to content
Open
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 @@ -56,7 +56,7 @@ def _fix_udunits_div(string):

# Fix UDUNITS-style powers, percent signs, and ill-defined units
_UDUNIT_POWER = re.compile(r'(?<=[A-Za-z\)])(?![A-Za-z\)])'
r'(?<![0-9\-][eE])(?<![0-9\-])(?=[0-9\-])')
r'(?<![0-9\-][eE])(?<![0-9\-])(?=-?[0-9]+([ )]|$))')
_unit_preprocessors = [_fix_udunits_powers, lambda string: string.replace('%', 'percent'),
_fix_udunits_div]

Expand Down
14 changes: 12 additions & 2 deletions tests/units/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from metpy.testing import (assert_almost_equal, assert_array_almost_equal, assert_array_equal,
assert_nan)
from metpy.units import (check_units, concatenate, is_quantity,
pandas_dataframe_to_unit_arrays, units)
pandas_dataframe_to_unit_arrays, UndefinedUnitError, units)


def test_concatenate():
Expand Down Expand Up @@ -231,9 +231,19 @@ def test_percent_units():
marks=pytest.mark.xfail(reason='hgrecco/pint#1485')
),
('(J kg-1)(m s-1)(-1)', units.m ** 3 / units.s ** 3),
('/s', units.s ** -1)
('/s', units.s ** -1),
('feet_H2O', units.kg / units.m / units.s ** 2)
)
)
def test_udunits_power_syntax(unit_str, pint_unit):
"""Test that UDUNITS style powers are properly parsed and interpreted."""
assert units(unit_str).to_base_units().units == pint_unit


def test_unsupported_udunits_power_fails():
"""Test that removing the spaces between factors causes parsing to fail.

UDUnits does not support this either.
"""
with pytest.raises(UndefinedUnitError):
assert units('m2s-2').to_base_units.units == units.m ** 2 / units.s ** 2
Loading