10000 gh-105858: Improve AST node constructors by JelleZijlstra · Pull Request #105880 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-105858: Improve AST node constructors #105880

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 18 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix most deprecationwarnings in test_ast
  • Loading branch information
JelleZijlstra committed Jun 22, 2023
commit 05d5569ee2ec0e07b7f8a0975bf0de47a0d215e2
63 changes: 41 additions & 22 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,23 @@ def test_field_attr_existence(self):
if name == 'Index':
continue
if self._is_ast_node(name, item):
x = item()
x = self._construct_ast_class(item)
if isinstance(x, ast.AST):
self.assertIs(type(x._fields), tuple)

def _construct_ast_class(self, cls):
kwargs = {}
for name, typ in cls.__annotations__.items():
if typ is str:
kwargs[name] = 'capybara'
elif typ is int:
kwargs[name] = 42
elif typ is object:
kwargs[name] = b'capybara'
elif isinstance(typ, type) and issubclass(typ, ast.AST):
kwargs[name] = self._construct_ast_class(typ)
return cls(**kwargs)

def test_arguments(self):
x = ast.arguments()
self.assertEqual(x._fields, ('posonlyargs', 'args', 'vararg', 'kwonlyargs',
Expand Down Expand Up @@ -521,7 +534,7 @@ def test_field_attr_writable_deprecated(self):
self.assertEqual(x._fields, 666)

def test_field_attr_writable(self):
x = ast.Constant()
x = ast.Constant(1)
# We can assign to _fields
x._fields = 666
self.assertEqual(x._fields, 666)
Expand Down Expand Up @@ -613,7 +626,8 @@ def test_classattrs_deprecated(self):
])

def test_classattrs(self):
x = ast.Constant()
with self.assertWarns(DeprecationWarning):
x = ast.Constant()
self.assertEqual(x._fields, ('value', 'kind'))

with self.assertRaises(AttributeError):
Expand All @@ -628,7 +642,7 @@ def test_classattrs(self):
with self.assertRaises(AttributeError):
x.foobar

x = ast.Constant(lineno=2)
x = ast.Constant(lineno=2, value=3)
self.assertEqual(x.lineno, 2)

x = ast.Constant(42, lineno=0)
Expand All @@ -639,8 +653,9 @@ def test_classattrs(self):
self.assertRaises(TypeError, ast.Constant, 1, None, 2)
self.assertRaises(TypeError, ast.Constant, 1, None, 2, lineno=0)

# Arbitrary keyword arguments are supported
self.assertEqual(ast.Constant(1, foo='bar').foo, 'bar')
# Arbitrary keyword arguments are supported (but deprecated)
with self.assertWarns(DeprecationWarning):
self.assertEqual(ast.Constant(1, foo='bar').foo, 'bar')

with self.assertRaisesRegex(TypeError, "Constant got multiple values for argument 'value'"):
ast.Constant(1, value=2)
Expand Down Expand Up @@ -792,11 +807,11 @@ def test_isinstance(self):
assertBytesDeprecated(self.assertNotIsInstance, Constant('42'), Bytes)
assertNameConstantDeprecated(self.assertNotIsInstance, Constant(42), NameConstant)
assertEllipsisDeprecated(self.assertNotIsInstance, Constant(42), Ellipsis)
assertNumDeprecated(self.assertNotIsInstance, Constant(), Num)
assertStrDeprecated(self.assertNotIsInstance, Constant(), Str)
assertBytesDeprecated(self.assertNotIsInstance, Constant(), Bytes)
assertNameConstantDeprecated(self.assertNotIsInstance, Constant(), NameConstant)
assertEllipsisDeprecated(self.assertNotIsInstance, Constant(), Ellipsis)
assertNumDeprecated(self.assertNotIsInstance, Constant(None), Num)
assertStrDeprecated(self.assertNotIsInstance, Constant(None), Str)
assertBytesDeprecated(self.assertNotIsInstance, Constant(None), Bytes)
assertNameConstantDeprecated(self.assertNotIsInstance, Constant(1), NameConstant)
assertEllipsisDeprecated(self.assertNotIsInstance, Constant(None), Ellipsis)

class S(str): pass
with assertStrDeprecated():
Expand Down Expand Up @@ -865,8 +880,9 @@ def test_module(self):
self.assertEqual(x.body, body)

def test_nodeclasses(self):
# Zero arguments constructor explicitly allowed
x = ast.BinOp()
# Zero arguments constructor explicitly allowed (but deprecated)
with self.assertWarns(DeprecationWarning):
x = ast.BinOp()
self.assertEqual(x._fields, ('left', 'op', 'right'))

# Random attribute allowed too
Expand Down Expand Up @@ -904,8 +920,9 @@ def test_nodeclasses(self):
self.assertEqual(x.right, 3)
self.assertEqual(x.lineno, 0)

# Random kwargs also allowed
x = ast.BinOp(1, 2, 3, foobarbaz=42)
# Random kwargs also allowed (but deprecated)
with self.assertWarns(DeprecationWarning):
x = ast.BinOp(1, 2, 3, foobarbaz=42)
self.assertEqual(x.foobarbaz, 42)

def test_no_fields(self):
Expand All @@ -918,8 +935,9 @@ def test_pickling(self):

for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
ast2 = pickle.loads(pickle.dumps(ast, protocol))
self.assertEqual(to_tuple(ast2), to_tuple(ast))
with self.subTest(ast=ast, protocol=protocol):
ast2 = pickle.loads(pickle.dumps(ast, protocol))
self.assertEqual(to_tuple(ast2), to_tuple(ast))

def test_invalid_sum(self):
pos = dict(lineno=2, col_offset=3)
Expand Down Expand Up @@ -1288,8 +1306,9 @@ def test_copy_location(self):
'lineno=1, col_offset=4, end_lineno=1, end_col_offset=5), lineno=1, '
'col_offset=0, end_lineno=1, end_col_offset=5))'
)
src = ast.Call(col_offset=1, lineno=1, end_lineno=1, end_col_offset=1)
new = ast.copy_location(src, ast.Call(col_offset=None, lineno=None))
func = ast.Name('spam', ast.Load())
src = ast.Call(col_offset=1, lineno=1, end_lineno=1, end_col_offset=1, func=func)
new = ast.copy_location(src, ast.Call(col_offset=None, lineno=None, func=func))
self.assertIsNone(new.end_lineno)
self.assertIsNone(new.end_col_offset)
self.assertEqual(new.lineno, 1)
Expand Down Expand Up @@ -1548,15 +1567,15 @@ def test_level_as_none(self):
self.assertIn('sleep', ns)

def test_recursion_direct(self):
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0, operand=ast.Constant(1))
e.operand = e
with self.assertRaises(RecursionError):
with support.infinite_recursion():
compile(ast.Expression(e), "<test>", "eval")

def test_recursion_indirect(self):
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
f = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0, operand=ast.Constant(1))
f = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0, operand=ast.Constant(1))
e.operand = f
f.operand = e
with self.assertRaises(RecursionError):
Expand Down
2 changes: 2 additions & 0 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ def visitModule(self, mod):
"and will be removed in Python 3.15.",
Py_TYPE(self)->tp_name, key
) < 0) {
res = -1;
goto cleanup;
}
}
Expand Down Expand Up @@ -1013,6 +1014,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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions Python/Python-ast.c

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

0