Skip to content

Commit 61ddd98

Browse files
Copilotyouknowonegithub-actions[bot]
authored
Handle missing type_params on AST Function/Class/TypeAlias nodes (#6512)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent a5a1173 commit 61ddd98

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

crates/vm/src/stdlib/ast/statement.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl Node for ruff::StmtFunctionDef {
257257
type_params: Node::ast_from_object(
258258
_vm,
259259
source_file,
260-
get_node_field(_vm, &_object, "type_params", "FunctionDef")?,
260+
get_node_field_opt(_vm, &_object, "type_params")?.unwrap_or_else(|| _vm.ctx.none()),
261261
)?,
262262
range: range_from_object(_vm, source_file, _object, "FunctionDef")?,
263263
is_async,
@@ -341,7 +341,7 @@ impl Node for ruff::StmtClassDef {
341341
type_params: Node::ast_from_object(
342342
_vm,
343343
source_file,
344-
get_node_field(_vm, &_object, "type_params", "ClassDef")?,
344+
get_node_field_opt(_vm, &_object, "type_params")?.unwrap_or_else(|| _vm.ctx.none()),
345345
)?,
346346
range: range_from_object(_vm, source_file, _object, "ClassDef")?,
347347
})
@@ -503,7 +503,7 @@ impl Node for ruff::StmtTypeAlias {
503503
type_params: Node::ast_from_object(
504504
_vm,
505505
source_file,
506-
get_node_field(_vm, &_object, "type_params", "TypeAlias")?,
506+
get_node_field_opt(_vm, &_object, "type_params")?.unwrap_or_else(|| _vm.ctx.none()),
507507
)?,
508508
value: Node::ast_from_object(
509509
_vm,

extra_tests/snippets/stdlib_types.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import _ast
2+
import platform
13
import types
24

35
from testutils import assert_raises
@@ -8,3 +10,31 @@
810
assert ns.b == "Rust"
911
with assert_raises(AttributeError):
1012
_ = ns.c
13+
14+
15+
def _run_missing_type_params_regression():
16+
args = _ast.arguments(
17+
posonlyargs=[],
18+
args=[],
19+
vararg=None,
20+
kwonlyargs=[],
21+
kw_defaults=[],
22+
kwarg=None,
23+
defaults=[],
24+
)
25+
fn = _ast.FunctionDef("f", args, [], [], None, None)
26+
fn.lineno = 1
27+
fn.col_offset = 0
28+
fn.end_lineno = 1
29+
fn.end_col_offset = 0
30+
mod = _ast.Module([fn], [])
31+
mod.lineno = 1
32+
mod.col_offset = 0
33+
mod.end_lineno = 1
34+
mod.end_col_offset = 0
35+
compiled = compile(mod, "<stdlib_types_missing_type_params>", "exec")
36+
exec(compiled, {})
37+
38+
39+
if platform.python_implementation() == "RustPython":
40+
_run_missing_type_params_regression()

0 commit comments

Comments
 (0)