Skip to content

Accept the format keyword in Struct initialization - #9064

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:fix-struct-format-keyword
Oct 10, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowdot:fix-struct-format-keyword

Conversation

@youknowdot

@youknowdot youknowdot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Extract the Python 3.14-compatible Struct(format=...) argument binding from #8954.

Bind the existing format converter as one required positional-or-keyword field. This accepts keyword construction and initialization while preserving the current constructor, converter, parser, state replacement, and subclass behavior. No 3.15 initialization tracking, reinitialization warning, or error-type change is included.

Both CPython 3.14.7 and 3.14.8 accept format by position or keyword. Main currently binds the converter through its positional-only blanket implementation. The related changes in #8950 concern non-ASCII errors and uninitialized .format, not this binder.

Add a small regression to the existing struct snippet for str/bytes keyword construction, inherited initialization, successful keyword reinitialization, and failed parsing preserving the prior usable state. Existing positional cases remain unchanged.

Validation

  • Independent source/ownership review, normal repository hooks, and native debug build passed.
  • Exact CPython 3.14.7 and native full struct snippets pass; the prior native implementation fails the new keyword regression.
  • Exact CPython 3.14.7/3.14.8 and native bounded checks agree on inherited/forwarding initialization, str/bytes format inputs, successful reinitialization, and 28 invalid-binding/state-preservation controls.
  • Four unchanged canonical struct initialization/format tests pass.
  • All four struct and struct-complex snippet pytest entries pass with the pinned 3.14.7 reference.
  • Stdlib and separately configured CAPI Clippy pass with -D warnings.

Hosted CI is pending.

The generic argument binder's pre-existing diagnostic wording is not claimed to match every CPython argument-error message. No canonical assertions, README wording, or implementation-name guards are changed.

Summary by CodeRabbit

  • Bug Fixes
    • struct.Struct now accepts format as a keyword argument when creating or reinitializing an instance. String and bytes formats are supported, and invalid formats raise struct.error without replacing the existing format.

Keep the existing conversion and initialization path while binding format as positional-or-keyword, matching Python 3.14.

Assisted-by: OpenAI Codex:model identifier unavailable
@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9ba8b9b7-57fc-49a4-983b-74d1ecf8a12b

📥 Commits

Reviewing files that changed from the base of the PR and between d674ea1 and 717ac1f.


📒 Files selected for processing (2)
  • crates/stdlib/src/pystruct.rs
  • extra_tests/snippets/stdlib_struct.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.



📝 Walkthrough

Walkthrough

Struct.__init__ now parses its arguments through StructArgs and extracts the format value before parsing it. Tests cover keyword-based construction and reinitialization for struct.Struct and a subclass.

Changes

Struct format initialization

Layer / File(s) Summary
Parse and validate format initialization
crates/stdlib/src/pystruct.rs, extra_tests/snippets/stdlib_struct.py
Struct.__init__ extracts format from StructArgs before parsing it. Tests cover string and bytes formats, packing and unpacking, subclass behavior, and preservation of the existing format after invalid reinitialization.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: youknowone


Merge Risk: ⚪ Minimal · up to 717ac

The change appears mergeable after normal checks, including completion of hosted CI.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: accepting the format keyword during Struct initialization.
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.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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 October 10, 2026 12:56
@youknowone
youknowone enabled auto-merge (squash) October 10, 2026 12:56
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T13:03:02.333747Z 717ac1f Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codspeed

codspeed Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing youknowdot:fix-struct-format-keyword (717ac1f) with main (d674ea1)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@youknowone
youknowone merged commit 1ff5bbd into RustPython:main Oct 10, 2026
22 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