Skip to content

Commit faee3f0

Browse files
committed
Was testing pint < 0.21 bug
1 parent b21113a commit faee3f0

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

gemd/units/impl.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
_ALLOWED_OPERATORS = {".", "+", "-", "*", "/", "//", "^", "**", "(", ")"}
1919

2020

21+
def _space_after_minus_preprocessor(input_string: str) -> str:
22+
"""A preprocessor that protects against a pint < 0.21 bug."""
23+
return re.sub(r"(?<=-)\s+(?=\d)", "", input_string)
24+
25+
2126
def _scientific_notation_preprocessor(input_string: str) -> str:
2227
"""Preprocessor that converts x * 10 ** y format to xEy."""
2328
def _as_scientific(matchobj: re.Match) -> str:
@@ -120,7 +125,8 @@ def _scaling_preprocessor(input_string: str) -> str:
120125

121126

122127
_REGISTRY = UnitRegistry(filename=DEFAULT_FILE,
123-
preprocessors=[_scientific_notation_preprocessor,
128+
preprocessors=[_space_after_minus_preprocessor,
129+
_scientific_notation_preprocessor,
124130
_scaling_preprocessor],
125131
autoconvert_offset_to_baseunit=True)
126132

@@ -287,6 +293,7 @@ def change_definitions_file(filename: str = None):
287293
filename = DEFAULT_FILE
288294
_REGISTRY = UnitRegistry(filename=filename,
289295
preprocessors=[
296+
_space_after_minus_preprocessor,
290297
_scientific_notation_preprocessor,
291298
_scaling_preprocessor
292299
],

tests/units/test_parser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def test_parse_expected(return_unit):
2929
"g / 2.5 cm", # Scaling factors are acceptable
3030
"g / -+-25e-1 m", # Weird but fine
3131
"ug / - -250 mL", # Spaces between unaries is acceptable
32-
"1 / 10**5 degC" # Spaces between unaries is acceptable
32+
"1 / 10**5 degC", # Spaces between unaries is acceptable
33+
"m ** - 1" # Pint < 0.21 throws DefinitionSyntaxError
3334
]
3435
for unit in expected:
3536
parsed = parse_units(unit, return_unit=return_unit)
@@ -75,7 +76,6 @@ def test_parse_unexpected():
7576

7677
definition = [
7778
"/gram", # A leading operator makes no sense
78-
"m ** - 1" # Pint parse doesn't accept spaces between negative & numeral / unit
7979
]
8080
for unit in definition:
8181
with pytest.raises(DefinitionSyntaxError):

0 commit comments

Comments
 (0)