Skip to content

Commit 806c997

Browse files
gh-140344: ast: Add deprecation warnings (#140345)
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.21 per PEP 387. Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent dbd8985 commit 806c997

6 files changed

Lines changed: 95 additions & 6 deletions

File tree

Doc/deprecations/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Deprecations
1313

1414
.. include:: pending-removal-in-3.20.rst
1515

16+
.. include:: pending-removal-in-3.21.rst
17+
1618
.. include:: pending-removal-in-future.rst
1719

1820
.. include:: soft-deprecations.rst
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Pending removal in Python 3.21
2+
------------------------------
3+
4+
* :mod:`ast`:
5+
6+
* Classes ``slice``, ``Index``, ``ExtSlice``, ``Suite``, ``Param``,
7+
``AugLoad`` and ``AugStore``, will be removed in Python 3.21. These types
8+
are not generated by the parser or accepted by the code generator.
9+
* The ``dims`` property of ``ast.Tuple`` will be removed in Python 3.21. Use
10+
the ``ast.Tuple.elts`` property instead.

Doc/whatsnew/3.16.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,16 @@ tarfile
184184
Deprecated
185185
==========
186186

187-
* module_name:
188-
TODO
189-
187+
* :mod:`ast`:
188+
189+
* Classes ``slice``, ``Index``, ``ExtSlice``, ``Suite``, ``Param``,
190+
``AugLoad`` and ``AugStore``, deprecated since Python 3.9, are no longer
191+
imported by ``from ast import *`` and issue a deprecation warning on
192+
use. The classes are slated for removal in Python 3.21. These types are not
193+
generated by the parser or accepted by the code generator.
194+
* The ``dims`` property of ``ast.Tuple`` objects, deprecated since Python
195+
3.9, now issues a deprecation warning on use. This property is slated for
196+
removal in 3.21. Use ``ast.Tuple.elts`` instead.
190197

191198
.. Add deprecations above alphabetically, not here at the end.
192199

Lib/ast.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
:license: Python License.
2222
"""
2323
from _ast import *
24-
24+
lazy import warnings
2525

2626
def parse(source, filename='<unknown>', mode='exec', *,
2727
type_comments=False, feature_version=None, optimize=-1, module=None):
@@ -630,9 +630,11 @@ def __new__(cls, dims=(), **kwargs):
630630

631631
def _dims_getter(self):
632632
"""Deprecated. Use elts instead."""
633+
warnings._deprecated(f"ast.Tuple.dims", remove=(3, 21))
633634
return self.elts
634635

635636
def _dims_setter(self, value):
637+
warnings._deprecated(f"ast.Tuple.dims", remove=(3, 21))
636638
self.elts = value
637639

638640
Tuple.dims = property(_dims_getter, _dims_setter)
@@ -714,5 +716,23 @@ def main(args=None):
714716
color=can_colorize(file=sys.stdout),
715717
indent=args.indent, show_empty=args.show_empty))
716718

719+
_deprecated = {
720+
'slice': globals().pop("slice"),
721+
'Index': globals().pop("Index"),
722+
'ExtSlice': globals().pop("ExtSlice"),
723+
'Suite': globals().pop("Suite"),
724+
'AugLoad': globals().pop("AugLoad"),
725+
'AugStore': globals().pop("AugStore"),
726+
'Param': globals().pop("Param")
727+
}
728+
729+
def __getattr__(attr):
730+
try:
731+
val = _deprecated[attr]
732+
except KeyError:
733+
raise AttributeError(f"module 'ast' has no attribute {attr!r}") from None
734+
warnings._deprecated(f"ast.{attr}", remove=(3, 21))
735+
return val
736+
717737
if __name__ == '__main__':
718738
main()

Lib/test/test_ast/test_ast.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,12 @@ def test_classattrs(self):
523523
self.assertIs(ast.Constant(None).value, None)
524524
self.assertIs(ast.Constant(...).value, ...)
525525

526+
with self.assertWarns(DeprecationWarning):
527+
ast.Tuple().dims
528+
529+
with self.assertWarns(DeprecationWarning):
530+
ast.Tuple().dims = 3
531+
526532
def test_constant_subclasses(self):
527533
class N(ast.Constant):
528534
def __init__(self, *args, **kwargs):
@@ -1100,6 +1106,38 @@ def test_tstring(self):
11001106
self.assertIsInstance(tree.body[0].value.values[0], ast.Constant)
11011107
self.assertIsInstance(tree.body[0].value.values[1], ast.Interpolation)
11021108

1109+
def test_deprecated(self):
1110+
with self.assertWarns(DeprecationWarning):
1111+
ast.slice
1112+
1113+
with self.assertWarns(DeprecationWarning):
1114+
ast.Index
1115+
1116+
with self.assertWarns(DeprecationWarning):
1117+
ast.ExtSlice
1118+
1119+
with self.assertWarns(DeprecationWarning):
1120+
ast.Suite
1121+
1122+
with self.assertWarns(DeprecationWarning):
1123+
ast.AugLoad
1124+
1125+
with self.assertWarns(DeprecationWarning):
1126+
ast.AugStore
1127+
1128+
with self.assertWarns(DeprecationWarning):
1129+
ast.Param
1130+
1131+
namespace = {}
1132+
exec("from ast import *", namespace)
1133+
self.assertNotIn("slice", namespace)
1134+
self.assertNotIn("Index", namespace)
1135+
self.assertNotIn("ExtSlice", namespace)
1136+
self.assertNotIn("Suite", namespace)
1137+
self.assertNotIn("AugLoad", namespace)
1138+
self.assertNotIn("AugStore", namespace)
1139+
self.assertNotIn("Param", namespace)
1140+
11031141
def test_filter_syntax_warnings_by_module(self):
11041142
filename = support.findfile('test_import/data/syntax_warnings.py')
11051143
with open(filename, 'rb') as f:
@@ -1139,8 +1177,9 @@ def iter_ast_classes():
11391177
def do(cls):
11401178
if cls.__module__ != 'ast':
11411179
return
1142-
if cls is ast.Index:
1143-
return
1180+
with warnings.catch_warnings(action="ignore", category=DeprecationWarning):
1181+
if cls is ast.Index:
1182+
return
11441183
# Don't attempt to create instances of abstract AST nodes
11451184
if _ast._is_abstract(cls):
11461185
return
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The classes ``ast.slice``, ``ast.ExtSlice``, ``ast.Index``, ``ast.Suite``, ``ast.AugLoad``,
2+
``ast.AugStore``, and ``ast.Param``, deprecated since Python 3.9, now issue
3+
deprecation warnings on use. They are now scheduled for removal in Python 3.21.
4+
5+
The ``dims`` property of ``ast.Tuple`` objects, deprecated since Python 3.9,
6+
now issues a deprecation warning when accessed. The property is scheduled for
7+
removal in Python 3.21. Use the (non-deprecated) ``elts`` property of
8+
``ast.Tuple`` objects instead.
9+
10+
The deprecated global names are also no longer imported by
11+
``from ast import *``.

0 commit comments

Comments
 (0)