You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm experiencing a problem with the new custom preprocessors feature;
trying to convert a quantity to "%" using a string instead of the quantity raises an AttributeError in the ParserHelper.from_string method, since it's not aware of the preprocessors.
UREG=UnitRegistry(
preprocessors=[
functools.partial(re.sub, r"(.*)%$", r"\1 percent"),
functools.partial(re.sub, r"(.*)\u2030$", r"\1 permille"),
]
)
UREG.define("percent = 0.01 count = %")
UREG.define("permille = 0.001 count = \u2030")
assertUREG("count").to(UREG("%")).magnitude==100# this works okassertUREG("count").to(UREG("\u2030")).magnitude==1000# this also works okassertUREG("count").to(UREG("‰")).magnitude==1000# this also works okassertUREG("count").to("%").magnitude==100
The last line throws this exception:
.\venv\lib\site-packages\pint\quantity.py:561: in to
other = to_units_container(other, self._REGISTRY)
.\venv\lib\site-packages\pint\util.py:832: in to_units_container
return registry._parse_units(unit_like)
.\venv\lib\site-packages\pint\registry.py:1167: in _parse_units
return super()._parse_units(input_string, as_delta)
.\venv\lib\site-packages\pint\registry.py:1045: in _parse_units
units = ParserHelper.from_string(input_string)
.\venv\lib\site-packages\pint\util.py:574: in from_string
ret = build_eval_tree(gen).evaluate(cls.eval_token)
AttributeError: 'NoneType' object has no attribute 'evaluate'
The text was updated successfully, but these errors were encountered:
This is (unfortunately) the expected behavior of the preprocessors feature. Given that the preprocessors exist on the registry level, they only function when using the registry-level parsers (parse_expression/registry-as-a-callable and parse_units), and not with lower-level functions using ParserHelper.from_string as you note. (See #429 (comment) and #429 (comment) for the rationale.)
However, this can definitely be better documented! (xref #972)
Thanks @jthielen for the explanation, I supposed that it was related to the design of the feature (I tried to read the entire discussion but I got lost somewhere ;) ).
Documenting this is fine, but IMHO this is something that should be better handled, i.e. by issuing a warning if a string is passed and the preprocessor attribute is not empty.
I'm experiencing a problem with the new custom preprocessors feature;
trying to convert a quantity to "%" using a string instead of the quantity raises an AttributeError in the
ParserHelper.from_string
method, since it's not aware of the preprocessors.The last line throws this exception:
The text was updated successfully, but these errors were encountered: