GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ast: Populate _field_types with real type objects by youknowone · Pull Request #6981 · RustPython/RustPython

Conversation

@youknowone

@youknowone youknowone commented Feb 3, 2026 •
edited by coderabbitai Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • 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.

All reactions
@youknowone
Populate _field_types with real type objects
2f70979
- 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

coderabbitai Bot commented Feb 3, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
📝 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
  • X
  • Mastodon
  • Reddit
  • LinkedIn

Comment @coderabbitai help to get the list of available commands and usage tips.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@youknowone
youknowone marked this pull request as ready for review February 3, 2026 12:56
@youknowone
Merge branch 'main' into ast
b45cea5
@github-actions

github-actions Bot commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

📦 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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@youknowone
youknowone merged commit 8d07c45 into RustPython:main Feb 3, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@youknowone
youknowone deleted the ast branch February 3, 2026 13:05
@coderabbitai coderabbitai Bot mentioned this pull request Aug 13, 2026
Fix 33 reproduced fuzzing and static-review defects #8514
Merged
youknowone added a commit that referenced this pull request Sep 16, 2026
@youknowone
Populate _field_types with real type objects (#6981)
7f69bde
- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

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.

1 participant

@youknowone
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.