Skip to content

Commit 3249a2e

Browse files
committed
gh-140344: ast: Add deprecation warnings
These were all deprecated in 3.9 (bace59d) but without a runtime deprecation warning. Add it now, so that these items can be removed in 3.20 per PEP 387.
1 parent ed672f7 commit 3249a2e

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

Lib/ast.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"""
2323
from _ast import *
2424

25-
2625
def parse(source, filename='<unknown>', mode='exec', *,
2726
type_comments=False, feature_version=None, optimize=-1):
2827
"""
@@ -607,9 +606,13 @@ def __new__(cls, dims=(), **kwargs):
607606

608607
def _dims_getter(self):
609608
"""Deprecated. Use elts instead."""
609+
import warnings
610+
warnings._deprecated(f"ast.Tuple.dims", remove=(3, 20))
610611
return self.elts
611612

612613
def _dims_setter(self, value):
614+
import warnings
615+
warnings._deprecated(f"ast.Tuple.dims", remove=(3, 20))
613616
self.elts = value
614617

615618
Tuple.dims = property(_dims_getter, _dims_setter)
@@ -689,5 +692,23 @@ def main(args=None):
689692
print(dump(tree, include_attributes=args.include_attributes,
690693
indent=args.indent, show_empty=args.show_empty))
691694

695+
_deprecated = {
696+
'slice': globals().pop("slice"),
697+
'Index': globals().pop("Index"),
698+
'ExtSlice': globals().pop("ExtSlice"),
699+
'Suite': globals().pop("Suite"),
700+
'AugLoad': globals().pop("AugLoad"),
701+
'AugStore': globals().pop("AugStore"),
702+
'Param': globals().pop("Param")
703+
}
704+
705+
def __getattr__(attr):
706+
if val := _deprecated.get(attr, None):
707+
import warnings
708+
warnings._deprecated(f"ast.{attr}", remove=(3, 20))
709+
globals()[attr] = val
710+
return val
711+
raise AttributeError(f"module 'ast' has no attribute {attr!r}")
712+
692713
if __name__ == '__main__':
693714
main()

Lib/test/test_ast/test_ast.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ def test_classattrs(self):
502502
self.assertIs(ast.Constant(None).value, None)
503503
self.assertIs(ast.Constant(...).value, ...)
504504

505+
with self.assertWarns(DeprecationWarning):
506+
ast.Tuple().dims
507+
508+
with self.assertWarns(DeprecationWarning):
509+
ast.Tuple().dims = 3
510+
505511
def test_constant_subclasses(self):
506512
class N(ast.Constant):
507513
def __init__(self, *args, **kwargs):
@@ -1124,6 +1130,28 @@ def test_tstring(self):
11241130
self.assertIsInstance(tree.body[0].value.values[0], ast.Constant)
11251131
self.assertIsInstance(tree.body[0].value.values[1], ast.Interpolation)
11261132

1133+
def test_deprecated(self):
1134+
with self.assertWarns(DeprecationWarning):
1135+
ast.slice
1136+
1137+
with self.assertWarns(DeprecationWarning):
1138+
ast.Index
1139+
1140+
with self.assertWarns(DeprecationWarning):
1141+
ast.ExtSlice
1142+
1143+
with self.assertWarns(DeprecationWarning):
1144+
ast.Suite
1145+
1146+
with self.assertWarns(DeprecationWarning):
1147+
ast.AugLoad
1148+
1149+
with self.assertWarns(DeprecationWarning):
1150+
ast.AugStore
1151+
1152+
with self.assertWarns(DeprecationWarning):
1153+
ast.Param
1154+
11271155

11281156
class CopyTests(unittest.TestCase):
11291157
"""Test copying and pickling AST nodes."""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The classes ``ast.Suite``, ``ast.AugLoad``, ``ast.AugStore``, and
2+
``ast.Param``, deprecated since Python 3.9, now issue deprecation warnings
3+
on use. They are now scheduled for removal in Python 3.20.

0 commit comments

Comments
 (0)