Skip to content

Commit 6aa5cd7

Browse files
committed
Accept suggestions
1 parent de328af commit 6aa5cd7

File tree

2 files changed

+44
-27
lines changed

2 files changed

+44
-27
lines changed

Lib/ast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def unparse(ast_obj):
626626
return unparser.visit(ast_obj)
627627

628628

629-
def _main(args=None):
629+
def main(args=None):
630630
import argparse
631631
import sys
632632

@@ -656,4 +656,4 @@ def _main(args=None):
656656
print(dump(tree, include_attributes=args.include_attributes, indent=args.indent))
657657

658658
if __name__ == '__main__':
659-
_main()
659+
main()

Lib/test/test_ast/test_ast.py

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3186,20 +3186,21 @@ def setUp(self):
31863186

31873187
@staticmethod
31883188
def text_normalize(string):
3189-
"""Dedent *string* and strip it from its surrounding whitespaces.
3190-
This method is used by the other utility functions so that any
3191-
string to write or to match against can be freely indented.
3192-
"""
31933189
return textwrap.dedent(string).strip()
31943190

31953191
def set_source(self, content):
31963192
Path(self.filename).write_text(self.text_normalize(content))
31973193

31983194
def invoke_ast(self, *flags):
3199-
output = StringIO()
3200-
with contextlib.redirect_stdout(output):
3201-
ast._main(args=[*flags, self.filename])
3202-
return self.text_normalize(output.getvalue())
3195+
stderr = StringIO()
3196+
stdout = StringIO()
3197+
with (
3198+
contextlib.redirect_stdout(stdout),
3199+
contextlib.redirect_stderr(stderr),
3200+
):
3201+
ast.main(args=[*flags, self.filename])
3202+
self.assertEqual(stderr.getvalue(), '')
3203+
return self.text_normalize(stdout.getvalue())
32033204

32043205
def check_output(self, source, expect, *flags):
32053206
with self.subTest(source=source, flags=flags):
@@ -3218,39 +3219,49 @@ def test_invocation(self):
32183219
)
32193220
self.set_source('''
32203221
print(1, 2, 3)
3221-
def f(x):
3222+
def f(x: int) -> int:
32223223
x -= 1
32233224
return x
32243225
''')
32253226

32263227
for r in range(1, len(base_flags) + 1):
32273228
for choices in itertools.combinations(base_flags, r=r):
32283229
for args in itertools.product(*choices):
3229-
with self.subTest(args=args[1:]):
3230+
with self.subTest(flags=args[1:]):
32303231
_ = self.invoke_ast(*args)
32313232

3233+
def test_unknown_flag(self):
32323234
with self.assertRaises(SystemExit):
3233-
# suppress argparse error message
3234-
with contextlib.redirect_stderr(StringIO()):
3235-
output = self.invoke_ast('--unknown')
3236-
self.assertStartsWith(output, 'usage: ')
3237-
3238-
def test_mode_flag(self):
3239-
# test 'python -m ast -m/--mode'
3240-
source = 'print(1, 2, 3)'
3235+
output = self.invoke_ast('--unknown')
3236+
self.assertStartsWith(output, 'usage: ')
3237+
3238+
def test_help_flag(self):
3239+
# test 'python -m ast -h/--help'
3240+
for flag in ('-h', '--help'):
3241+
with self.subTest(flags=flag):
3242+
with self.assertRaises(SystemExit):
3243+
output = self.invoke_ast(flag)
3244+
self.assertStartsWith(output, 'usage: ')
3245+
3246+
def test_exec_mode_flag(self):
3247+
# test 'python -m ast -m/--mode exec'
3248+
source = 'x: bool = 1 # type: ignore[assignment]'
32413249
expect = '''
32423250
Module(
32433251
body=[
3244-
Expr(
3245-
value=Call(
3246-
func=Name(id='print', ctx=Load()),
3247-
args=[
3248-
Constant(value=1),
3249-
Constant(value=2),
3250-
Constant(value=3)]))])
3252+
AnnAssign(
3253+
target=Name(id='x', ctx=Store()),
3254+
annotation=Name(id='bool', ctx=Load()),
3255+
value=Constant(value=1),
3256+
simple=1)],
3257+
type_ignores=[
3258+
TypeIgnore(lineno=1, tag='[assignment]')])
32513259
'''
32523260
for flag in ('-m=exec', '--mode=exec'):
32533261
self.check_output(source, expect, flag)
3262+
3263+
def test_single_mode_flag(self):
3264+
# test 'python -m ast -m/--mode single'
32543265
source = 'pass'
32553266
expect = '''
32563267
Interactive(
@@ -3259,6 +3270,9 @@ def test_mode_flag(self):
32593270
'''
32603271
for flag in ('-m=single', '--mode=single'):
32613272
self.check_output(source, expect, flag)
3273+
3274+
def test_eval_mode_flag(self):
3275+
# test 'python -m ast -m/--mode eval'
32623276
source = 'print(1, 2, 3)'
32633277
expect = '''
32643278
Expression(
@@ -3271,6 +3285,9 @@ def test_mode_flag(self):
32713285
'''
32723286
for flag in ('-m=eval', '--mode=eval'):
32733287
self.check_output(source, expect, flag)
3288+
3289+
def test_func_type_mode_flag(self):
3290+
# test 'python -m ast -m/--mode func_type'
32743291
source = '(int, str) -> list[int]'
32753292
expect = '''
32763293
FunctionType(

0 commit comments

Comments
 (0)