summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_ast.py
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-03-28 10:30:31 (GMT)
committerGitHub <noreply@github.com>2024-03-28 10:30:31 (GMT)
commit4c71d51a4b7989fc8754ba512c40e21666f9db0d (patch)
tree698a1996cb489c6426a6353c033983435640c143 /Lib/test/test_ast.py
parent8cb7d7ff86a1a2d41195f01ba4f218941dd7308c (diff)
downloadcpython-4c71d51a4b7989fc8754ba512c40e21666f9db0d.zip
cpython-4c71d51a4b7989fc8754ba512c40e21666f9db0d.tar.gz
cpython-4c71d51a4b7989fc8754ba512c40e21666f9db0d.tar.bz2
gh-117266: Fix crashes on user-created AST subclasses (GH-117276)
Fix crashes on user-created AST subclasses
Diffstat (limited to 'Lib/test/test_ast.py')
-rw-r--r--Lib/test/test_ast.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 7cecf31..3929e4e 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -2916,6 +2916,47 @@ class ASTConstructorTests(unittest.TestCase):
self.assertEqual(node.name, 'foo')
self.assertEqual(node.decorator_list, [])
+ def test_custom_subclass(self):
+ class NoInit(ast.AST):
+ pass
+
+ obj = NoInit()
+ self.assertIsInstance(obj, NoInit)
+ self.assertEqual(obj.__dict__, {})
+
+ class Fields(ast.AST):
+ _fields = ('a',)
+
+ with self.assertWarnsRegex(DeprecationWarning,
+ r"Fields provides _fields but not _field_types."):
+ obj = Fields()
+ with self.assertRaises(AttributeError):
+ obj.a
+ obj = Fields(a=1)
+ self.assertEqual(obj.a, 1)
+
+ class FieldsAndTypes(ast.AST):
+ _fields = ('a',)
+ _field_types = {'a': int | None}
+ a: int | None = None
+
+ obj = FieldsAndTypes()
+ self.assertIs(obj.a, None)
+ obj = FieldsAndTypes(a=1)
+ self.assertEqual(obj.a, 1)
+
+ class FieldsAndTypesNoDefault(ast.AST):
+ _fields = ('a',)
+ _field_types = {'a': int}
+
+ with self.assertWarnsRegex(DeprecationWarning,
+ r"FieldsAndTypesNoDefault\.__init__ missing 1 required positional argument: 'a'\."):
+ obj = FieldsAndTypesNoDefault()
+ with self.assertRaises(AttributeError):
+ obj.a
+ obj = FieldsAndTypesNoDefault(a=1)
+ self.assertEqual(obj.a, 1)
+
@support.cpython_only
class ModuleStateTests(unittest.TestCase):