Skip to content

Common ValueError raised when formatting complex numbers #551

Description

@robtovey

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

Activity

  1. added a commit that references this issue on Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions