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

Percent (and python identifiers) characters not allowed when definining UnitRegistry #38

Open
quajada opened this issue Dec 13, 2022 · 3 comments

Comments

@quajada
Copy link

quajada commented Dec 13, 2022

Error under: def define_haystack_units() in pintutil.py

DefinitionSyntaxError: Cannot define '%' (UnitDefinition): is not a valid unit name (must follow Python identifier rules)
1,0-1,10 % = [] = %

Percentage units must be defined some other way. Additionally, DefinitionSyntaxError: missing unary operator "%" is thrown when tried to bypass with following code:

ureg = UnitRegistry(on_redefinition='ignore')
ureg.define("percent = [] = %")
ureg('50 %')

This error prevents hszinc to be pip installed

versions:
hszinc - 1.3.1
pint - 0.20.1

@quajada quajada changed the title Percent (%) characters not compatible when definining UnitRegistry Percent (%) characters not allowed when definining UnitRegistry Dec 13, 2022
@sjlongland
Copy link
Contributor

Ick, yeah… I can see that being a potential problem. It might be a while before I can get to looking at this problem as not many of us get to work with Python stuff these days.

Do you know if it happens with a particular release of pint?

Alternatively, if you do not require unit conversion, you can remove pint from requirements.txt. It is an optional extra dependency (I need to figure out how to declare it as such) and not actually required to make hszinc work.

@quajada quajada changed the title Percent (%) characters not allowed when definining UnitRegistry Percent (and python identifiers) characters not allowed when definining UnitRegistry Dec 13, 2022
@quajada
Copy link
Author

quajada commented Dec 13, 2022

Thanks for your reply! It is happening with pint 0.20.1, but I think the issue comes from trying to use python identifiers to define the UnitRegistry - so no special characters.

Perhaps they can all be treated as:

ureg.define('fraction = [] = frac')
ureg.define('percent = 1e-2 frac = pct')

But I am not sure how to recognize and assign the unit string from haystack

@sjlongland
Copy link
Contributor

Could those affected please try this branch and let me know how they go?

https://github.com/widesky/hszinc/tree/bugfix/WC-5825-issue38-pint-percent

I had some fun and games, because nose is no longer maintained, I had to port a number of tests over… and because Travis CI is no more, I had to replace that with Github actions.

At this time, I am not testing on Python 2.7. Not sure if I should bother actually going forward, if it breaks now, it breaks. But anyway… that branch should resolve the issues with pint as it is today.

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