Skip to content

Implement complex.from_number#7453

Merged
youknowone merged 4 commits intoRustPython:mainfrom
moreal:complex-related
Mar 17, 2026
Merged

Implement complex.from_number#7453
youknowone merged 4 commits intoRustPython:mainfrom
moreal:complex-related

Conversation

@moreal
Copy link
Contributor

@moreal moreal commented Mar 17, 2026

This pull request does things:

  • Upgrade test.test_complex to CPython 3.14.3
  • Implement complex.from_number
  • Correct complex.__eq__ behavior where complex(float('nan'), float('nan')) == complex(float('nan'), float('nan')) was True.

As a note, test_truediv and test_mul fail because complex(float('inf'), float('inf')) * 1 returns nan+nanj. It is num-complex's bug and I'll see it later.

Summary by CodeRabbit

  • New Features

    • Added a new class method to construct complex numbers from numeric inputs, with proper subclassing support and preservation of existing behavior for complex and real number inputs.
  • Bug Fixes

    • Simplified complex number equality comparison by removing special NaN handling, resulting in more consistent equality semantics.

CPython Developers and others added 4 commits March 17, 2026 19:11
test_mul and test_truediv fail due to incorrect INF/NAN handling
in complex arithmetic operations.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 17, 2026

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: 152a9754-6a3b-423c-90ae-7f8b35dea5ca

📥 Commits

Reviewing files that changed from the base of the PR and between 2a61237 and b8c2077.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_complex.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/builtins/complex.rs

📝 Walkthrough

Walkthrough

Added a new PyComplex.from_number classmethod for constructing complex numbers from various numeric types while preserving subclassing semantics. Updated imports to include Callable. Simplified complex equality comparison by removing NaN-special-case handling.

Changes

Cohort / File(s) Summary
New from_number classmethod and imports
crates/vm/src/builtins/complex.rs
Added PyComplex.from_number classmethod to construct complex from numbers with subclassing support; added Callable to public type exports.
Equality logic simplification
crates/vm/src/builtins/complex.rs
Removed NaN-for-NaN special-case equality handling; now uses direct value comparison for PyComplex operands.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Add float.from_number classmethod #7107: Adds identical from_number classmethod pattern to PyFloat, implementing the same numeric type conversion and subclassing semantics across multiple numeric builtins.

Suggested reviewers

  • youknowone

Poem

🐰 Complex numbers now have a second way,
From any number, come what may,
The from_number classmethod's here to stay,
Converting with grace, no special NaN play,
Subclasses rejoice, hip hip hooray! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Implement complex.from_number' directly and clearly describes the primary change — adding a new classmethod to PyComplex, which is the main substantive code addition in this PR.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

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 and usage tips.

@github-actions
Copy link
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] test: cpython/Lib/test/test_complex.py (TODO: 4)

dependencies:

dependent tests: (no tests depend on complex)

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@moreal moreal self-assigned this Mar 17, 2026
@moreal moreal marked this pull request as ready for review March 17, 2026 11:12
@youknowone youknowone merged commit f868f81 into RustPython:main Mar 17, 2026
16 checks passed
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.

2 participants