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

Fix #1190 Make is_compatible_with function work with dimensionless types & string parsing errors. #1191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jules-ch
Copy link
Collaborator

@jules-ch jules-ch commented Oct 15, 2020

  • Closes is_compatible_with edge cases #1190
  • Executed black -t py36 . && isort -rc . && flake8 with no errors
  • The change is fully covered by automated unit tests
  • Documented in docs/ as appropriate
  • Added an entry to the CHANGES file

Make 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.

  • Updated registry.py managing dimensionless types & discarding types not managed by pint.
  • Updated unit.py using parse_expression instead of parse_units.

@jules-ch jules-ch changed the title Fix #1189 Make is_compatible_with function work with dimensionless types & string parsing errors. WIP: Fix #1189 Make is_compatible_with function work with dimensionless types & string parsing errors. Oct 15, 2020
@jules-ch jules-ch force-pushed the fix-1189-is-compatible-with-dimensionless-types branch from 3481378 to 684f783 Compare October 15, 2020 21:01
@jules-ch jules-ch changed the title WIP: Fix #1189 Make is_compatible_with function work with dimensionless types & string parsing errors. WIP: Fix #1190 Make is_compatible_with function work with dimensionless types & string parsing errors. Oct 15, 2020
@dalito
Copy link
Contributor

dalito commented Oct 16, 2020

The code path for offset units is quite different. Maybe add a test for those, too?

@jules-ch jules-ch force-pushed the fix-1189-is-compatible-with-dimensionless-types branch 3 times, most recently from 92828b6 to 447b166 Compare October 16, 2020 12:20
@jules-ch jules-ch changed the title WIP: Fix #1190 Make is_compatible_with function work with dimensionless types & string parsing errors. Fix #1190 Make is_compatible_with function work with dimensionless types & string parsing errors. Oct 18, 2020
Copy link
Contributor

@keewis keewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, @jules-ch. I have a few comments, but I don't know if there are better options. Any insight, @hgrecco?

Comment on lines +924 to +930
if isinstance(obj1, (dict, bool, type(None))) or isinstance(
obj2, (dict, bool, type(None))
):
raise TypeError("Type can't be casted to Quantity")

Copy link
Contributor

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?

Comment on lines +937 to +942
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)
Copy link
Contributor

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?

Suggested change
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.

pint/testsuite/test_unit.py Outdated Show resolved Hide resolved
pint/testsuite/test_unit.py Outdated Show resolved Hide resolved
pint/registry.py Outdated Show resolved Hide resolved
@jules-ch jules-ch force-pushed the fix-1189-is-compatible-with-dimensionless-types branch 3 times, most recently from 2ff644d to e90f50c Compare October 28, 2020 22:22
…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.
@jules-ch jules-ch force-pushed the fix-1189-is-compatible-with-dimensionless-types branch from e90f50c to 154463e Compare October 28, 2020 22:26
@hgrecco hgrecco mentioned this pull request Feb 24, 2022
21 tasks
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

Successfully merging this pull request may close these issues.

is_compatible_with edge cases
3 participants