@@ -131,7 +131,7 @@ def test_snippets(self):
131
131
(eval_tests ,eval_results ,"eval" )):
132
132
for i ,o in zip (input ,output ):
133
133
with self .subTest (action = "parsing" ,input = i ):
134
- ast_tree = compile (i ,"?" ,kind ,ast .PyCF_ONLY_AST )
134
+ ast_tree = compile (i ,"?" ,kind ,ast .PyCF_ONLY_AST , optimize = False )
135
135
self .assertEqual (to_tuple (ast_tree ),o )
136
136
self ._assertTrueorder (ast_tree , (0 ,0 ))
137
137
with self .subTest (action = "compiling" ,input = i ,kind = kind ):
@@ -141,7 +141,7 @@ def test_ast_validation(self):
141
141
# compile() is the only function that calls PyAST_Validate
142
142
snippets_to_validate = exec_tests + single_tests + eval_tests
143
143
for snippet in snippets_to_validate :
144
- tree = ast .parse (snippet )
144
+ tree = ast .parse (snippet , optimize = False )
145
145
compile (tree ,'<string>' ,'exec' )
146
146
147
147
def test_parse_invalid_ast (self ):
@@ -923,7 +923,7 @@ def test_repr(self) -> None:
923
923
snapshots = AST_REPR_DATA_FILE .read_text ().split ("\n " )
924
924
for test ,snapshot in zip (ast_repr_get_test_cases (),snapshots ,strict = True ):
925
925
with self .subTest (test_input = test ):
926
- self .assertEqual (repr (ast .parse (test )),snapshot )
926
+ self .assertEqual (repr (ast .parse (test , optimize = False )),snapshot )
927
927
928
928
def test_repr_large_input_crash (self ):
929
929
# gh-125010: Fix use-after-free in ast repr()
@@ -1698,22 +1698,22 @@ def test_iter_child_nodes(self):
1698
1698
)
1699
1699
1700
1700
def test_get_docstring (self ):
1701
- node = ast .parse ('"""line one\n line two"""' )
1701
+ node = ast .parse ('"""line one\n line two"""' , optimize = False )
1702
1702
self .assertEqual (ast .get_docstring (node ),
1703
1703
'line one\n line two' )
1704
1704
1705
- node = ast .parse ('class foo:\n """line one\n line two"""' )
1705
+ node = ast .parse ('class foo:\n """line one\n line two"""' , optimize = False )
1706
1706
self .assertEqual (ast .get_docstring (node .body [0 ]),
1707
1707
'line one\n line two' )
1708
1708
1709
- node = ast .parse ('def foo():\n """line one\n line two"""' )
1709
+ node = ast .parse ('def foo():\n """line one\n line two"""' , optimize = False )
1710
1710
self .assertEqual (ast .get_docstring (node .body [0 ]),
1711
1711
'line one\n line two' )
1712
1712
1713
- node = ast .parse ('async def foo():\n """spam\n ham"""' )
1713
+ node = ast .parse ('async def foo():\n """spam\n ham"""' , optimize = False )
1714
1714
self .assertEqual (ast .get_docstring (node .body [0 ]),'spam\n ham' )
1715
1715
1716
- node = ast .parse ('async def foo():\n """spam\n ham"""' )
1716
+ node = ast .parse ('async def foo():\n """spam\n ham"""' , optimize = False )
1717
1717
self .assertEqual (ast .get_docstring (node .body [0 ],clean = False ),'spam\n ham' )
1718
1718
1719
1719
node = ast .parse ('x' )
@@ -1752,7 +1752,8 @@ def test_multi_line_docstring_col_offset_and_lineno_issue16806(self):
1752
1752
'def foo():\n """line one\n line two"""\n \n '
1753
1753
' def bar():\n """line one\n line two"""\n '
1754
1754
' """line one\n line two"""\n '
1755
- '"""line one\n line two"""\n \n '
1755
+ '"""line one\n line two"""\n \n ' ,
1756
+ optimize = False
1756
1757
)
1757
1758
self .assertEqual (node .body [0 ].col_offset ,0 )
1758
1759
self .assertEqual (node .body [0 ].lineno ,1 )
@@ -2321,9 +2322,9 @@ def test_stdlib_validates(self):
2321
2322
fn = os .path .join (STDLIB ,module )
2322
2323
with open (fn ,"r" ,encoding = "utf-8" )as fp :
2323
2324
source = fp .read ()
2324
- mod = ast .parse (source ,fn )
2325
+ mod = ast .parse (source ,fn , optimize = False )
2325
2326
compile (mod ,fn ,"exec" )
2326
- mod2 = ast .parse (source ,fn )
2327
+ mod2 = ast .parse (source ,fn , optimize = False )
2327
2328
self .assertTrue (ast .compare (mod ,mod2 ))
2328
2329
2329
2330
constant_1 = ast .Constant (1 )
@@ -2537,7 +2538,7 @@ def test_assign_to_constant(self):
2537
2538
"to in Store context" )
2538
2539
2539
2540
def test_get_docstring (self ):
2540
- tree = ast .parse ("'docstring'\n x = 1" )
2541
+ tree = ast .parse ("'docstring'\n x = 1" , optimize = False )
2541
2542
self .assertEqual (ast .get_docstring (tree ),'docstring' )
2542
2543
2543
2544
def get_load_const (self ,tree ):