Skip to content

fix: handle negative imaginary parts in number_to_string - #616

Open
Sanjays2402 wants to merge 1 commit into
qlustered:devfrom
Sanjays2402:fix/complex-negative-imaginary-number-to-string
Open

Sanjays2402 wants to merge 1 commit into
qlustered:devfrom
Sanjays2402:fix/complex-negative-imaginary-number-to-string

Conversation

@Sanjays2402

Copy link
Copy Markdown

Closes #551

number_to_string joined the rendered real and imaginary parts with a hardcoded +, so a negative imaginary part produced a malformed literal like 1.0+-1.0j, which complex() rejects — number_to_string(1-1j, significant_digits=1) raised ValueError, and so did any diff/hash comparison involving such a number. The separator is now omitted when the rendered imaginary part is already negative.

Four cases added to the existing test_number_to_string_complex_digits parametrize list in tests/test_helper.py; they fail on master and pass with the fix.

This change was prepared with AI assistance; the regression test was run locally and fails without the fix.

@seperman seperman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review: #616 fix: handle negative imaginary parts in number_to_string

Summary: Fixes the "1.0+-1.0j" malformed-literal ValueError from #551. In the complex branch of number_to_string, the + separator is now omitted when the rendered imaginary part already starts with -. The fix is correct, but the same fix is already on dev.

Overlap with other PRs

  • #598 (merged into dev, also fixes #551) makes a functionally identical change. It computes real and imag separately and writes imag = imag if imag.startswith("-") else "+" + imag (helper.py:497-513 on dev). This PR's sign = '' if str(imag).startswith('-') else '+' (helper.py:511) gives the same output for every input. I found no other open PR for #551.
  • This PR targets master, which doesn't have #598 yet. Against dev it conflicts in deepdiff/helper.py and in CHANGELOG.md, where dev already has its own Unreleased section. tests/test_helper.py merges cleanly.

Security

No security concerns found in static review. The change is a local string-formatting refactor plus test cases and a changelog line. There is no I/O, eval or dependency change, and the diff has no non-ASCII or hidden characters.

Correctness

  • deepdiff/helper.py:499-514: the logic is correct. -0.0 imaginary parts are normalised by the recursive call's abs() for zero. nan/-inf imaginary parts also produce parseable strings.
  • str(imag) at helper.py:511 does nothing, because imag is already the string returned by number_to_string.

Tests

  • The four new cases (tests/test_helper.py:187-190) would fail on master. Checked by hand against dev's implementation, they should pass there already. Two of them cover inputs #598's tests don't: "e" notation (1.0e+0-1.0e+0j) and a negative real with a negative imaginary part (-1.0-1.0j). #598 only added "f"-notation cases with a positive real part.

Suggestions

  • Since the code fix is already on dev via #598, the most useful remaining part is the extra test coverage. A small PR against dev that adds only the "e"-notation and negative-real cases to test_number_to_string_complex_digits would keep that coverage without conflicts.
  • Please target dev for future PRs, and leave CHANGELOG.md edits to the maintainer at release time.

AI-assisted static review; the code was not executed.

Adds the two cases from the original fix that are not covered on dev:
- 'e' notation with negative imaginary part (1.0e+0-1.0e+0j)
- negative real with negative imaginary part (-1.0-1.0j)

The code fix itself is already on dev via qlustered#598; this keeps only the
extra test coverage. Targets dev, no CHANGELOG edit.
@Sanjays2402
Sanjays2402 force-pushed the fix/complex-negative-imaginary-number-to-string branch from 3e49e55 to 43da3ef Compare October 16, 2026 19:33
@Sanjays2402
Sanjays2402 changed the base branch from master to dev October 16, 2026 19:34
@Sanjays2402

Copy link
Copy Markdown
Author

Thanks for the review! You're right, the code fix is redundant with #598. I've reworked this into a test-only PR against dev: dropped the helper.py change and the CHANGELOG edit, and kept just the two cases #598 doesn't cover (the "e"-notation case and the negative-real with negative-imaginary case). Tests pass locally.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.07%. Comparing base (59acdd9) to head (43da3ef).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #616   +/-   ##
=======================================
  Coverage   95.07%   95.07%           
=======================================
  Files          18       18           
  Lines        4918     4918           
=======================================
  Hits         4676     4676           
  Misses        242      242           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Common ValueError raised when formatting complex numbers

3 participants