bpo-39199: Use 'eval' mode for the examples with expression nodes (GH-18828)

Co-Authored-By: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Pablo Galindo 2020-03-07 18:22:58 +00:00 committed by GitHub
parent eb7560a73d
commit 02f64cb791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 211 additions and 292 deletions

View File

@ -139,12 +139,9 @@ Literals
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("123"), indent=4)) >>> print(ast.dump(ast.parse('123', mode='eval'), indent=4))
Module( Expression(
body=[ body=Constant(value=123, kind=None))
Expr(
value=Constant(value=123, kind=None))],
type_ignores=[])
.. class:: FormattedValue(value, conversion, format_spec) .. class:: FormattedValue(value, conversion, format_spec)
@ -174,11 +171,9 @@ Literals
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"'), indent=4)) >>> print(ast.dump(ast.parse('f"sin({a}) is {sin(a):.3}"', mode='eval'), indent=4))
Module( Expression(
body=[ body=JoinedStr(
Expr(
value=JoinedStr(
values=[ values=[
Constant(value='sin(', kind=None), Constant(value='sin(', kind=None),
FormattedValue( FormattedValue(
@ -195,8 +190,7 @@ Literals
conversion=-1, conversion=-1,
format_spec=JoinedStr( format_spec=JoinedStr(
values=[ values=[
Constant(value='.3', kind=None)]))]))], Constant(value='.3', kind=None)]))]))
type_ignores=[])
.. class:: List(elts, ctx) .. class:: List(elts, ctx)
@ -208,29 +202,22 @@ Literals
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("[1, 2, 3]"), indent=4)) >>> print(ast.dump(ast.parse('[1, 2, 3]', mode='eval'), indent=4))
Module( Expression(
body=[ body=List(
Expr(
value=List(
elts=[ elts=[
Constant(value=1, kind=None), Constant(value=1, kind=None),
Constant(value=2, kind=None), Constant(value=2, kind=None),
Constant(value=3, kind=None)], Constant(value=3, kind=None)],
ctx=Load()))], ctx=Load()))
type_ignores=[]) >>> print(ast.dump(ast.parse('(1, 2, 3)', mode='eval'), indent=4))
Expression(
>>> print(ast.dump(ast.parse("(1, 2, 3)"), indent=4)) body=Tuple(
Module(
body=[
Expr(
value=Tuple(
elts=[ elts=[
Constant(value=1, kind=None), Constant(value=1, kind=None),
Constant(value=2, kind=None), Constant(value=2, kind=None),
Constant(value=3, kind=None)], Constant(value=3, kind=None)],
ctx=Load()))], ctx=Load()))
type_ignores=[])
.. class:: Set(elts) .. class:: Set(elts)
@ -239,16 +226,13 @@ Literals
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("{1, 2, 3}"), indent=4)) >>> print(ast.dump(ast.parse('{1, 2, 3}', mode='eval'), indent=4))
Module( Expression(
body=[ body=Set(
Expr(
value=Set(
elts=[ elts=[
Constant(value=1, kind=None), Constant(value=1, kind=None),
Constant(value=2, kind=None), Constant(value=2, kind=None),
Constant(value=3, kind=None)]))], Constant(value=3, kind=None)]))
type_ignores=[])
.. class:: Dict(keys, values) .. class:: Dict(keys, values)
@ -263,18 +247,15 @@ Literals
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("{'a':1, **d}"), indent=4)) >>> print(ast.dump(ast.parse('{"a":1, **d}', mode='eval'), indent=4))
Module( Expression(
body=[ body=Dict(
Expr(
value=Dict(
keys=[ keys=[
Constant(value='a', kind=None), Constant(value='a', kind=None),
None], None],
values=[ values=[
Constant(value=1, kind=None), Constant(value=1, kind=None),
Name(id='d', ctx=Load())]))], Name(id='d', ctx=Load())]))
type_ignores=[])
Variables Variables
@ -385,14 +366,11 @@ Expressions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("not x"), indent=4)) >>> print(ast.dump(ast.parse('not x', mode='eval'), indent=4))
Module( Expression(
body=[ body=UnaryOp(
Expr(
value=UnaryOp(
op=Not(), op=Not(),
operand=Name(id='x', ctx=Load())))], operand=Name(id='x', ctx=Load())))
type_ignores=[])
.. class:: BinOp(left, op, right) .. class:: BinOp(left, op, right)
@ -402,15 +380,12 @@ Expressions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("x + y"), indent=4)) >>> print(ast.dump(ast.parse('x + y', mode='eval'), indent=4))
Module( Expression(
body=[ body=BinOp(
Expr(
value=BinOp(
left=Name(id='x', ctx=Load()), left=Name(id='x', ctx=Load()),
op=Add(), op=Add(),
right=Name(id='y', ctx=Load())))], right=Name(id='y', ctx=Load())))
type_ignores=[])
.. class:: Add .. class:: Add
@ -441,16 +416,13 @@ Expressions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("x or y"), indent=4)) >>> print(ast.dump(ast.parse('x or y', mode='eval'), indent=4))
Module( Expression(
body=[ body=BoolOp(
Expr(
value=BoolOp(
op=Or(), op=Or(),
values=[ values=[
Name(id='x', ctx=Load()), Name(id='x', ctx=Load()),
Name(id='y', ctx=Load())]))], Name(id='y', ctx=Load())]))
type_ignores=[])
.. class:: And .. class:: And
@ -467,19 +439,16 @@ Expressions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("1 < a < 10"), indent=4)) >>> print(ast.dump(ast.parse('1 <= a < 10', mode='eval'), indent=4))
Module( Expression(
body=[ body=Compare(
Expr(
value=Compare(
left=Constant(value=1, kind=None), left=Constant(value=1, kind=None),
ops=[ ops=[
Lt(), LtE(),
Lt()], Lt()],
comparators=[ comparators=[
Name(id='a', ctx=Load()), Name(id='a', ctx=Load()),
Constant(value=10, kind=None)]))], Constant(value=10, kind=None)]))
type_ignores=[])
.. class:: Eq .. class:: Eq
@ -510,11 +479,9 @@ Expressions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)'), indent=4)) >>> print(ast.dump(ast.parse('func(a, b=c, *d, **e)', mode='eval'), indent=4))
Module( Expression(
body=[ body=Call(
Expr(
value=Call(
func=Name(id='func', ctx=Load()), func=Name(id='func', ctx=Load()),
args=[ args=[
Name(id='a', ctx=Load()), Name(id='a', ctx=Load()),
@ -527,8 +494,7 @@ Expressions
value=Name(id='c', ctx=Load())), value=Name(id='c', ctx=Load())),
keyword( keyword(
arg=None, arg=None,
value=Name(id='e', ctx=Load()))]))], value=Name(id='e', ctx=Load()))]))
type_ignores=[])
.. class:: keyword(arg, value) .. class:: keyword(arg, value)
@ -544,15 +510,12 @@ Expressions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("a if b else c"), indent=4)) >>> print(ast.dump(ast.parse('a if b else c', mode='eval'), indent=4))
Module( Expression(
body=[ body=IfExp(
Expr(
value=IfExp(
test=Name(id='b', ctx=Load()), test=Name(id='b', ctx=Load()),
body=Name(id='a', ctx=Load()), body=Name(id='a', ctx=Load()),
orelse=Name(id='c', ctx=Load())))], orelse=Name(id='c', ctx=Load())))
type_ignores=[])
.. class:: Attribute(value, attr, ctx) .. class:: Attribute(value, attr, ctx)
@ -564,15 +527,12 @@ Expressions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse('snake.colour'), indent=4)) >>> print(ast.dump(ast.parse('snake.colour', mode='eval'), indent=4))
Module( Expression(
body=[ body=Attribute(
Expr(
value=Attribute(
value=Name(id='snake', ctx=Load()), value=Name(id='snake', ctx=Load()),
attr='colour', attr='colour',
ctx=Load()))], ctx=Load()))
type_ignores=[])
.. class:: NamedExpr(target, value) .. class:: NamedExpr(target, value)
@ -584,14 +544,11 @@ Expressions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("(x := 4)"), indent=4)) >>> print(ast.dump(ast.parse('(x := 4)', mode='eval'), indent=4))
Module( Expression(
body=[ body=NamedExpr(
Expr(
value=NamedExpr(
target=Name(id='x', ctx=Store()), target=Name(id='x', ctx=Store()),
value=Constant(value=4, kind=None)))], value=Constant(value=4, kind=None)))
type_ignores=[])
Subscripting Subscripting
@ -611,16 +568,13 @@ Subscripting
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse('l[1]'), indent=4)) >>> print(ast.dump(ast.parse('l[1]', mode='eval'), indent=4))
Module( Expression(
body=[ body=Subscript(
Expr(
value=Subscript(
value=Name(id='l', ctx=Load()), value=Name(id='l', ctx=Load()),
slice=Index( slice=Index(
value=Constant(value=1, kind=None)), value=Constant(value=1, kind=None)),
ctx=Load()))], ctx=Load()))
type_ignores=[])
.. class:: Slice(lower, upper, step) .. class:: Slice(lower, upper, step)
@ -629,18 +583,15 @@ Subscripting
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse('l[1:2]'), indent=4)) >>> print(ast.dump(ast.parse('l[1:2]', mode='eval'), indent=4))
Module( Expression(
body=[ body=Subscript(
Expr(
value=Subscript(
value=Name(id='l', ctx=Load()), value=Name(id='l', ctx=Load()),
slice=Slice( slice=Slice(
lower=Constant(value=1, kind=None), lower=Constant(value=1, kind=None),
upper=Constant(value=2, kind=None), upper=Constant(value=2, kind=None),
step=None), step=None),
ctx=Load()))], ctx=Load()))
type_ignores=[])
.. class:: ExtSlice(dims) .. class:: ExtSlice(dims)
@ -650,11 +601,9 @@ Subscripting
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse('l[1:2, 3]'), indent=4)) >>> print(ast.dump(ast.parse('l[1:2, 3]', mode='eval'), indent=4))
Module( Expression(
body=[ body=Subscript(
Expr(
value=Subscript(
value=Name(id='l', ctx=Load()), value=Name(id='l', ctx=Load()),
slice=ExtSlice( slice=ExtSlice(
dims=[ dims=[
@ -664,8 +613,7 @@ Subscripting
step=None), step=None),
Index( Index(
value=Constant(value=3, kind=None))]), value=Constant(value=3, kind=None))]),
ctx=Load()))], ctx=Load()))
type_ignores=[])
Comprehensions Comprehensions
@ -684,25 +632,19 @@ Comprehensions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("[x for x in numbers]"), indent=4)) >>> print(ast.dump(ast.parse('[x for x in numbers]', mode='eval'), indent=4))
Module( Expression(
body=[ body=ListComp(
Expr(
value=ListComp(
elt=Name(id='x', ctx=Load()), elt=Name(id='x', ctx=Load()),
generators=[ generators=[
comprehension( comprehension(
target=Name(id='x', ctx=Store()), target=Name(id='x', ctx=Store()),
iter=Name(id='numbers', ctx=Load()), iter=Name(id='numbers', ctx=Load()),
ifs=[], ifs=[],
is_async=0)]))], is_async=0)]))
type_ignores=[]) >>> print(ast.dump(ast.parse('{x: x**2 for x in numbers}', mode='eval'), indent=4))
Expression(
>>> print(ast.dump(ast.parse("{x: x**2 for x in numbers}"), indent=4)) body=DictComp(
Module(
body=[
Expr(
value=DictComp(
key=Name(id='x', ctx=Load()), key=Name(id='x', ctx=Load()),
value=BinOp( value=BinOp(
left=Name(id='x', ctx=Load()), left=Name(id='x', ctx=Load()),
@ -713,22 +655,17 @@ Comprehensions
target=Name(id='x', ctx=Store()), target=Name(id='x', ctx=Store()),
iter=Name(id='numbers', ctx=Load()), iter=Name(id='numbers', ctx=Load()),
ifs=[], ifs=[],
is_async=0)]))], is_async=0)]))
type_ignores=[]) >>> print(ast.dump(ast.parse('{x for x in numbers}', mode='eval'), indent=4))
Expression(
>>> print(ast.dump(ast.parse("{x for x in numbers}"), indent=4)) body=SetComp(
Module(
body=[
Expr(
value=SetComp(
elt=Name(id='x', ctx=Load()), elt=Name(id='x', ctx=Load()),
generators=[ generators=[
comprehension( comprehension(
target=Name(id='x', ctx=Store()), target=Name(id='x', ctx=Store()),
iter=Name(id='numbers', ctx=Load()), iter=Name(id='numbers', ctx=Load()),
ifs=[], ifs=[],
is_async=0)]))], is_async=0)]))
type_ignores=[])
.. class:: comprehension(target, iter, ifs, is_async) .. class:: comprehension(target, iter, ifs, is_async)
@ -743,7 +680,7 @@ Comprehensions
.. doctest:: .. 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. ... indent=4)) # Multiple comprehensions in one.
Expression( Expression(
body=ListComp( body=ListComp(
@ -764,7 +701,7 @@ Comprehensions
ifs=[], ifs=[],
is_async=0)])) 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 ... indent=4)) # generator comprehension
Expression( Expression(
body=GeneratorExp( body=GeneratorExp(
@ -791,35 +728,17 @@ Comprehensions
Constant(value=10, kind=None)])], Constant(value=10, kind=None)])],
is_async=0)])) is_async=0)]))
>>> print(ast.dump(ast.parse("async def f():" >>> print(ast.dump(ast.parse('[i async for i in soc]', mode='eval'),
... " return [i async for i in soc]"),
... indent=4)) # Async comprehension ... indent=4)) # Async comprehension
Module( Expression(
body=[ body=ListComp(
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()), elt=Name(id='i', ctx=Load()),
generators=[ generators=[
comprehension( comprehension(
target=Name(id='i', ctx=Store()), target=Name(id='i', ctx=Store()),
iter=Name(id='soc', ctx=Load()), iter=Name(id='soc', ctx=Load()),
ifs=[], ifs=[],
is_async=1)]))], is_async=1)]))
decorator_list=[],
returns=None,
type_comment=None)],
type_ignores=[])
Statements Statements
^^^^^^^^^^ ^^^^^^^^^^
@ -838,7 +757,7 @@ Statements
.. doctest:: .. 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( Module(
body=[ body=[
Assign( Assign(
@ -849,7 +768,7 @@ Statements
type_comment=None)], type_comment=None)],
type_ignores=[]) 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( Module(
body=[ body=[
Assign( Assign(
@ -875,7 +794,7 @@ Statements
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("c: int"), indent=4)) >>> print(ast.dump(ast.parse('c: int'), indent=4))
Module( Module(
body=[ body=[
AnnAssign( AnnAssign(
@ -885,7 +804,7 @@ Statements
simple=1)], simple=1)],
type_ignores=[]) 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( Module(
body=[ body=[
AnnAssign( AnnAssign(
@ -895,7 +814,7 @@ Statements
simple=0)], simple=0)],
type_ignores=[]) 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( Module(
body=[ body=[
AnnAssign( AnnAssign(
@ -908,7 +827,7 @@ Statements
simple=0)], simple=0)],
type_ignores=[]) 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( Module(
body=[ body=[
AnnAssign( AnnAssign(
@ -935,7 +854,7 @@ Statements
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("x += 2"), indent=4)) >>> print(ast.dump(ast.parse('x += 2'), indent=4))
Module( Module(
body=[ body=[
AugAssign( AugAssign(
@ -953,7 +872,7 @@ Statements
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("raise x from y"), indent=4)) >>> print(ast.dump(ast.parse('raise x from y'), indent=4))
Module( Module(
body=[ body=[
Raise( Raise(
@ -969,7 +888,7 @@ Statements
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("assert x,y"), indent=4)) >>> print(ast.dump(ast.parse('assert x,y'), indent=4))
Module( Module(
body=[ body=[
Assert( Assert(
@ -985,7 +904,7 @@ Statements
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("del x,y,z"), indent=4)) >>> print(ast.dump(ast.parse('del x,y,z'), indent=4))
Module( Module(
body=[ body=[
Delete( Delete(
@ -1002,7 +921,7 @@ Statements
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("pass"), indent=4)) >>> print(ast.dump(ast.parse('pass'), indent=4))
Module( Module(
body=[ body=[
Pass()], Pass()],
@ -1021,7 +940,7 @@ Imports
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("import x,y,z"), indent=4)) >>> print(ast.dump(ast.parse('import x,y,z'), indent=4))
Module( Module(
body=[ body=[
Import( Import(
@ -1041,7 +960,7 @@ Imports
.. doctest:: .. 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( Module(
body=[ body=[
ImportFrom( ImportFrom(
@ -1061,7 +980,7 @@ Imports
.. doctest:: .. 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( Module(
body=[ body=[
ImportFrom( ImportFrom(
@ -1368,7 +1287,7 @@ Function and class definitions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("lambda x,y: ..."), indent=4)) >>> print(ast.dump(ast.parse('lambda x,y: ...'), indent=4))
Module( Module(
body=[ body=[
Expr( Expr(
@ -1459,7 +1378,7 @@ Function and class definitions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("return 4"), indent=4)) >>> print(ast.dump(ast.parse('return 4'), indent=4))
Module( Module(
body=[ body=[
Return( Return(
@ -1475,7 +1394,7 @@ Function and class definitions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("yield x"), indent=4)) >>> print(ast.dump(ast.parse('yield x'), indent=4))
Module( Module(
body=[ body=[
Expr( Expr(
@ -1483,7 +1402,7 @@ Function and class definitions
value=Name(id='x', ctx=Load())))], value=Name(id='x', ctx=Load())))],
type_ignores=[]) type_ignores=[])
>>> print(ast.dump(ast.parse("yield from x"), indent=4)) >>> print(ast.dump(ast.parse('yield from x'), indent=4))
Module( Module(
body=[ body=[
Expr( Expr(
@ -1499,7 +1418,7 @@ Function and class definitions
.. doctest:: .. doctest::
>>> print(ast.dump(ast.parse("global x,y,z"), indent=4)) >>> print(ast.dump(ast.parse('global x,y,z'), indent=4))
Module( Module(
body=[ body=[
Global( Global(
@ -1509,7 +1428,7 @@ Function and class definitions
'z'])], 'z'])],
type_ignores=[]) 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( Module(
body=[ body=[
Nonlocal( Nonlocal(