diff --git a/Doc/deprecations/pending-removal-in-3.20.rst b/Doc/deprecations/pending-removal-in-3.20.rst index c86979c8ff91e9..a828d95a9c92d9 100644 --- a/Doc/deprecations/pending-removal-in-3.20.rst +++ b/Doc/deprecations/pending-removal-in-3.20.rst @@ -22,3 +22,14 @@ Pending removal in Python 3.20 - :mod:`zlib` (Contributed by Hugo van Kemenade and Stan Ulbrych in :gh:`76007`.) + +* The ``__version__`` attribute has been deprecated in these standard library + modules and will be removed in Python 3.20. + +* :mod:`ast`: + + * Classes ``slice``, ``Index`` and ``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. diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index d3ae7c21a0358b..14077d39158890 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -802,6 +802,16 @@ New deprecations (Contributed by Nikita Sobolev in :gh:`136355`.) +* :mod:`ast`: + + * Classes ``slice``, ``Index`` and ``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 first + use. The classes are slated for removal in Python 3.20. + * The ``dims`` property of ``ast.Tuple`` objects, deprecated since Python + 3.9, now issues a deprecation warning on use. Thie property is slated for + removal in 3.20. Use ``ast.Tuple.elts`` instead. + * :mod:`hashlib`: * In hash function constructors such as :func:`~hashlib.new` or the diff --git a/Lib/ast.py b/Lib/ast.py index 983ac1710d0205..13459f848f6f48 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -22,7 +22,6 @@ """ from _ast import * - def parse(source, filename='', mode='exec', *, type_comments=False, feature_version=None, optimize=-1): """ @@ -607,9 +606,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) @@ -689,5 +692,23 @@ def main(args=None): print(dump(tree, include_attributes=args.include_attributes, indent=args.indent, show_empty=args.show_empty)) +_deprecated = { + 'slice': globals().pop("slice"), + '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): + if val := _deprecated.get(attr, None): + import warnings + warnings._deprecated(f"ast.{attr}", remove=(3, 20)) + globals()[attr] = val + return val + raise AttributeError(f"module 'ast' has no attribute {attr!r}") + if __name__ == '__main__': main() diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py index 1e6f60074308e2..4258ce58d1091a 100644 --- a/Lib/test/test_ast/test_ast.py +++ b/Lib/test/test_ast/test_ast.py @@ -502,6 +502,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): @@ -1124,6 +1130,28 @@ 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 + class CopyTests(unittest.TestCase): """Test copying and pickling AST nodes.""" diff --git a/Misc/NEWS.d/next/Library/2025-10-20-09-12-18.gh-issue-140344.WjbYg-.rst b/Misc/NEWS.d/next/Library/2025-10-20-09-12-18.gh-issue-140344.WjbYg-.rst new file mode 100644 index 00000000000000..6f56d03b658d7a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-20-09-12-18.gh-issue-140344.WjbYg-.rst @@ -0,0 +1,11 @@ +The classes ``ast.slice``, ``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 *``.