Skip to content

Commit 692e055

Browse files
authored
Merge pull request #4059 from DWesl/patch-2
2 parents a791a75 + 43c3669 commit 692e055

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/metpy/units.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _fix_udunits_div(string):
5656

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

tests/units/test_units.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from metpy.testing import (assert_almost_equal, assert_array_almost_equal, assert_array_equal,
1212
assert_nan)
1313
from metpy.units import (check_units, concatenate, is_quantity,
14-
pandas_dataframe_to_unit_arrays, units)
14+
pandas_dataframe_to_unit_arrays, UndefinedUnitError, units)
1515

1616

1717
def test_concatenate():
@@ -231,9 +231,19 @@ def test_percent_units():
231231
marks=pytest.mark.xfail(reason='hgrecco/pint#1485')
232232
),
233233
('(J kg-1)(m s-1)(-1)', units.m ** 3 / units.s ** 3),
234-
('/s', units.s ** -1)
234+
('/s', units.s ** -1),
235+
('feet_H2O', units.kg / units.m / units.s ** 2)
235236
)
236237
)
237238
def test_udunits_power_syntax(unit_str, pint_unit):
238239
"""Test that UDUNITS style powers are properly parsed and interpreted."""
239240
assert units(unit_str).to_base_units().units == pint_unit
241+
242+
243+
def test_unsupported_udunits_power_fails():
244+
"""Test that removing the spaces between factors causes parsing to fail.
245+
246+
UDUnits does not support this either.
247+
"""
248+
with pytest.raises(UndefinedUnitError):
249+
assert units('m2s-2').to_base_units.units == units.m ** 2 / units.s ** 2

0 commit comments

Comments
 (0)