-
Notifications
You must be signed in to change notification settings - Fork 473
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
Fix #1190 Make is_compatible_with function work with dimensionless types & string parsing errors. #1191
base: master
Are you sure you want to change the base?
Conversation
3481378
to
684f783
Compare
The code path for offset units is quite different. Maybe add a test for those, too? |
92828b6
to
447b166
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if isinstance(obj1, (dict, bool, type(None))) or isinstance( | ||
obj2, (dict, bool, type(None)) | ||
): | ||
raise TypeError("Type can't be casted to Quantity") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure what the expected behavior should be: as per the docstring, everything which is not a Quantity
, Unit
or str
is treated as dimensionless
. Maybe it would be better to keep it that way?
if isinstance(obj2, (self.Quantity, self.Unit, str)): | ||
return self.is_compatible_with(obj2, obj1, *contexts, **ctx_kwargs) | ||
else: | ||
return self.Quantity(obj1).is_compatible_with(obj2, *contexts, **ctx_kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
obj2
possibly being a quantity / unit / str incompatible to dimensionless is a good point. Maybe something like this instead?
if isinstance(obj2, (self.Quantity, self.Unit, str)): | |
return self.is_compatible_with(obj2, obj1, *contexts, **ctx_kwargs) | |
else: | |
return self.Quantity(obj1).is_compatible_with(obj2, *contexts, **ctx_kwargs) | |
if isinstance(obj2, (self.Quantity, self.Unit, str)): | |
return self.is_compatible_with(obj2, obj1, *contexts, **ctx_kwargs) | |
# neither obj1 or obj2 are Quantity, Unit or str objects so both are dimensionless | |
return True |
I don't think we should try to make sure types unrelated to pint
are compatible with each other, but we certainly could replace the return True
with a ValueError
.
2ff644d
to
e90f50c
Compare
…types & string parsing errors. is_compatible_with was not consistent with dimensionless types when switching arguments. String processing was not allowing strings with any magnitude. - Updated registry.py managing dimensionless types & discarding types not managed by pint. - Updated unit.py using parse_expression instead of parse_units.
e90f50c
to
154463e
Compare
black -t py36 . && isort -rc . && flake8
with no errorsDocumented in docs/ as appropriateMake is_compatible_with function with dimensionless types & string parsing errors.
is_compatible_with was not consistent with dimensionless types when switching arguments.
String processing was not allowing strings with any magnitude.