Update _ast_unparse and fix unparse, type_comments by youknowone · Pull Request #6961 · RustPython/RustPython
📝 Walkthrough
Walkthrough
Convert internal panics/asserts into explicit syntax errors in codegen; guard truncated string-literal parsing; add t-string unparse support; restrict type subscripting to non-None __class_getitem__; add AST field-defaulting, default_value for type-parameter nodes, and always emit type_comment = None for certain nodes; implement PyCF_ONLY_AST short-circuit in compile().
Changes
| Cohort / File(s) | Summary |
|---|---|
Codegen / Pattern validation crates/codegen/src/compile.rs |
Replace panics/asserts with explicit self.error(...) returns for forbidden-name checks, class-pattern (too many subpatterns), and MatchOr size ≤ 1; tighten error handling. |
String parsing & t-string unparse crates/codegen/src/string_parser.rs, crates/codegen/src/unparse.rs |
Guard parse_string_literal against truncated sources (return raw source when too short); add unparse_tstring(&TStringValue) and route TString rendering through it using fstring-body composition + UnicodeEscape. |
VM protocol / subscripting error crates/vm/src/protocol/object.rs |
Only call __class_getitem__ on types when attribute exists and is not None; otherwise raise TypeError: type '<Name>' is not subscriptable. |
AST init & defaulting crates/vm/src/stdlib/ast/python.rs, crates/vm/src/stdlib/ast/pyast.rs |
Track provided fields during AST init, mark built-ins with _field_types, default missing built-in fields (lists → [], others → None), and add default_value to TypeVar/ParamSpec/TypeVarTuple field lists. |
AST serialization / node emissions crates/vm/src/stdlib/ast/expression.rs, crates/vm/src/stdlib/ast/parameter.rs, crates/vm/src/stdlib/ast/statement.rs, crates/vm/src/stdlib/ast/type_parameters.rs, crates/vm/src/stdlib/ast.rs |
Emit args as empty_arguments_object for lambdas with no params; always emit type_comment = None for several nodes; add round-trip default_value handling for type-parameter nodes; add empty_arguments_object and mode_type_and_name helpers. |
Compile builtin: AST-only mode crates/vm/src/stdlib/builtins.rs, crates/vm/src/stdlib/ast.rs |
Implement PyCF_ONLY_AST handling: parse flags, map mode → expected AST node class, validate mode, and short-circuit by returning AST node when it matches expected type. |
Minor serialization cast change crates/vm/src/stdlib/ast/other.rs |
Serialize ConversionFlag as i8 instead of u8. |
Docs / scripts AGENTS.md, DEVELOPMENT.md |
Exclude additional crate rustpython-venvlauncher from workspace cargo test invocations. |
Sequence Diagram(s)
sequenceDiagram
participant Caller
participant BuiltinsCompile as builtins::compile
participant Parser
participant ASTNode as ParsedAST
Caller->>BuiltinsCompile: compile(source, filename, mode, flags)
BuiltinsCompile->>BuiltinsCompile: extract PyCF_ONLY_AST from flags
BuiltinsCompile->>Parser: parse(source) -> ParsedAST
alt PyCF_ONLY_AST set
BuiltinsCompile->>BuiltinsCompile: mode_type_and_name(mode) -> expected_type
BuiltinsCompile->>ASTNode: inspect node type
alt node.type == expected_type
BuiltinsCompile-->>Caller: return AST node (short-circuit)
else mismatch
BuiltinsCompile-->>Caller: raise TypeError (expected vs actual)
end
else normal compile
BuiltinsCompile->>BuiltinsCompile: continue compile pipeline
BuiltinsCompile-->>Caller: return compiled result
end
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~50 minutes
Possibly related PRs
- Py 3.14 changes fix #6755: related changes to forbidden-name/error handling in
crates/codegen/src/compile.rs. - Prohibit AI PR submit #6813: related VM stdlib/path initialization and stdlib_dir changes that touch the same VM areas.
Suggested labels
skip:ci
Suggested reviewers
- ShaharNaveh
Poem
🐇 I nudged the panics into softer sighs,
I checked the quotes where tiny truncation lies,
T-strings now sparkle, defaults tucked in place,
Subscripted types now fail with kinder grace,
🥕 A hop, a nibble—code in tidy files.
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 21.74% 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 refers to AST unparsing and type comments, which are core to multiple changes across the PR, particularly in unparse.rs, statement.rs, and parameter.rs files. |
✏️ 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.
Comment @coderabbitai help to get the list of available commands and usage tips.