Use empty list as default according to Python.asdl #6812
Merged
+22
−10
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.
Overview
This pull request corrects some
Node::ast_to_objectimplementations according to CPython ASDL. SoClassDef.type_params,ClassDef.keywords,ClassDef.basesandFunctionDef.type_paramsbecame to use[]as default because they have*modifier in Python.asdl.Background
When running
cargo run --release -q -- scripts/update_lib auto-mark Lib/test/test_warnings, I met the following logs:The source lines are:
So we can reproduce with the following code:
CPython does:
RustPython (before this patch) does:
CPython behavior
https://github.com/python/cpython/blob/71cbffde61449a224dffcae937f5f6be4f86ad09/InternalDocs/compiler.md#L89-L94
To quote the definitions of FunctionDef and ClassDef from Python.asdl:
So
ClassDef.type_params,ClassDef.keywords,ClassDef.basesandFunctionDef.type_paramsshould be[]because they have*modifier.CPython internal (at 3.14.2)
- `ast.parse` calls `builtins.compile`. - Internally, it calls `ast2obj_mod` through `builtin_compile_impl`, `Py_CompileStringObject`, `PyAST_mod2obj`. - It calls `ast2obj_stmt` (corresponding to `StmtClassDef`) https://github.com/python/cpython/blob/v3.14.2/Python/Python-ast.c#L8888 ```c // ast2obj_mod(struct ast_state *state, void* _o) case Module_kind: tp = (PyTypeObject *)state->Module_type; result = PyType_GenericNew(tp, NULL, NULL); if (!result) goto failed; value = ast2obj_list(state, (asdl_seq*)o->v.Module.body, ast2obj_stmt); // ast2obj_stmt(struct ast_state *state, void* _o) case ClassDef_kind: tp = (PyTypeObject *)state->ClassDef_type; result = PyType_GenericNew(tp, NULL, NULL); if (!result) goto failed; value = ast2obj_identifier(state, o->v.ClassDef.name); if (!value) goto failed; if (PyObject_SetAttr(result, state->name, value) == -1) goto failed; Py_DECREF(value); value = ast2obj_list(state, (asdl_seq*)o->v.ClassDef.bases, ast2obj_expr); if (!value) goto failed; if (PyObject_SetAttr(result, state->bases, value) == -1) goto failed; ```Following lines are full implementation of
ast2obj_listin cpython 3.14.2, and theasdl_seq_LENmacro returns(?) 0 when the given param isNULL.So the
ast2obj_listreturns empty list if the givenseq(i.e.,bases) isNULL.Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.