Skip to content
Open
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
23 changes: 22 additions & 1 deletion Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"""
from _ast import *


Copy link
Member

Choose a reason for hiding this comment

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

Please revert unrelated changes.

def parse(source, filename='<unknown>', mode='exec', *,
type_comments=False, feature_version=None, optimize=-1):
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"),
Copy link
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):
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()
28 changes: 28 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The classes ``ast.Suite``, ``ast.AugLoad``, ``ast.AugStore``, and
Copy link
Member

Choose a reason for hiding this comment

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

This only lists four of the seven things deprecated. It also doesn't mention the .dims attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Any migration guidance if anyone still uses these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can find discussion around the original deprecations, e.g., #84169 but I didn't find any specific migration advice.

An ast produced by ast.parse will never include instances of these classes.

The implementation of all the removed classes are pretty trivial so if you need them for some reason you can probably just copy them. e.g., AugLoad has no body beyond a docstring.

  class AugLoad(expr_context):
      """Deprecated AST node class.  Unused in Python 3."""

Copy link
Member

Choose a reason for hiding this comment

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

Yeah there's no good use for them. Most likely if people are using them, it's just some code that does something for all AST nodes, and they should simply delete the code handling these nodes. But that's guesswork.

``ast.Param``, deprecated since Python 3.9, now issue deprecation warnings
on use. They are now scheduled for removal in Python 3.20.
Loading