bpo-39999: Improve compatibility of the ast module. (GH-19056) · python/cpython@bace59d · GitHub
Skip to content

Commit bace59d

Browse files
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".
1 parent 044cf94 commit bace59d

10 files changed

Lines changed: 49 additions & 22 deletions

Doc/whatsnew/3.9.rst

Lines changed: 8 additions & 6 deletions

Lib/ast.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ def generic_visit(self, node):
489489
# It will be removed in future.
490490

491491
def _getter(self):
492+
"""Deprecated. Use value instead."""
492493
return self.value
493494

494495
def _setter(self, value):
@@ -499,6 +500,9 @@ def _setter(self, value):
499500

500501
class _ABC(type):
501502

503+
def __init__(cls, *args):
504+
cls.__doc__ = """Deprecated AST node class. Use ast.Constant instead"""
505+
502506
def __instancecheck__(cls, inst):
503507
if not isinstance(inst, Constant):
504508
return False
@@ -564,22 +568,40 @@ def __new__(cls, *args, **kwargs):
564568
type(...): 'Ellipsis',
565569
}
566570

567-
class Index(AST):
571+
class slice(AST):
572+
"""Deprecated AST node class."""
573+
574+
class Index(slice):
575+
"""Deprecated AST node class. Use the index value directly instead."""
568576
def __new__(cls, value, **kwargs):
569577
return value
570578

571-
class ExtSlice(AST):
579+
class ExtSlice(slice):
580+
"""Deprecated AST node class. Use ast.Tuple instead."""
572581
def __new__(cls, dims=(), **kwargs):
573582
return Tuple(list(dims), Load(), **kwargs)
574583

575584
def _dims_getter(self):
585+
"""Deprecated. Use elts instead."""
576586
return self.elts
577587

578588
def _dims_setter(self, value):
579589
self.elts = value
580590

581591
Tuple.dims = property(_dims_getter, _dims_setter)
582592

593+
class Suite(mod):
594+
"""Deprecated AST node class. Unused in Python 3."""
595+
596+
class AugLoad(expr_context):
597+
"""Deprecated AST node class. Unused in Python 3."""
598+
599+
class AugStore(expr_context):
600+
"""Deprecated AST node class. Unused in Python 3."""
601+
602+
class Param(expr_context):
603+
"""Deprecated AST node class. Unused in Python 3."""
604+
583605

584606
# Large float and imaginary literals get turned into infinities in the AST.
585607
# We unparse those infinities to INFSTR.

Lib/test/test_ast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def test_AST_objects(self):
283283
x.vararg
284284

285285
with self.assertRaises(TypeError):
286-
# "_ast.AST constructor takes 0 positional arguments"
286+
# "ast.AST constructor takes 0 positional arguments"
287287
ast.AST(2)
288288

289289
def test_AST_garbage_collection(self):
@@ -573,7 +573,7 @@ def test_invalid_sum(self):
573573
m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], [])
574574
with self.assertRaises(TypeError) as cm:
575575
compile(m, "<test>", "exec")
576-
self.assertIn("but got <_ast.expr", str(cm.exception))
576+
self.assertIn("but got <ast.expr", str(cm.exception))
577577

578578
def test_invalid_identifier(self):
579579
m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))], [])

Misc/NEWS.d/next/Core and Builtins/2020-02-15-15-29-34.bpo-39639.3mqJjm.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecated ``ast.Suite`` node class because it's no longer used. Patch by Batuhan Taskaya.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Remove ``ast.Param`` node class because it's no longer used. Patch by
1+
Deprecated ``ast.Param`` node class because it's no longer used. Patch by
22
Batuhan Taskaya.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Removed ``ast.AugLoad`` and ``ast.AugStore`` node classes because they are
1+
Deprecated ``ast.AugLoad`` and ``ast.AugStore`` node classes because they are
22
no longer used.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``__module__`` of the AST node classes is now set to "ast" instead of
2+
"_ast". Added docstrings for dummy AST node classes and deprecated
3+
attributes.

Parser/asdl_c.py

Lines changed: 3 additions & 3 deletions

Python/Python-ast.c

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)