GeezerVison is showing github.com — Bug-Fix: TypeError in _get_numbers_distance() when ignore_order = True by Dhanvantari · Pull Request #284 · qlustered/deepdiff
Conversation
Dhanvantari
commented
Dec 8, 2021
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
For lists comparison when ignore_order is True, TypeError occurs as type(_max) = float it doesn't match with other numbers like Decimal.
The cast should be done when numbers are not 'float' type.
Example:
from decimal import Decimal
from deepdiff import DeepDiff
def test_deep_diff():
a = {'a': [Decimal(1), Decimal(2), Decimal(3), Decimal(5)]}
b = {'a': [Decimal(3), Decimal(2), Decimal(1), Decimal(4)]}
print(DeepDiff(a, b, ignore_order = True, cutoff_distance_for_pairs=1))
def main():
test_deep_diff()
if __name__ == "__main__":
main()
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
For lists comparison when ignore_order is True, TypeError occurs as type(_max) = float it doesnt match with other numbers like Decimal.
The cast should be done when numbers are not 'float' type.
Example:
```
from decimal import Decimal
from deepdiff import DeepDiff
from deepdiff.helper import number_to_string
def custom_number_to_string(number, *args, **kwargs):
if type(number) == Decimal:
number = float(number)
return number_to_string(number, *args, **kwargs)
def test_deep_diff():
# a = {'a': [datetime.datetime(2020, 5, 17), datetime.datetime(2020, 6, 17), datetime.datetime(2020, 7, 17)]}
# b = {'a': [datetime.datetime(2020, 7, 17), datetime.datetime(2020, 6, 17), datetime.datetime(2020, 5, 17)]}
a = {'a': [Decimal(1), Decimal(2), Decimal(3), Decimal(5)]}
b = {'a': [Decimal(3), Decimal(2), Decimal(1), Decimal(4)]}
print(DeepDiff(a, b, ignore_order = True, cutoff_distance_for_pairs=1, number_to_string_func=custom_number_to_string))
def main():
test_deep_diff()
if __name__ == "__main__":
main()
```
Dhanvantari reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
One test fails but I'm gonna fix it.
|
Dhanvantari reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|