Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions Doc/deprecations/pending-removal-in-3.20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ Pending removal in Python 3.20

* Creating instances of abstract AST nodes (such as :class:`ast.AST`
or :class:`!ast.expr`) is deprecated and will raise an error in Python 3.20.
* Classes ``slice``, ``Index``, ``ExtSlice``, ``Suite``, ``Param``,
``AugLoad`` and ``AugStore``, will be removed in Python 3.20. These types
are not generated by the parser or accepted by the code generator.
* The ``dims`` property of ``ast.Tuple`` will be removed in Python 3.20. Use
the ``ast.Tuple.elts`` property instead.
11 changes: 11 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,17 @@ New deprecations

(Contributed by Nikita Sobolev in :gh:`136355`.)

* :mod:`ast`:

* Classes ``slice``, ``Index``, ``ExtSlice``, ``Suite``, ``Param``,
``AugLoad`` and ``AugStore``, deprecated since Python 3.9, are no longer
imported by ``from ast import *`` and issue a deprecation warning on
use. The classes are slated for removal in Python 3.20. These types are not
generated by the parser or accepted by the code generator.
* The ``dims`` property of ``ast.Tuple`` objects, deprecated since Python
3.9, now issues a deprecation warning on use. This property is slated for
removal in 3.20. Use ``ast.Tuple.elts`` instead.

* :mod:`collections.abc`

* The following statements now cause ``DeprecationWarning``\ s to be emitted
Expand Down
23 changes: 23 additions & 0 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,13 @@ def __new__(cls, dims=(), **kwargs):

def _dims_getter(self):
"""Deprecated. Use elts instead."""
import warnings
warnings._deprecated(f"ast.Tuple.dims", remove=(3, 20))
return self.elts

def _dims_setter(self, value):
import warnings
warnings._deprecated(f"ast.Tuple.dims", remove=(3, 20))
self.elts = value

Tuple.dims = property(_dims_getter, _dims_setter)
Expand Down Expand Up @@ -712,5 +716,24 @@ def main(args=None):
color=can_colorize(file=sys.stdout),
indent=args.indent, show_empty=args.show_empty))

_deprecated = {
'slice': globals().pop("slice"),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this means from ast import * will no longer pick these up. That's probably OK for 3.15 but let's mention it in docs.

'Index': globals().pop("Index"),
'ExtSlice': globals().pop("ExtSlice"),
'Suite': globals().pop("Suite"),
'AugLoad': globals().pop("AugLoad"),
'AugStore': globals().pop("AugStore"),
'Param': globals().pop("Param")
}

def __getattr__(attr):
try:
val = _deprecated[attr]
except KeyError:
raise AttributeError(f"module 'ast' has no attribute {attr!r}") from None
import warnings
warnings._deprecated(f"ast.{attr}", remove=(3, 20))
return val

if __name__ == '__main__':
main()
43 changes: 41 additions & 2 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ def test_classattrs(self):
self.assertIs(ast.Constant(None).value, None)
self.assertIs(ast.Constant(...).value, ...)

with self.assertWarns(DeprecationWarning):
ast.Tuple().dims

with self.assertWarns(DeprecationWarning):
ast.Tuple().dims = 3

def test_constant_subclasses(self):
class N(ast.Constant):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -1100,6 +1106,38 @@ def test_tstring(self):
self.assertIsInstance(tree.body[0].value.values[0], ast.Constant)
self.assertIsInstance(tree.body[0].value.values[1], ast.Interpolation)

def test_deprecated(self):
with self.assertWarns(DeprecationWarning):
ast.slice

with self.assertWarns(DeprecationWarning):
ast.Index

with self.assertWarns(DeprecationWarning):
ast.ExtSlice

with self.assertWarns(DeprecationWarning):
ast.Suite

with self.assertWarns(DeprecationWarning):
ast.AugLoad

with self.assertWarns(DeprecationWarning):
ast.AugStore

with self.assertWarns(DeprecationWarning):
ast.Param

namespace = {}
exec("from ast import *", namespace)
self.assertNotIn("slice", namespace)
self.assertNotIn("Index", namespace)
self.assertNotIn("ExtSlice", namespace)
self.assertNotIn("Suite", namespace)
self.assertNotIn("AugLoad", namespace)
self.assertNotIn("AugStore", namespace)
self.assertNotIn("Param", namespace)

def test_filter_syntax_warnings_by_module(self):
filename = support.findfile('test_import/data/syntax_warnings.py')
with open(filename, 'rb') as f:
Expand Down Expand Up @@ -1139,8 +1177,9 @@ def iter_ast_classes():
def do(cls):
if cls.__module__ != 'ast':
return
if cls is ast.Index:
return
with warnings.catch_warnings(action="ignore", category=DeprecationWarning):
if cls is ast.Index:
return
# Don't attempt to create instances of abstract AST nodes
if _ast._is_abstract(cls):
return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
The classes ``ast.slice``, ``ast.ExtSlice``, ``ast.Index``, ``ast.Suite``, ``ast.AugLoad``,
``ast.AugStore``, and ``ast.Param``, deprecated since Python 3.9, now issue
deprecation warnings on use. They are now scheduled for removal in Python 3.20.

The ``dims`` property of ``ast.Tuple`` objects, deprecated since Python 3.9,
now issues a deprecation warning when accessed. The property is scheduled for
removal in Python 3.20. Use the (non-deprecated) ``elts`` property of
``ast.Tuple`` objects instead.

The deprecated global names are also no longer imported by
``from ast import *``.
Loading