Skip to content

Fix number __format__#6930

Merged
youknowone merged 1 commit intoRustPython:mainfrom
youknowone:number-format
Feb 1, 2026
Merged

Fix number __format__#6930
youknowone merged 1 commit intoRustPython:mainfrom
youknowone:number-format

Conversation

@youknowone
Copy link
Copy Markdown
Member

@youknowone youknowone commented Feb 1, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced formatting for numeric types (integers, floats, complex numbers) to provide improved support for user-defined subclasses
    • Refined string representation handling for numeric subclass instances while maintaining full backward compatibility
    • Optimized format specification processing for consistent behavior across standard and derived numeric types

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 1, 2026

📝 Walkthrough

Walkthrough

The PR refactors the __format__ method signatures in PyComplex, PyFloat, and PyInt builtins to accept Py<Self> instead of &self, adding early-return handling for empty format specs to return string representations.

Changes

Cohort / File(s) Summary
Builtin Format Method Refactoring
crates/vm/src/builtins/complex.rs, crates/vm/src/builtins/float.rs, crates/vm/src/builtins/int.rs
Updated __format__ method signatures from &self to zelf: &Py<Self> parameter. Added early return for empty format specs to return string representation via zelf.value. Maintains existing formatting behavior for non-empty specs.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 Three types now format with grace,
Py takes center place!
Empty specs hop quickly by,
String repr answers every cry—
Format magic, rabbit-approved! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix number format' directly describes the main change: refactoring format method signatures in numeric types (int, float, complex) to accept Py instead of &self.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
Copy Markdown
Contributor

github-actions bot commented Feb 1, 2026

📦 Library Dependencies

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

[ ] lib: cpython/Lib/enum.py
[ ] test: cpython/Lib/test/test_enum.py (TODO: 14)

dependencies:

  • enum (native: builtins, sys)
    • types

dependent tests: (10 tests)

  • enum: test_argparse test_ast test_enum test_httplib test_json test_patma test_signal test_ssl test_time test_uuid

[ ] lib: cpython/Lib/timeit.py
[x] test: cpython/Lib/test/test_timeit.py

dependencies:

  • timeit (native: gc, itertools, sys, time)

dependent tests: (1 tests)

  • timeit: test_timeit

Legend:

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

@youknowone youknowone marked this pull request as ready for review February 1, 2026 07:42
@youknowone youknowone merged commit 25bf682 into RustPython:main Feb 1, 2026
13 of 14 checks passed
@youknowone youknowone deleted the number-format branch February 1, 2026 08:17
@coderabbitai coderabbitai bot mentioned this pull request Feb 24, 2026
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