diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-03-22 18:33:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-22 18:33:34 (GMT) |
commit | bace59d8b8e38f5c779ff6296ebdc0527f6db14a (patch) | |
tree | 81aa39edc1e15004da1d54507484609ab0745e22 /Lib | |
parent | 044cf94f610e831464a69a8e713dad89878824ce (diff) | |
download | cpython-bace59d8b8e38f5c779ff6296ebdc0527f6db14a.zip cpython-bace59d8b8e38f5c779ff6296ebdc0527f6db14a.tar.gz cpython-bace59d8b8e38f5c779ff6296ebdc0527f6db14a.tar.bz2 |
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".
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ast.py | 26 | ||||
-rw-r--r-- | Lib/test/test_ast.py | 4 |
2 files changed, 26 insertions, 4 deletions
@@ -489,6 +489,7 @@ class NodeTransformer(NodeVisitor): # It will be removed in future. def _getter(self): + """Deprecated. Use value instead.""" return self.value def _setter(self, value): @@ -499,6 +500,9 @@ Constant.s = property(_getter, _setter) 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 @@ _const_node_type_names = { 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 @@ def _dims_setter(self, value): 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 @@ class AST_Tests(unittest.TestCase): 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 @@ class AST_Tests(unittest.TestCase): 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()))], []) |