Skip to content

Commit 6fc0479

Browse files
committed
Add RustPython runtime AST fields (#2)
Assisted-by: Codex:GPT-5
1 parent 6849bab commit 6fc0479

630 files changed

Lines changed: 9862 additions & 758 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ruff_python_ast/Cargo.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ license = { workspace = true }
1515
ignored = ["ruff_cache"]
1616

1717
[lib]
18+
name = "ruff_python_ast"
1819

1920
[dependencies]
2021
# ruff_cache = { workspace = true, optional = true }
@@ -42,14 +43,13 @@ thin-vec = { workspace = true }
4243
[features]
4344
schemars = ["dep:schemars", "dep:serde_json"]
4445
# cache = ["dep:ruff_cache", "dep:ruff_macros"]
45-
# serde = [
46-
# "dep:serde",
47-
# "ruff_text_size/serde",
48-
# "dep:ruff_cache",
49-
# "char_str/serde",
50-
# "compact_str/serde",
51-
# "thin-vec/serde",
52-
# ]
46+
serde = [
47+
"dep:serde",
48+
"ruff_text_size/serde",
49+
"char_str/serde",
50+
"compact_str/serde",
51+
"thin-vec/serde",
52+
]
5353
get-size = ["dep:get-size2", "char_str/get-size", "ruff_text_size/get-size"]
5454
salsa = ["dep:salsa"]
5555

crates/ruff_python_ast/ast.toml

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ doc = "See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)"
6969

7070
[Mod.nodes.ModModule]
7171
doc = "See also [Module](https://docs.python.org/3/library/ast.html#ast.Module)"
72-
fields = [{ name = "body", type = "ThinVec<Stmt>" }]
72+
fields = [
73+
{ name = "body", type = "ThinVec<Stmt>" },
74+
{ name = "runtime_body", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
75+
]
7376

7477
[Mod.nodes.ModExpression]
7578
doc = "See also [Module](https://docs.python.org/3/library/ast.html#ast.Module)"
@@ -93,6 +96,10 @@ fields = [
9396
{ name = "parameters", type = "Box<crate::Parameters>" },
9497
{ name = "returns", type = "Expr?", is_annotation = true },
9598
{ name = "body", type = "ThinVec<Stmt>" },
99+
{ name = "runtime_decorator_list", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
100+
{ name = "runtime_type_comment", type = "Box<str>?", skip_visit = true },
101+
{ name = "runtime_type_comment_bytes", type = "Vec<u8>?", skip_visit = true },
102+
{ name = "runtime_body", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
96103
]
97104

98105
[Stmt.nodes.StmtClassDef]
@@ -103,6 +110,8 @@ fields = [
103110
{ name = "type_params", type = "Box<crate::TypeParams>?" },
104111
{ name = "arguments", type = "Box<crate::Arguments>?" },
105112
{ name = "body", type = "ThinVec<Stmt>" },
113+
{ name = "runtime_decorator_list", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
114+
{ name = "runtime_body", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
106115
]
107116

108117
[Stmt.nodes.StmtReturn]
@@ -111,7 +120,10 @@ fields = [{ name = "value", type = "Expr?" }]
111120

112121
[Stmt.nodes.StmtDelete]
113122
doc = "See also [Delete](https://docs.python.org/3/library/ast.html#ast.Delete)"
114-
fields = [{ name = "targets", type = "Expr*" }]
123+
fields = [
124+
{ name = "targets", type = "Expr*" },
125+
{ name = "runtime_targets", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
126+
]
115127

116128
[Stmt.nodes.StmtTypeAlias]
117129
doc = "See also [TypeAlias](https://docs.python.org/3/library/ast.html#ast.TypeAlias)"
@@ -126,6 +138,9 @@ doc = "See also [Assign](https://docs.python.org/3/library/ast.html#ast.Assign)"
126138
fields = [
127139
{ name = "targets", type = "Expr*" },
128140
{ name = "value", type = "Expr" },
141+
{ name = "runtime_targets", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
142+
{ name = "runtime_type_comment", type = "Box<str>?", skip_visit = true },
143+
{ name = "runtime_type_comment_bytes", type = "Vec<u8>?", skip_visit = true },
129144
]
130145

131146
[Stmt.nodes.StmtAugAssign]
@@ -143,6 +158,7 @@ fields = [
143158
{ name = "annotation", type = "Expr", is_annotation = true },
144159
{ name = "value", type = "Expr?" },
145160
{ name = "simple", type = "bool" },
161+
{ name = "runtime_simple", type = "i32?", skip_visit = true },
146162
]
147163

148164
[Stmt.nodes.StmtFor]
@@ -156,6 +172,10 @@ fields = [
156172
{ name = "iter", type = "Expr" },
157173
{ name = "body", type = "ThinVec<Stmt>" },
158174
{ name = "orelse", type = "ThinVec<Stmt>" },
175+
{ name = "runtime_type_comment", type = "Box<str>?", skip_visit = true },
176+
{ name = "runtime_type_comment_bytes", type = "Vec<u8>?", skip_visit = true },
177+
{ name = "runtime_body", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
178+
{ name = "runtime_orelse", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
159179
]
160180

161181
[Stmt.nodes.StmtWhile]
@@ -165,6 +185,8 @@ fields = [
165185
{ name = "test", type = "Expr" },
166186
{ name = "body", type = "ThinVec<Stmt>" },
167187
{ name = "orelse", type = "ThinVec<Stmt>" },
188+
{ name = "runtime_body", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
189+
{ name = "runtime_orelse", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
168190
]
169191

170192
[Stmt.nodes.StmtIf]
@@ -174,6 +196,7 @@ fields = [
174196
{ name = "test", type = "Expr" },
175197
{ name = "body", type = "ThinVec<Stmt>" },
176198
{ name = "elif_else_clauses", type = "ElifElseClause*" },
199+
{ name = "runtime_body", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
177200
]
178201

179202
[Stmt.nodes.StmtWith]
@@ -185,6 +208,9 @@ fields = [
185208
{ name = "is_async", type = "bool" },
186209
{ name = "items", type = "WithItem*" },
187210
{ name = "body", type = "ThinVec<Stmt>" },
211+
{ name = "runtime_type_comment", type = "Box<str>?", skip_visit = true },
212+
{ name = "runtime_type_comment_bytes", type = "Vec<u8>?", skip_visit = true },
213+
{ name = "runtime_body", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
188214
]
189215

190216
[Stmt.nodes.StmtMatch]
@@ -207,6 +233,10 @@ fields = [
207233
{ name = "orelse", type = "ThinVec<Stmt>" },
208234
{ name = "finalbody", type = "ThinVec<Stmt>" },
209235
{ name = "is_star", type = "bool" },
236+
{ name = "runtime_body", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
237+
{ name = "runtime_handlers", type = "Vec<Option<crate::ExceptHandler>>?", skip_visit = true },
238+
{ name = "runtime_orelse", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
239+
{ name = "runtime_finalbody", type = "Vec<Option<crate::Stmt>>?", skip_visit = true },
210240
]
211241

212242
[Stmt.nodes.StmtAssert]
@@ -227,6 +257,7 @@ fields = [
227257
{ name = "names", type = "Alias*" },
228258
{ name = "level", type = "u32" },
229259
{ name = "is_lazy", type = "bool" },
260+
{ name = "runtime_level", type = "i32?", skip_visit = true },
230261
]
231262

232263
[Stmt.nodes.StmtGlobal]
@@ -321,7 +352,11 @@ doc = "See also [expr](https://docs.python.org/3/library/ast.html#ast.expr)"
321352

322353
[Expr.nodes.ExprBoolOp]
323354
doc = "See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp)"
324-
fields = [{ name = "op", type = "BoolOp" }, { name = "values", type = "Expr*" }]
355+
fields = [
356+
{ name = "op", type = "BoolOp" },
357+
{ name = "values", type = "Expr*" },
358+
{ name = "runtime_values", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
359+
]
325360
custom_source_order = true
326361

327362
[Expr.nodes.ExprNamed]
@@ -361,12 +396,18 @@ source_order = ["body", "test", "orelse"]
361396

362397
[Expr.nodes.ExprDict]
363398
doc = "See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict)"
364-
fields = [{ name = "items", type = "DictItem*" }]
399+
fields = [
400+
{ name = "items", type = "DictItem*" },
401+
{ name = "runtime_values", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
402+
]
365403
custom_source_order = true
366404

367405
[Expr.nodes.ExprSet]
368406
doc = "See also [Set](https://docs.python.org/3/library/ast.html#ast.Set)"
369-
fields = [{ name = "elts", type = "Expr*" }]
407+
fields = [
408+
{ name = "elts", type = "Expr*" },
409+
{ name = "runtime_elts", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
410+
]
370411

371412
[Expr.nodes.ExprListComp]
372413
doc = "See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp)"
@@ -416,6 +457,7 @@ fields = [
416457
{ name = "left", type = "Expr" },
417458
{ name = "ops", type = "Box<[CmpOp]>" },
418459
{ name = "comparators", type = "Box<[Expr]>" },
460+
{ name = "runtime_comparators", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
419461
]
420462
# The fields must be visited simultaneously
421463
custom_source_order = true
@@ -444,7 +486,11 @@ doesn't join the implicitly concatenated parts into a single string. Instead,
444486
it keeps them separate and provide various methods to access the parts.
445487
446488
See also [JoinedStr](https://docs.python.org/3/library/ast.html#ast.JoinedStr)"""
447-
fields = [{ name = "value", type = "FStringValue" }]
489+
fields = [
490+
{ name = "value", type = "FStringValue" },
491+
{ name = "runtime_joined_str", type = "Vec<crate::Expr>?", skip_visit = true },
492+
{ name = "runtime_values", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
493+
]
448494
custom_source_order = true
449495

450496
[Expr.nodes.ExprTString]
@@ -456,7 +502,11 @@ doesn't join the implicitly concatenated parts into a single string. Instead,
456502
it keeps them separate and provide various methods to access the parts.
457503
458504
See also [TemplateStr](https://docs.python.org/3/library/ast.html#ast.TemplateStr)"""
459-
fields = [{ name = "value", type = "TStringValue" }]
505+
fields = [
506+
{ name = "value", type = "TStringValue" },
507+
{ name = "runtime_template_str", type = "Vec<crate::Expr>?", skip_visit = true },
508+
{ name = "runtime_values", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
509+
]
460510
custom_source_order = true
461511

462512
[Expr.nodes.ExprStringLiteral]
@@ -488,6 +538,14 @@ derives = ["Default"]
488538
fields = []
489539
derives = ["Default"]
490540

541+
[Expr.nodes.ExprConstant]
542+
doc = "See also [Constant](https://docs.python.org/3/library/ast.html#ast.Constant)"
543+
fields = [
544+
{ name = "value", type = "ConstantValue", skip_visit = true },
545+
{ name = "kind", type = "Box<str>?", skip_visit = true },
546+
{ name = "invalid_type", type = "Box<str>?", skip_visit = true },
547+
]
548+
491549
[Expr.nodes.ExprAttribute]
492550
doc = "See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute)"
493551
fields = [
@@ -523,6 +581,7 @@ doc = "See also [List](https://docs.python.org/3/library/ast.html#ast.List)"
523581
fields = [
524582
{ name = "elts", type = "Expr*" },
525583
{ name = "ctx", type = "ExprContext" },
584+
{ name = "runtime_elts", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
526585
]
527586

528587
[Expr.nodes.ExprTuple]
@@ -531,6 +590,7 @@ fields = [
531590
{ name = "elts", type = "Expr*" },
532591
{ name = "ctx", type = "ExprContext" },
533592
{ name = "parenthesized", type = "bool" },
593+
{ name = "runtime_elts", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
534594
]
535595

536596
[Expr.nodes.ExprSlice]
@@ -583,14 +643,19 @@ fields = [{ name = "value", type = "Singleton" }]
583643

584644
[Pattern.nodes.PatternMatchSequence]
585645
doc = "See also [MatchSequence](https://docs.python.org/3/library/ast.html#ast.MatchSequence)"
586-
fields = [{ name = "patterns", type = "Pattern*" }]
646+
fields = [
647+
{ name = "patterns", type = "Pattern*" },
648+
{ name = "runtime_patterns", type = "Vec<Option<crate::Pattern>>?", skip_visit = true },
649+
]
587650

588651
[Pattern.nodes.PatternMatchMapping]
589652
doc = "See also [MatchMapping](https://docs.python.org/3/library/ast.html#ast.MatchMapping)"
590653
fields = [
591654
{ name = "keys", type = "ThinVec<Expr>" },
592655
{ name = "patterns", type = "ThinVec<Pattern>" },
593656
{ name = "rest", type = "Identifier?" },
657+
{ name = "runtime_keys", type = "Vec<Option<crate::Expr>>?", skip_visit = true },
658+
{ name = "runtime_patterns", type = "Vec<Option<crate::Pattern>>?", skip_visit = true },
594659
]
595660
custom_source_order = true
596661

@@ -599,6 +664,9 @@ doc = "See also [MatchClass](https://docs.python.org/3/library/ast.html#ast.Matc
599664
fields = [
600665
{ name = "cls", type = "Box<Expr>" },
601666
{ name = "arguments", type = "PatternArguments" },
667+
{ name = "runtime_patterns", type = "Vec<Option<crate::Pattern>>?", skip_visit = true },
668+
{ name = "runtime_kwd_attrs", type = "Vec<crate::Identifier>?", skip_visit = true },
669+
{ name = "runtime_kwd_patterns", type = "Vec<Option<crate::Pattern>>?", skip_visit = true },
602670
]
603671

604672
[Pattern.nodes.PatternMatchStar]
@@ -614,7 +682,10 @@ fields = [
614682

615683
[Pattern.nodes.PatternMatchOr]
616684
doc = "See also [MatchOr](https://docs.python.org/3/library/ast.html#ast.MatchOr)"
617-
fields = [{ name = "patterns", type = "Pattern*" }]
685+
fields = [
686+
{ name = "patterns", type = "Pattern*" },
687+
{ name = "runtime_patterns", type = "Vec<Option<crate::Pattern>>?", skip_visit = true },
688+
]
618689

619690
[TypeParam]
620691
doc = "See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param)"

crates/ruff_python_ast/generate.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"Alias",
4545
"Singleton",
4646
"PatternArguments",
47+
"ConstantValue",
4748
}
4849

4950

@@ -276,10 +277,10 @@ def __init__(self, rule: str) -> None:
276277
rule = rule[:-1]
277278

278279
self.sequence_kind, self.name = split_sequence_type(rule)
279-
if self.optional and self.sequence_kind is not None:
280+
if self.optional and self.sequence_kind is not None and "<" not in rule:
280281
raise ValueError(f"optional field cannot be sequence or slice: {self.rule}")
281282
if self.sequence_kind is not None and (
282-
not self.name or any(ch in self.name for ch in "?*&[]<>")
283+
not self.name or any(ch in self.name for ch in "?*&[]")
283284
):
284285
raise ValueError(f"Invalid collection element type: {rule}")
285286

0 commit comments

Comments
 (0)