[3.13] gh-121332: Make AST node constructor check _attributes instead of hardcoding attributes (GH-121334) (#121625)

(cherry picked from commit 58e8cf2bb6)
This commit is contained in:
Jelle Zijlstra 2024-07-11 08:51:32 -07:00 committed by GitHub
parent 3b5f8d256c
commit 38c4028dd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 66 additions and 28 deletions

View file

@ -3148,6 +3148,18 @@ class ASTConstructorTests(unittest.TestCase):
obj = FieldsAndTypes(a=1)
self.assertEqual(obj.a, 1)
def test_custom_attributes(self):
class MyAttrs(ast.AST):
_attributes = ("a", "b")
obj = MyAttrs(a=1, b=2)
self.assertEqual(obj.a, 1)
self.assertEqual(obj.b, 2)
with self.assertWarnsRegex(DeprecationWarning,
r"MyAttrs.__init__ got an unexpected keyword argument 'c'."):
obj = MyAttrs(c=3)
def test_fields_and_types_no_default(self):
class FieldsAndTypesNoDefault(ast.AST):
_fields = ('a',)