Skip to content

literal: reject internal whitespace in complex::parse_str#8242

Open
youknowone wants to merge 1 commit into
RustPython:mainfrom
youknowone:complex-parse-ws
Open

literal: reject internal whitespace in complex::parse_str#8242
youknowone wants to merge 1 commit into
RustPython:mainfrom
youknowone:complex-parse-ws

Conversation

@youknowone

@youknowone youknowone commented Jul 8, 2026

Copy link
Copy Markdown
Member

complex::parse_str splits the token on the central +/- and parses each side with float::parse_str, which tolerates surrounding whitespace on a fragment. As a result complex("1 +2j") parsed as (1+2j) instead of raising.

Whitespace is only valid around the whole string and the optional parentheses, never inside the numeric token. Reject any whitespace remaining after the parentheses are stripped, matching CPython (complex("1 +2j")ValueError).

Added unit tests: internal-whitespace rejection, surrounding/paren whitespace acceptance, and basic parsing (incl. underscores).

assisted by Claude

Summary by CodeRabbit

  • Bug Fixes

    • Complex number parsing now rejects inputs with whitespace inside the numeric content, preventing invalid values like 1 +2j or 2 -3j from being accepted.
    • Whitespace is still allowed around the full literal and around outer parentheses, preserving valid formats.
  • Tests

    • Added coverage for rejected whitespace variants, valid spacing around literals, and common valid/invalid parsing cases.

parse_str split the token then parsed each part with float::parse_str,
which tolerates surrounding whitespace, so "1 +2j" parsed as (1+2j).
Reject whitespace inside the token after stripping optional parentheses.
Add unit tests.

Assisted-by: Claude
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: cfb45e88-e685-43fa-9fcd-e0725f3ade03

📥 Commits

Reviewing files that changed from the base of the PR and between 5c36d5c and bc209d9.

📒 Files selected for processing (1)
  • crates/literal/src/complex.rs

📝 Walkthrough

Walkthrough

Modifies parse_str in the complex literal parser to reject inputs containing internal ASCII whitespace after trimming, while still permitting surrounding whitespace and optional outer parentheses. Adds unit tests covering rejection and acceptance cases.

Changes

Complex Literal Whitespace Validation

Layer / File(s) Summary
Whitespace rejection and tests
crates/literal/src/complex.rs
parse_str now returns None if trimmed input contains internal whitespace; new tests verify rejection of internal-whitespace variants and acceptance of surrounding/parentheses whitespace plus basic valid/invalid parsing cases.

Estimated code review effort: 1 (Trivial) | ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: rejecting internal whitespace in complex literal parsing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@youknowone youknowone marked this pull request as ready for review July 8, 2026 16:57
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.

1 participant