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

Bug-Fix: TypeError in _get_numbers_distance() when ignore_order = True #284

Merged
merged 1 commit into from
Dec 17, 2021

Conversation

Dhanvantari
Copy link
Contributor

@Dhanvantari Dhanvantari commented Dec 8, 2021

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()

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()

```
@seperman
Copy link
Owner

@Dhanvantari Haha, thanks for catching that.

@seperman seperman added the bug label Dec 17, 2021
@seperman
Copy link
Owner

One test fails but I'm gonna fix it.

@seperman seperman merged commit e2de588 into seperman:dev Dec 17, 2021
@seperman seperman mentioned this pull request Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants