Skip to content

ast: Populate _field_types with real type objects#6981

Merged
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:ast
Feb 3, 2026
Merged

ast: Populate _field_types with real type objects#6981
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:ast

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Feb 3, 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

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.

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

coderabbitai bot commented Feb 3, 2026

📝 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 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.

@youknowone youknowone marked this pull request as ready for review February 3, 2026 12:56
@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

📦 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:

  • ast

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

@youknowone youknowone merged commit 8d07c45 into RustPython:main Feb 3, 2026
13 checks passed
@youknowone youknowone deleted the ast branch February 3, 2026 13:05
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