Skip to content

gh-117486: Improve behavior for user-defined AST subclasses #118212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Node classes

.. attribute:: _fields

Each concrete class has an attribute :attr:`_fields` which gives the names
Each concrete class has an attribute :attr:`!_fields` which gives the names
of all child nodes.

Each instance of a concrete class has one attribute for each child node,
Expand All @@ -74,6 +74,18 @@ Node classes
as Python lists. All possible attributes must be present and have valid
values when compiling an AST with :func:`compile`.

.. attribute:: _field_types

The :attr:`!_field_types` attribute on each concrete class is a dictionary
mapping field names (as also listed in :attr:`_fields`) to their types.

.. doctest::

>>> ast.TypeVar._field_types
{'name': <class 'str'>, 'bound': ast.expr | None, 'default_value': ast.expr | None}

.. versionadded:: 3.13

.. attribute:: lineno
col_offset
end_lineno
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ ast
argument that does not map to a field on the AST node is now deprecated,
and will raise an exception in Python 3.15.

These changes do not apply to user-defined subclasses of :class:`ast.AST`,
unless the class opts in to the new behavior by setting the attribute
:attr:`ast.AST._field_types`.

(Contributed by Jelle Zijlstra in :gh:`105858` and :gh:`117486`.)

* :func:`ast.parse` now accepts an optional argument *optimize*
which is passed on to the :func:`compile` built-in. This makes it
possible to obtain an optimized AST.
Expand Down
41 changes: 37 additions & 4 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3036,25 +3036,25 @@ def test_FunctionDef(self):
self.assertEqual(node.name, 'foo')
self.assertEqual(node.decorator_list, [])

def test_custom_subclass(self):
def test_custom_subclass_with_no_fields(self):
class NoInit(ast.AST):
pass

obj = NoInit()
self.assertIsInstance(obj, NoInit)
self.assertEqual(obj.__dict__, {})

def test_fields_but_no_field_types(self):
class Fields(ast.AST):
_fields = ('a',)

with self.assertWarnsRegex(DeprecationWarning,
r"Fields provides _fields but not _field_types."):
obj = Fields()
obj = Fields()
with self.assertRaises(AttributeError):
obj.a
obj = Fields(a=1)
self.assertEqual(obj.a, 1)

def test_fields_and_types(self):
class FieldsAndTypes(ast.AST):
_fields = ('a',)
_field_types = {'a': int | None}
Expand All @@ -3065,6 +3065,7 @@ class FieldsAndTypes(ast.AST):
obj = FieldsAndTypes(a=1)
self.assertEqual(obj.a, 1)

def test_fields_and_types_no_default(self):
class FieldsAndTypesNoDefault(ast.AST):
_fields = ('a',)
_field_types = {'a': int}
Expand All @@ -3077,6 +3078,38 @@ class FieldsAndTypesNoDefault(ast.AST):
obj = FieldsAndTypesNoDefault(a=1)
self.assertEqual(obj.a, 1)

def test_incomplete_field_types(self):
class MoreFieldsThanTypes(ast.AST):
_fields = ('a', 'b')
_field_types = {'a': int | None}
a: int | None = None
b: int | None = None

with self.assertWarnsRegex(
DeprecationWarning,
r"Field 'b' is missing from MoreFieldsThanTypes\._field_types"
):
obj = MoreFieldsThanTypes()
self.assertIs(obj.a, None)
self.assertIs(obj.b, None)

obj = MoreFieldsThanTypes(a=1, b=2)
self.assertEqual(obj.a, 1)
self.assertEqual(obj.b, 2)

def test_complete_field_types(self):
class _AllFieldTypes(ast.AST):
_fields = ('a', 'b')
_field_types = {'a': int | None, 'b': list[str]}
# This must be set explicitly
a: int | None = None
# This will add an implicit empty list default
b: list[str]

obj = _AllFieldTypes()
self.assertIs(obj.a, None)
self.assertEqual(obj.b, [])


@support.cpython_only
class ModuleStateTests(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improve the behavior of user-defined subclasses of :class:`ast.AST`. Such
classes will now require no changes in the usual case to conform with the
behavior changes of the :mod:`ast` module in Python 3.13. Patch by Jelle
Zijlstra.
31 changes: 17 additions & 14 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,14 +979,9 @@ def visitModule(self, mod):
goto cleanup;
}
if (field_types == NULL) {
if (PyErr_WarnFormat(
PyExc_DeprecationWarning, 1,
"%.400s provides _fields but not _field_types. "
"This will become an error in Python 3.15.",
Py_TYPE(self)->tp_name
) < 0) {
res = -1;
}
// Probably a user-defined subclass of AST that lacks _field_types.
// This will continue to work as it did before 3.13; i.e., attributes
// that are not passed in simply do not exist on the instance.
goto cleanup;
}
remaining_list = PySequence_List(remaining_fields);
Expand All @@ -997,12 +992,21 @@ def visitModule(self, mod):
PyObject *name = PyList_GET_ITEM(remaining_list, i);
PyObject *type = PyDict_GetItemWithError(field_types, name);
if (!type) {
if (!PyErr_Occurred()) {
PyErr_SetObject(PyExc_KeyError, name);
if (PyErr_Occurred()) {
goto set_remaining_cleanup;
}
else {
if (PyErr_WarnFormat(
PyExc_DeprecationWarning, 1,
"Field '%U' is missing from %.400s._field_types. "
"This will become an error in Python 3.15.",
name, Py_TYPE(self)->tp_name
) < 0) {
goto set_remaining_cleanup;
}
}
goto set_remaining_cleanup;
}
if (_PyUnion_Check(type)) {
else if (_PyUnion_Check(type)) {
// optional field
// do nothing, we'll have set a None default on the class
}
Expand All @@ -1026,8 +1030,7 @@ def visitModule(self, mod):
"This will become an error in Python 3.15.",
Py_TYPE(self)->tp_name, name
) < 0) {
res = -1;
goto cleanup;
goto set_remaining_cleanup;
}
}
}
Expand Down
31 changes: 17 additions & 14 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading