From 6b7ee52279d8ed618063aa962a5243593a012949 Mon Sep 17 00:00:00 2001 From: Dhanvantari Tilak Date: Tue, 7 Dec 2021 23:39:14 -0800 Subject: [PATCH] Fix: TypeError in _get_numbers_distance when ignore_order 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() ``` --- deepdiff/distance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deepdiff/distance.py b/deepdiff/distance.py index 321ff8cf..fb572d6b 100644 --- a/deepdiff/distance.py +++ b/deepdiff/distance.py @@ -194,9 +194,9 @@ def _get_numbers_distance(num1, num2, max_=1): """ if num1 == num2: return 0 - if isinstance(num1, float): + if not isinstance(num1, float): num1 = float(num1) - if isinstance(num2, float): + if not isinstance(num2, float): num2 = float(num2) # Since we have a default cutoff of 0.3 distance when # getting the pairs of items during the ingore_order=True