gh-133367: Add missing options to `ast` CLI (#133369) · python/cpython@2b4e2b7

GitHub

@@ -3272,6 +3272,9 @@ def test_invocation(self):

32723272 ('--no-type-comments', '--no-type-comments'),

32733273 ('-a', '--include-attributes'),

32743274 ('-i=4', '--indent=4'),

3275+ ('--feature-version=3.13', '--feature-version=3.13'),

3276+ ('-O=-1', '--optimize=-1'),

3277+ ('--show-empty', '--show-empty'),

32753278 )

32763279self.set_source('''

32773280 print(1, 2, 3)

@@ -3389,7 +3392,7 @@ def test_include_attributes_flag(self):

33893392self.check_output(source, expect, flag)

3390339333913394deftest_indent_flag(self):

3392-# test 'python -m ast -i/--indent'

3395+# test 'python -m ast -i/--indent 0'

33933396source='pass'

33943397expect='''

33953398 Module(

@@ -3400,6 +3403,96 @@ def test_indent_flag(self):

34003403withself.subTest(flag=flag):

34013404self.check_output(source, expect, flag)

340234053406+deftest_feature_version_flag(self):

3407+# test 'python -m ast --feature-version 3.9/3.10'

3408+source='''

3409+ match x:

3410+ case 1:

3411+ pass

3412+ '''

3413+expect='''

3414+ Module(

3415+ body=[

3416+ Match(

3417+ subject=Name(id='x', ctx=Load()),

3418+ cases=[

3419+ match_case(

3420+ pattern=MatchValue(

3421+ value=Constant(value=1)),

3422+ body=[

3423+ Pass()])])])

3424+ '''

3425+self.check_output(source, expect, '--feature-version=3.10')

3426+withself.assertRaises(SyntaxError):

3427+self.invoke_ast('--feature-version=3.9')

3428+3429+deftest_no_optimize_flag(self):

3430+# test 'python -m ast -O/--optimize -1/0'

3431+source='''

3432+ match a:

3433+ case 1+2j:

3434+ pass

3435+ '''

3436+expect='''

3437+ Module(

3438+ body=[

3439+ Match(

3440+ subject=Name(id='a', ctx=Load()),

3441+ cases=[

3442+ match_case(

3443+ pattern=MatchValue(

3444+ value=BinOp(

3445+ left=Constant(value=1),

3446+ op=Add(),

3447+ right=Constant(value=2j))),

3448+ body=[

3449+ Pass()])])])

3450+ '''

3451+forflagin ('-O=-1', '--optimize=-1', '-O=0', '--optimize=0'):

3452+withself.subTest(flag=flag):

3453+self.check_output(source, expect, flag)

3454+3455+deftest_optimize_flag(self):

3456+# test 'python -m ast -O/--optimize 1/2'

3457+source='''

3458+ match a:

3459+ case 1+2j:

3460+ pass

3461+ '''

3462+expect='''

3463+ Module(

3464+ body=[

3465+ Match(

3466+ subject=Name(id='a', ctx=Load()),

3467+ cases=[

3468+ match_case(

3469+ pattern=MatchValue(

3470+ value=Constant(value=(1+2j))),

3471+ body=[

3472+ Pass()])])])

3473+ '''

3474+forflagin ('-O=1', '--optimize=1', '-O=2', '--optimize=2'):

3475+withself.subTest(flag=flag):

3476+self.check_output(source, expect, flag)

3477+3478+deftest_show_empty_flag(self):

3479+# test 'python -m ast --show-empty'

3480+source='print(1, 2, 3)'

3481+expect='''

3482+ Module(

3483+ body=[

3484+ Expr(

3485+ value=Call(

3486+ func=Name(id='print', ctx=Load()),

3487+ args=[

3488+ Constant(value=1),

3489+ Constant(value=2),

3490+ Constant(value=3)],

3491+ keywords=[]))],

3492+ type_ignores=[])

3493+ '''

3494+self.check_output(source, expect, '--show-empty')

3495+3403349634043497classASTOptimiziationTests(unittest.TestCase):

34053498defwrap_expr(self, expr):