GeezerVison is showing github.com — ast: Populate _field_types with real type objects by youknowone · Pull Request #6981 · RustPython/RustPython
Conversation
youknowone
commented
Feb 3, 2026
•
edited by coderabbitai
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
- Add FieldType enum and FIELD_TYPES static table mapping all AST node classes to their ASDL field types
- Resolve markers to real Python type objects (GenericAlias, Union, plain types) at module init in populate_field_types()
- Set class-level None defaults for optional fields
- Replace hardcoded LIST_FIELDS and class-name checks in slot_init with _field_types-based lookup
- Add expr_context default to Load(), fix ImportFrom.level default (now None instead of 0)
- Fix class_getitem None check, compile() formatting
Summary by CodeRabbit
- Refactor
- Improved internal handling of AST field type resolution and default value assignment to enhance maintainability and flexibility in the runtime environment.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
- Add FieldType enum and FIELD_TYPES static table mapping all
AST node classes to their ASDL field types
- Resolve markers to real Python type objects (GenericAlias,
Union, plain types) at module init in populate_field_types()
- Set class-level None defaults for optional fields
- Replace hardcoded LIST_FIELDS and class-name checks in
slot_init with _field_types-based lookup
- Add expr_context default to Load(), fix ImportFrom.level
default (now None instead of 0)
- Fix __class_getitem__ None check, compile() formatting
- Remove 14 @expectedfailure decorators from test_ast
coderabbitai
Bot
commented
Feb 3, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
📝 Walkthrough
Walkthrough
This PR refactors the AST field initialization system by introducing a type-driven approach. It adds a FieldType enum, a FIELD_TYPES constant enumerating AST class fields, and a populate_field_types function to dynamically resolve and populate _field_types attributes on AST classes at module initialization, replacing prior hard-coded defaulting logic.
Changes
| Cohort / File(s) |
Summary |
AST Type Metadata Infrastructure crates/vm/src/stdlib/ast/pyast.rs |
Introduces FieldType enum to categorize field types (Node, Builtin, ListOf, Optional, etc.), adds FIELD_TYPES constant with per-class field metadata, and implements populate_field_types function that resolves builtin types, AST node references, and constructs generic aliases and unions for list and optional fields, then populates _field_types attributes at runtime. |
Field Defaulting Logic crates/vm/src/stdlib/ast/python.rs |
Replaces per-node hard-coded defaults with field-type-driven approach using _field_types metadata; now applies empty list defaults for list fields, None for optional fields, Load context for expression contexts, and skips defaults otherwise. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
#6011: Modifies AST node initialization logic in the same python.rs file; introduces custom Constructor for NodeAst which overlaps with the field defaulting mechanism refactored in this PR.
Poem
🐰 Hops with delight...
Field types now dance in metadata's glow,
No more hard-coded defaults we sow,
Types resolve at runtime, a structured way,
Lists, optionals, nodes—all here to stay!
The AST now speaks the language so true. 🌿✨
🚥 Pre-merge checks | ✅ 2 | ❌ 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%. |
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 'ast: Populate _field_types with real type objects' accurately and concisely summarizes the main change: implementing the populate_field_types() function that resolves AST field type annotations to actual Python type objects at runtime. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
🧪 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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
📦 Library Dependencies
The following Lib/ modules were modified. Here are their dependencies:
[x] lib: cpython/Lib/ast.py
[x] lib: cpython/Lib/_ast_unparse.py
[x] test: cpython/Lib/test/test_unparse.py (TODO: 6)
[x] test: cpython/Lib/test/test_type_comments.py (TODO: 15)
dependencies:
dependent tests: (48 tests)
- ast: test_ast test_builtin test_compile test_dis test_fstring test_future_stmt test_site test_ssl test_type_comments test_ucn test_unparse
- annotationlib: test_functools test_inspect test_reprlib test_typing
- inspect: test_abc test_argparse test_asyncgen test_code test_collections test_coroutines test_decimal test_enum test_generators test_grammar test_ntpath test_operator test_patma test_posixpath test_signal test_traceback test_types test_unittest test_yield_from test_zipimport
- dbm.dumb: test_dbm_dumb
- inspect:
- bdb: test_bdb
- dataclasses: test__colorize test_genericalias test_pprint test_regrtest test_zoneinfo
- importlib.metadata: test_importlib
- rlcompleter: test_rlcompleter
- trace: test_trace
- xmlrpc.server: test_docxmlrpc test_xmlrpc
- pyclbr: test_pyclbr
Legend:
[+] path exists in CPython
[x] up-to-date, [ ] outdated
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
youknowone
added a commit
that referenced
this pull request
Sep 16, 2026
- Add FieldType enum and FIELD_TYPES static table mapping all
AST node classes to their ASDL field types
- Resolve markers to real Python type objects (GenericAlias,
Union, plain types) at module init in populate_field_types()
- Set class-level None defaults for optional fields
- Replace hardcoded LIST_FIELDS and class-name checks in
slot_init with _field_types-based lookup
- Add expr_context default to Load(), fix ImportFrom.level
default (now None instead of 0)
- Fix __class_getitem__ None check, compile() formatting
- Remove 14 @expectedfailure decorators from test_ast
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|