Skip to content

Compare int with float values fail with number_format_notation='e' and ignore_nummeric_type_changes=True #178

Open
@eremmel

Description

@eremmel

Describe the bug
Comparing values of type int and float based on the settings:
number_format_notation='e'
ignore_numeric_type_changes=True
fail on compares like. key=41 with key=41.0. Does not happen with settings:
number_format_notation='f'
ignore_numeric_type_changes=True

To Reproduce
Steps to reproduce the behavior:

  1. Run the code:
 from deepdiff.diff import DeepDiff
 d1 = {'a': int(41)}
 d2 = {'a': float(41)}
 DeepDiff(d1, d2, number_format_notation='e', ignore_numeric_type_changes=True)
 DeepDiff(d1, d2, number_format_notation='f', ignore_numeric_type_changes=True)
  1. Results shown:
 {'values_changed': {"root['a']": {'new_value': 41.0, 'old_value': 41}}}
 {}

Expected behavior
Expect that number_float_notation should not have any impact.

Analysis
I did some debugging and found that in source: diff.py, function: __diff_numbers() the results of number_to_string() are (using 6 significant_digits):

t1_s = 4.100000e+04
t2_s = 4.100000e+4

In function: number_to_string() source: helper.py near end the following is listed:

    if number_format_notation == 'e' and isinstance(number, float):
        result = result.replace('+0', '+')

I think that also for type int the correction has to be executed or only skipped for type Decimal.
So solution might be:

    if number_format_notation == 'e' and not isinstance(number, Decimal):
        result = result.replace('+0', '+')

OS, DeepDiff version and Python version (please complete the following information):

  • OS: Win 10 Pro 64-bit
  • Version python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)]'
  • Version deepdiff 4.3.0

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions