bpo-39999: Improve compatibility of the ast module. (GH-19056)
* Re-add removed classes Suite, slice, Param, AugLoad and AugStore.
* Add docstrings for dummy classes.
* Add docstrings for attribute aliases.
* Set __module__ to "ast" instead of "_ast".
diff --git a/Lib/ast.py b/Lib/ast.py
index e347d8b..f51c71f 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -489,6 +489,7 @@
# It will be removed in future.
def _getter(self):
+ """Deprecated. Use value instead."""
return self.value
def _setter(self, value):
@@ -499,6 +500,9 @@
class _ABC(type):
+ def __init__(cls, *args):
+ cls.__doc__ = """Deprecated AST node class. Use ast.Constant instead"""
+
def __instancecheck__(cls, inst):
if not isinstance(inst, Constant):
return False
@@ -564,15 +568,21 @@
type(...): 'Ellipsis',
}
-class Index(AST):
+class slice(AST):
+ """Deprecated AST node class."""
+
+class Index(slice):
+ """Deprecated AST node class. Use the index value directly instead."""
def __new__(cls, value, **kwargs):
return value
-class ExtSlice(AST):
+class ExtSlice(slice):
+ """Deprecated AST node class. Use ast.Tuple instead."""
def __new__(cls, dims=(), **kwargs):
return Tuple(list(dims), Load(), **kwargs)
def _dims_getter(self):
+ """Deprecated. Use elts instead."""
return self.elts
def _dims_setter(self, value):
@@ -580,6 +590,18 @@
Tuple.dims = property(_dims_getter, _dims_setter)
+class Suite(mod):
+ """Deprecated AST node class. Unused in Python 3."""
+
+class AugLoad(expr_context):
+ """Deprecated AST node class. Unused in Python 3."""
+
+class AugStore(expr_context):
+ """Deprecated AST node class. Unused in Python 3."""
+
+class Param(expr_context):
+ """Deprecated AST node class. Unused in Python 3."""
+
# Large float and imaginary literals get turned into infinities in the AST.
# We unparse those infinities to INFSTR.
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index d072c33..3fd982c 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -283,7 +283,7 @@
x.vararg
with self.assertRaises(TypeError):
- # "_ast.AST constructor takes 0 positional arguments"
+ # "ast.AST constructor takes 0 positional arguments"
ast.AST(2)
def test_AST_garbage_collection(self):
@@ -573,7 +573,7 @@
m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], [])
with self.assertRaises(TypeError) as cm:
compile(m, "<test>", "exec")
- self.assertIn("but got <_ast.expr", str(cm.exception))
+ self.assertIn("but got <ast.expr", str(cm.exception))
def test_invalid_identifier(self):
m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))], [])