Use `ast.unparse` for generating patches with `lib_updater.py` by ShaharNaveh · Pull Request #6142 · RustPython/RustPython
Walkthrough
Rewrites patch generation in scripts/lib_updater.py from string-based formatting to AST-driven decorator construction, adds UtMethod.has_args, refactors reason extraction logic to AST/surrounding-source paths, introduces new constants, updates JSON load/show handling, and standardizes indentation and unittest aliasing.
Changes
| Cohort / File(s) | Summary of changes |
|---|---|
AST-driven patch generation and extractionscripts/lib_updater.py |
Replaced string fmt-based decorator generation with AST-based methods (PatchSpec._attr_node, as_ast_node, as_decorator, _reason); added UtMethod.has_args; revised Phase 1/2 emissions to use as_decorator() and textwrap.indent with DEFAULT_INDENT; updated reason/cond extraction via AST for arg-ful UT methods and surrounding source scan for arg-less; introduced DEFAULT_INDENT, COMMENT, UT alias; updated JSON load/show to handle UtMethod conversion and spec._asdict(); removed PatchSpec.fmt. |
Sequence Diagram(s)
sequenceDiagram
actor Dev as Developer/CLI
participant LU as LibUpdater
participant AST as Python AST
participant SF as Source File
participant JSON as Patches JSON
rect rgb(235, 245, 255)
note right of LU: Phase 1/2 Patch Emission (AST-driven)
Dev->>LU: run update
LU->>AST: build decorator nodes (PatchSpec.as_ast_node)
AST-->>LU: ast.Attribute / ast.Call
LU->>LU: render as_decorator() and indent with DEFAULT_INDENT
LU->>SF: write decorators and wrappers
end
rect rgb(245, 235, 255)
note right of LU: Patch Extraction (reason/cond)
LU->>AST: parse source for UT methods
alt UT method has_args()
LU->>AST: walk dec_node for COMMENT string and optional cond
AST-->>LU: reason, cond
else No args
LU->>SF: scan surrounding lines for COMMENT
SF-->>LU: reason line
end
LU->>LU: normalize reason via removeprefix(COMMENT)
end
rect rgb(235, 255, 240)
note right of LU: JSON I/O
LU->>JSON: load patches
JSON-->>LU: dicts
LU->>LU: convert ut_method -> UtMethod via _replace
LU-->>Dev: show-patches via spec._asdict()
end
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~60 minutes
Possibly related PRs
- Add tool for easier test updates #6089: Overlaps scripts/lib_updater.py, modifying PatchSpec/UtMethod and patch generation/extraction; likely part of or precursor to this AST-based refactor.
Suggested reviewers
- reactive-firewall
- youknowone
Pre-merge checks (1 passed, 1 warning, 1 inconclusive)
❌ Failed checks (1 warning, 1 inconclusive)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
|
| Description Check | ❓ Inconclusive | The pull request currently lacks a dedicated description outlining its scope or details, making it unclear how the changes align with project goals even though a raw summary is provided. | Please add a clear description summarizing the key changes and intent of the pull request so reviewers can quickly understand its purpose. |
✅ Passed checks (1 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title Check | ✅ Passed | The title succinctly captures the primary change—switching lib_updater.py’s patch generation to leverage ast.unparse—and is both concise and specific enough for teammates to quickly grasp the new implementation approach without extraneous detail. |
Poem
A nibble of AST, a sprinkle of reason,
I hop through phases, patching in season.
No strings to fray—just nodes that align,
Indents in step, decorators divine.
With whiskers a-twitch, I merge and I test—
Hippity-hop, this update’s the best! 🐇✨
Tip
👮 Agentic pre-merge checks are now available in preview!
Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
- Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
- Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.
Example:
reviews: pre_merge_checks: custom_checks: - name: "Undocumented Breaking Changes" mode: "warning" instructions: | Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).
Please share your feedback with us on this Discord post.
✨ Finishing Touches
- 📝 Generate Docstrings
🧪 Generate unit tests
- 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.
Comment @coderabbitai help to get the list of available commands and usage tips.