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

Pint should validate aliases #1918

Open
dalito opened this issue Jan 11, 2024 · 5 comments
Open

Pint should validate aliases #1918

dalito opened this issue Jan 11, 2024 · 5 comments

Comments

@dalito
Copy link
Contributor

dalito commented Jan 11, 2024

Currently it is possible to define invalid and unusable aliases. In contrast to unit names, alias names are obviously not sufficiently validated.

Python 3.11.7 (tags/v3.11.7:fa7a6f2, Dec  4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)] on win32
>>> import pint
>>> pint.__version__
'0.23'
>>> ureg = pint.UnitRegistry()
>>> ureg.define("@alias meter = my'dog")
>>> ureg("m").to("my'dog")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\dlinke\MyProg_local\gh-dalito\ucumvert\.venv\Lib\site-packages\pint\facets\plain\quantity.py", line 526, in to
    other = to_units_container(other, self._REGISTRY)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dlinke\MyProg_local\gh-dalito\ucumvert\.venv\Lib\site-packages\pint\util.py", line 1046, in to_units_container
    return registry.parse_units_as_container(unit_like)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dlinke\MyProg_local\gh-dalito\ucumvert\.venv\Lib\site-packages\pint\facets\nonmultiplicative\registry.py", line 70, in parse_units_as_container
    return super().parse_units_as_container(input_string, as_delta, case_sensitive)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dlinke\MyProg_local\gh-dalito\ucumvert\.venv\Lib\site-packages\pint\facets\plain\registry.py", line 1204, in parse_units_as_container
    return self._parse_units_as_container(input_string, as_delta, case_sensitive)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dlinke\MyProg_local\gh-dalito\ucumvert\.venv\Lib\site-packages\pint\facets\plain\registry.py", line 1239, in _parse_units_as_container
    cname = self.get_name(name, case_sensitive=case_sensitive)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dlinke\MyProg_local\gh-dalito\ucumvert\.venv\Lib\site-packages\pint\facets\plain\registry.py", line 647, in get_name
    raise UndefinedUnitError(name_or_alias)
pint.errors.UndefinedUnitError: 'my' is not defined in the unit registry
>>>ureg("m").to("my")  # So is maybe "my" defined by wrong parsing?
(...)
pint.errors.UndefinedUnitError: 'my' is not defined in the unit registry
@hgrecco
Copy link
Owner

hgrecco commented Jan 11, 2024

I agree. Alias and unit names must be validated. I think they must follow the same rules as python identifiers.

@dalito
Copy link
Contributor Author

dalito commented Jan 12, 2024

The unit names are already properly validated. So validation code is already written just not applied to aliases.

@hgrecco
Copy link
Owner

hgrecco commented Jan 12, 2024

indeed! Will fix it!

@hgrecco
Copy link
Owner

hgrecco commented Jan 12, 2024

The code is there already Just need to swap the function for is_valid_unit_alias. Not sure why it is _no_space instead of str.isidentifier. I will try to look at the history to see if there was any reason that I should tackle before.

But I agree with you, an alias should follow the same rule as a unit name.

@hgrecco
Copy link
Owner

hgrecco commented Mar 9, 2024

It turns out it is not so easy. Some alias that we want to have like °C are not valid identifiers. We will need to improve the function currently named _no_space. What else we need to check in addition that there are no spaces?

>>> str.isidentifier("°C")
False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants