deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

Common ValueError raised when formatting complex numbers

Open robtovey opened this issue 6 months ago • 0 comments

Describe the bug Attempts to format the number 1-1j will result in a ValueError raised. This is due to a feature (bug?) in CPython:

x = 1+-1j # this does not raise an error
x = complex("1+-1j") # this raises a ValueError

Unfortunately this case must be hit quite frequently in these lines of code: https://github.com/seperman/deepdiff/blob/ecc823a3fdb9c094b7392ca40f859eab8cc8572a/deepdiff/helper.py#L431-L446

To Reproduce

from deepdiff.helper import number_to_string
print(number_to_string(1-1j, significant_digits=1)) # raises a ValueError

Expected behavior I expect this to print 1.0-1.0j, not raise an error!

I am using

  • Python 3.12
  • DeepDiff on main

robtovey avatar May 24 '25 21:05 robtovey