summaryrefslogtreecommitdiffstats
path: root/Doc/library/ast.rst
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2020-03-07 18:22:58 (GMT)
committerGitHub <noreply@github.com>2020-03-07 18:22:58 (GMT)
commit02f64cb79175902705b40e3eaa8ea6c7038754ef (patch)
tree7791b435073332d26fea3a5d023f81a1504088da /Doc/library/ast.rst
parenteb7560a73d46800e4ade4a8869139b48e6c92811 (diff)
downloadcpython-02f64cb79175902705b40e3eaa8ea6c7038754ef.zip
cpython-02f64cb79175902705b40e3eaa8ea6c7038754ef.tar.gz
cpython-02f64cb79175902705b40e3eaa8ea6c7038754ef.tar.bz2
bpo-39199: Use 'eval' mode for the examples with expression nodes (GH-18828)
Co-Authored-By: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Doc/library/ast.rst')
-rw-r--r--Doc/library/ast.rst503
1 files changed, 211 insertions, 292 deletions
diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst
index ea30578..0173564 100644
--- a/Doc/library/ast.rst
+++ b/Doc/library/ast.rst
@@ -139,12 +139,9 @@ Literals
.. doctest::
- >>> print(ast.dump(ast.parse("123"), indent=4))
- Module(
- body=[
- Expr(
- value=Constant(value=123, kind=None))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('123', mode='eval'), indent=4))
+ Expression(
+ body=Constant(value=123, kind=None))
.. class:: FormattedValue(value, conversion, format_spec)
@@ -174,29 +171,26 @@ Literals
.. doctest::
- >>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"'), indent=4))
- Module(
- body=[
- Expr(
- value=JoinedStr(
- values=[
- Constant(value='sin(', kind=None),
- FormattedValue(
- value=Name(id='a', ctx=Load()),
- conversion=-1,
- format_spec=None),
- Constant(value=') is ', kind=None),
- FormattedValue(
- value=Call(
- func=Name(id='sin', ctx=Load()),
- args=[
- Name(id='a', ctx=Load())],
- keywords=[]),
- conversion=-1,
- format_spec=JoinedStr(
- values=[
- Constant(value='.3', kind=None)]))]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"', mode='eval'), indent=4))
+ Expression(
+ body=JoinedStr(
+ values=[
+ Constant(value='sin(', kind=None),
+ FormattedValue(
+ value=Name(id='a', ctx=Load()),
+ conversion=-1,
+ format_spec=None),
+ Constant(value=') is ', kind=None),
+ FormattedValue(
+ value=Call(
+ func=Name(id='sin', ctx=Load()),
+ args=[
+ Name(id='a', ctx=Load())],
+ keywords=[]),
+ conversion=-1,
+ format_spec=JoinedStr(
+ values=[
+ Constant(value='.3', kind=None)]))]))
.. class:: List(elts, ctx)
@@ -208,29 +202,22 @@ Literals
.. doctest::
- >>> print(ast.dump(ast.parse("[1, 2, 3]"), indent=4))
- Module(
- body=[
- Expr(
- value=List(
- elts=[
- Constant(value=1, kind=None),
- Constant(value=2, kind=None),
- Constant(value=3, kind=None)],
- ctx=Load()))],
- type_ignores=[])
-
- >>> print(ast.dump(ast.parse("(1, 2, 3)"), indent=4))
- Module(
- body=[
- Expr(
- value=Tuple(
- elts=[
- Constant(value=1, kind=None),
- Constant(value=2, kind=None),
- Constant(value=3, kind=None)],
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('[1, 2, 3]', mode='eval'), indent=4))
+ Expression(
+ body=List(
+ elts=[
+ Constant(value=1, kind=None),
+ Constant(value=2, kind=None),
+ Constant(value=3, kind=None)],
+ ctx=Load()))
+ >>> print(ast.dump(ast.parse('(1, 2, 3)', mode='eval'), indent=4))
+ Expression(
+ body=Tuple(
+ elts=[
+ Constant(value=1, kind=None),
+ Constant(value=2, kind=None),
+ Constant(value=3, kind=None)],
+ ctx=Load()))
.. class:: Set(elts)
@@ -239,16 +226,13 @@ Literals
.. doctest::
- >>> print(ast.dump(ast.parse("{1, 2, 3}"), indent=4))
- Module(
- body=[
- Expr(
- value=Set(
- elts=[
- Constant(value=1, kind=None),
- Constant(value=2, kind=None),
- Constant(value=3, kind=None)]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('{1, 2, 3}', mode='eval'), indent=4))
+ Expression(
+ body=Set(
+ elts=[
+ Constant(value=1, kind=None),
+ Constant(value=2, kind=None),
+ Constant(value=3, kind=None)]))
.. class:: Dict(keys, values)
@@ -263,18 +247,15 @@ Literals
.. doctest::
- >>> print(ast.dump(ast.parse("{'a':1, **d}"), indent=4))
- Module(
- body=[
- Expr(
- value=Dict(
- keys=[
- Constant(value='a', kind=None),
- None],
- values=[
- Constant(value=1, kind=None),
- Name(id='d', ctx=Load())]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('{"a":1, **d}', mode='eval'), indent=4))
+ Expression(
+ body=Dict(
+ keys=[
+ Constant(value='a', kind=None),
+ None],
+ values=[
+ Constant(value=1, kind=None),
+ Name(id='d', ctx=Load())]))
Variables
@@ -385,14 +366,11 @@ Expressions
.. doctest::
- >>> print(ast.dump(ast.parse("not x"), indent=4))
- Module(
- body=[
- Expr(
- value=UnaryOp(
- op=Not(),
- operand=Name(id='x', ctx=Load())))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('not x', mode='eval'), indent=4))
+ Expression(
+ body=UnaryOp(
+ op=Not(),
+ operand=Name(id='x', ctx=Load())))
.. class:: BinOp(left, op, right)
@@ -402,15 +380,12 @@ Expressions
.. doctest::
- >>> print(ast.dump(ast.parse("x + y"), indent=4))
- Module(
- body=[
- Expr(
- value=BinOp(
- left=Name(id='x', ctx=Load()),
- op=Add(),
- right=Name(id='y', ctx=Load())))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('x + y', mode='eval'), indent=4))
+ Expression(
+ body=BinOp(
+ left=Name(id='x', ctx=Load()),
+ op=Add(),
+ right=Name(id='y', ctx=Load())))
.. class:: Add
@@ -441,16 +416,13 @@ Expressions
.. doctest::
- >>> print(ast.dump(ast.parse("x or y"), indent=4))
- Module(
- body=[
- Expr(
- value=BoolOp(
- op=Or(),
- values=[
- Name(id='x', ctx=Load()),
- Name(id='y', ctx=Load())]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('x or y', mode='eval'), indent=4))
+ Expression(
+ body=BoolOp(
+ op=Or(),
+ values=[
+ Name(id='x', ctx=Load()),
+ Name(id='y', ctx=Load())]))
.. class:: And
@@ -467,19 +439,16 @@ Expressions
.. doctest::
- >>> print(ast.dump(ast.parse("1 < a < 10"), indent=4))
- Module(
- body=[
- Expr(
- value=Compare(
- left=Constant(value=1, kind=None),
- ops=[
- Lt(),
- Lt()],
- comparators=[
- Name(id='a', ctx=Load()),
- Constant(value=10, kind=None)]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('1 <= a < 10', mode='eval'), indent=4))
+ Expression(
+ body=Compare(
+ left=Constant(value=1, kind=None),
+ ops=[
+ LtE(),
+ Lt()],
+ comparators=[
+ Name(id='a', ctx=Load()),
+ Constant(value=10, kind=None)]))
.. class:: Eq
@@ -510,25 +479,22 @@ Expressions
.. doctest::
- >>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)'), indent=4))
- Module(
- body=[
- Expr(
- value=Call(
- func=Name(id='func', ctx=Load()),
- args=[
- Name(id='a', ctx=Load()),
- Starred(
- value=Name(id='d', ctx=Load()),
- ctx=Load())],
- keywords=[
- keyword(
- arg='b',
- value=Name(id='c', ctx=Load())),
- keyword(
- arg=None,
- value=Name(id='e', ctx=Load()))]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)', mode='eval'), indent=4))
+ Expression(
+ body=Call(
+ func=Name(id='func', ctx=Load()),
+ args=[
+ Name(id='a', ctx=Load()),
+ Starred(
+ value=Name(id='d', ctx=Load()),
+ ctx=Load())],
+ keywords=[
+ keyword(
+ arg='b',
+ value=Name(id='c', ctx=Load())),
+ keyword(
+ arg=None,
+ value=Name(id='e', ctx=Load()))]))
.. class:: keyword(arg, value)
@@ -544,15 +510,12 @@ Expressions
.. doctest::
- >>> print(ast.dump(ast.parse("a if b else c"), indent=4))
- Module(
- body=[
- Expr(
- value=IfExp(
- test=Name(id='b', ctx=Load()),
- body=Name(id='a', ctx=Load()),
- orelse=Name(id='c', ctx=Load())))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('a if b else c', mode='eval'), indent=4))
+ Expression(
+ body=IfExp(
+ test=Name(id='b', ctx=Load()),
+ body=Name(id='a', ctx=Load()),
+ orelse=Name(id='c', ctx=Load())))
.. class:: Attribute(value, attr, ctx)
@@ -564,15 +527,12 @@ Expressions
.. doctest::
- >>> print(ast.dump(ast.parse('snake.colour'), indent=4))
- Module(
- body=[
- Expr(
- value=Attribute(
- value=Name(id='snake', ctx=Load()),
- attr='colour',
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('snake.colour', mode='eval'), indent=4))
+ Expression(
+ body=Attribute(
+ value=Name(id='snake', ctx=Load()),
+ attr='colour',
+ ctx=Load()))
.. class:: NamedExpr(target, value)
@@ -584,14 +544,11 @@ Expressions
.. doctest::
- >>> print(ast.dump(ast.parse("(x := 4)"), indent=4))
- Module(
- body=[
- Expr(
- value=NamedExpr(
- target=Name(id='x', ctx=Store()),
- value=Constant(value=4, kind=None)))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('(x := 4)', mode='eval'), indent=4))
+ Expression(
+ body=NamedExpr(
+ target=Name(id='x', ctx=Store()),
+ value=Constant(value=4, kind=None)))
Subscripting
@@ -611,16 +568,13 @@ Subscripting
.. doctest::
- >>> print(ast.dump(ast.parse('l[1]'), indent=4))
- Module(
- body=[
- Expr(
- value=Subscript(
- value=Name(id='l', ctx=Load()),
- slice=Index(
- value=Constant(value=1, kind=None)),
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('l[1]', mode='eval'), indent=4))
+ Expression(
+ body=Subscript(
+ value=Name(id='l', ctx=Load()),
+ slice=Index(
+ value=Constant(value=1, kind=None)),
+ ctx=Load()))
.. class:: Slice(lower, upper, step)
@@ -629,18 +583,15 @@ Subscripting
.. doctest::
- >>> print(ast.dump(ast.parse('l[1:2]'), indent=4))
- Module(
- body=[
- Expr(
- value=Subscript(
- value=Name(id='l', ctx=Load()),
- slice=Slice(
- lower=Constant(value=1, kind=None),
- upper=Constant(value=2, kind=None),
- step=None),
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('l[1:2]', mode='eval'), indent=4))
+ Expression(
+ body=Subscript(
+ value=Name(id='l', ctx=Load()),
+ slice=Slice(
+ lower=Constant(value=1, kind=None),
+ upper=Constant(value=2, kind=None),
+ step=None),
+ ctx=Load()))
.. class:: ExtSlice(dims)
@@ -650,22 +601,19 @@ Subscripting
.. doctest::
- >>> print(ast.dump(ast.parse('l[1:2, 3]'), indent=4))
- Module(
- body=[
- Expr(
- value=Subscript(
- value=Name(id='l', ctx=Load()),
- slice=ExtSlice(
- dims=[
- Slice(
- lower=Constant(value=1, kind=None),
- upper=Constant(value=2, kind=None),
- step=None),
- Index(
- value=Constant(value=3, kind=None))]),
- ctx=Load()))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('l[1:2, 3]', mode='eval'), indent=4))
+ Expression(
+ body=Subscript(
+ value=Name(id='l', ctx=Load()),
+ slice=ExtSlice(
+ dims=[
+ Slice(
+ lower=Constant(value=1, kind=None),
+ upper=Constant(value=2, kind=None),
+ step=None),
+ Index(
+ value=Constant(value=3, kind=None))]),
+ ctx=Load()))
Comprehensions
@@ -684,51 +632,40 @@ Comprehensions
.. doctest::
- >>> print(ast.dump(ast.parse("[x for x in numbers]"), indent=4))
- Module(
- body=[
- Expr(
- value=ListComp(
- elt=Name(id='x', ctx=Load()),
- generators=[
- comprehension(
- target=Name(id='x', ctx=Store()),
- iter=Name(id='numbers', ctx=Load()),
- ifs=[],
- is_async=0)]))],
- type_ignores=[])
-
- >>> print(ast.dump(ast.parse("{x: x**2 for x in numbers}"), indent=4))
- Module(
- body=[
- Expr(
- value=DictComp(
- key=Name(id='x', ctx=Load()),
- value=BinOp(
- left=Name(id='x', ctx=Load()),
- op=Pow(),
- right=Constant(value=2, kind=None)),
- generators=[
- comprehension(
- target=Name(id='x', ctx=Store()),
- iter=Name(id='numbers', ctx=Load()),
- ifs=[],
- is_async=0)]))],
- type_ignores=[])
-
- >>> print(ast.dump(ast.parse("{x for x in numbers}"), indent=4))
- Module(
- body=[
- Expr(
- value=SetComp(
- elt=Name(id='x', ctx=Load()),
- generators=[
- comprehension(
- target=Name(id='x', ctx=Store()),
- iter=Name(id='numbers', ctx=Load()),
- ifs=[],
- is_async=0)]))],
- type_ignores=[])
+ >>> print(ast.dump(ast.parse('[x for x in numbers]', mode='eval'), indent=4))
+ Expression(
+ body=ListComp(
+ elt=Name(id='x', ctx=Load()),
+ generators=[
+ comprehension(
+ target=Name(id='x', ctx=Store()),
+ iter=Name(id='numbers', ctx=Load()),
+ ifs=[],
+ is_async=0)]))
+ >>> print(ast.dump(ast.parse('{x: x**2 for x in numbers}', mode='eval'), indent=4))
+ Expression(
+ body=DictComp(
+ key=Name(id='x', ctx=Load()),
+ value=BinOp(
+ left=Name(id='x', ctx=Load()),
+ op=Pow(),
+ right=Constant(value=2, kind=None)),
+ generators=[
+ comprehension(
+ target=Name(id='x', ctx=Store()),
+ iter=Name(id='numbers', ctx=Load()),
+ ifs=[],
+ is_async=0)]))
+ >>> print(ast.dump(ast.parse('{x for x in numbers}', mode='eval'), indent=4))
+ Expression(
+ body=SetComp(
+ elt=Name(id='x', ctx=Load()),
+ generators=[
+ comprehension(
+ target=Name(id='x', ctx=Store()),
+ iter=Name(id='numbers', ctx=Load()),
+ ifs=[],
+ is_async=0)]))
.. class:: comprehension(target, iter, ifs, is_async)
@@ -743,7 +680,7 @@ Comprehensions
.. doctest::
- >>> print(ast.dump(ast.parse("[ord(c) for line in file for c in line]", mode='eval'),
+ >>> print(ast.dump(ast.parse('[ord(c) for line in file for c in line]', mode='eval'),
... indent=4)) # Multiple comprehensions in one.
Expression(
body=ListComp(
@@ -764,7 +701,7 @@ Comprehensions
ifs=[],
is_async=0)]))
- >>> print(ast.dump(ast.parse("(n**2 for n in it if n>5 if n<10)", mode='eval'),
+ >>> print(ast.dump(ast.parse('(n**2 for n in it if n>5 if n<10)', mode='eval'),
... indent=4)) # generator comprehension
Expression(
body=GeneratorExp(
@@ -791,35 +728,17 @@ Comprehensions
Constant(value=10, kind=None)])],
is_async=0)]))
- >>> print(ast.dump(ast.parse("async def f():"
- ... " return [i async for i in soc]"),
+ >>> print(ast.dump(ast.parse('[i async for i in soc]', mode='eval'),
... indent=4)) # Async comprehension
- Module(
- body=[
- AsyncFunctionDef(
- name='f',
- args=arguments(
- posonlyargs=[],
- args=[],
- vararg=None,
- kwonlyargs=[],
- kw_defaults=[],
- kwarg=None,
- defaults=[]),
- body=[
- Return(
- value=ListComp(
- elt=Name(id='i', ctx=Load()),
- generators=[
- comprehension(
- target=Name(id='i', ctx=Store()),
- iter=Name(id='soc', ctx=Load()),
- ifs=[],
- is_async=1)]))],
- decorator_list=[],
- returns=None,
- type_comment=None)],
- type_ignores=[])
+ Expression(
+ body=ListComp(
+ elt=Name(id='i', ctx=Load()),
+ generators=[
+ comprehension(
+ target=Name(id='i', ctx=Store()),
+ iter=Name(id='soc', ctx=Load()),
+ ifs=[],
+ is_async=1)]))
Statements
^^^^^^^^^^
@@ -838,7 +757,7 @@ Statements
.. doctest::
- >>> print(ast.dump(ast.parse("a = b = 1"), indent=4)) # Multiple assignment
+ >>> print(ast.dump(ast.parse('a = b = 1'), indent=4)) # Multiple assignment
Module(
body=[
Assign(
@@ -849,7 +768,7 @@ Statements
type_comment=None)],
type_ignores=[])
- >>> print(ast.dump(ast.parse("a,b = c"), indent=4)) # Unpacking
+ >>> print(ast.dump(ast.parse('a,b = c'), indent=4)) # Unpacking
Module(
body=[
Assign(
@@ -875,7 +794,7 @@ Statements
.. doctest::
- >>> print(ast.dump(ast.parse("c: int"), indent=4))
+ >>> print(ast.dump(ast.parse('c: int'), indent=4))
Module(
body=[
AnnAssign(
@@ -885,7 +804,7 @@ Statements
simple=1)],
type_ignores=[])
- >>> print(ast.dump(ast.parse("(a): int = 1"), indent=4)) # Annotation with parenthesis
+ >>> print(ast.dump(ast.parse('(a): int = 1'), indent=4)) # Annotation with parenthesis
Module(
body=[
AnnAssign(
@@ -895,7 +814,7 @@ Statements
simple=0)],
type_ignores=[])
- >>> print(ast.dump(ast.parse("a.b: int"), indent=4)) # Attribute annotation
+ >>> print(ast.dump(ast.parse('a.b: int'), indent=4)) # Attribute annotation
Module(
body=[
AnnAssign(
@@ -908,7 +827,7 @@ Statements
simple=0)],
type_ignores=[])
- >>> print(ast.dump(ast.parse("a[1]: int"), indent=4)) # Subscript annotation
+ >>> print(ast.dump(ast.parse('a[1]: int'), indent=4)) # Subscript annotation
Module(
body=[
AnnAssign(
@@ -935,7 +854,7 @@ Statements
.. doctest::
- >>> print(ast.dump(ast.parse("x += 2"), indent=4))
+ >>> print(ast.dump(ast.parse('x += 2'), indent=4))
Module(
body=[
AugAssign(
@@ -953,7 +872,7 @@ Statements
.. doctest::
- >>> print(ast.dump(ast.parse("raise x from y"), indent=4))
+ >>> print(ast.dump(ast.parse('raise x from y'), indent=4))
Module(
body=[
Raise(
@@ -969,7 +888,7 @@ Statements
.. doctest::
- >>> print(ast.dump(ast.parse("assert x,y"), indent=4))
+ >>> print(ast.dump(ast.parse('assert x,y'), indent=4))
Module(
body=[
Assert(
@@ -985,7 +904,7 @@ Statements
.. doctest::
- >>> print(ast.dump(ast.parse("del x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('del x,y,z'), indent=4))
Module(
body=[
Delete(
@@ -1002,7 +921,7 @@ Statements
.. doctest::
- >>> print(ast.dump(ast.parse("pass"), indent=4))
+ >>> print(ast.dump(ast.parse('pass'), indent=4))
Module(
body=[
Pass()],
@@ -1021,7 +940,7 @@ Imports
.. doctest::
- >>> print(ast.dump(ast.parse("import x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('import x,y,z'), indent=4))
Module(
body=[
Import(
@@ -1041,7 +960,7 @@ Imports
.. doctest::
- >>> print(ast.dump(ast.parse("from y import x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('from y import x,y,z'), indent=4))
Module(
body=[
ImportFrom(
@@ -1061,7 +980,7 @@ Imports
.. doctest::
- >>> print(ast.dump(ast.parse("from ..foo.bar import a as b, c"), indent=4))
+ >>> print(ast.dump(ast.parse('from ..foo.bar import a as b, c'), indent=4))
Module(
body=[
ImportFrom(
@@ -1368,7 +1287,7 @@ Function and class definitions
.. doctest::
- >>> print(ast.dump(ast.parse("lambda x,y: ..."), indent=4))
+ >>> print(ast.dump(ast.parse('lambda x,y: ...'), indent=4))
Module(
body=[
Expr(
@@ -1459,7 +1378,7 @@ Function and class definitions
.. doctest::
- >>> print(ast.dump(ast.parse("return 4"), indent=4))
+ >>> print(ast.dump(ast.parse('return 4'), indent=4))
Module(
body=[
Return(
@@ -1475,7 +1394,7 @@ Function and class definitions
.. doctest::
- >>> print(ast.dump(ast.parse("yield x"), indent=4))
+ >>> print(ast.dump(ast.parse('yield x'), indent=4))
Module(
body=[
Expr(
@@ -1483,7 +1402,7 @@ Function and class definitions
value=Name(id='x', ctx=Load())))],
type_ignores=[])
- >>> print(ast.dump(ast.parse("yield from x"), indent=4))
+ >>> print(ast.dump(ast.parse('yield from x'), indent=4))
Module(
body=[
Expr(
@@ -1499,7 +1418,7 @@ Function and class definitions
.. doctest::
- >>> print(ast.dump(ast.parse("global x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('global x,y,z'), indent=4))
Module(
body=[
Global(
@@ -1509,7 +1428,7 @@ Function and class definitions
'z'])],
type_ignores=[])
- >>> print(ast.dump(ast.parse("nonlocal x,y,z"), indent=4))
+ >>> print(ast.dump(ast.parse('nonlocal x,y,z'), indent=4))
Module(
body=[
Nonlocal(