@@ -106,6 +106,10 @@ def child_elements(self):
106
106
# `compile` builtin needs these attributes on AST nodes.
107
107
# It's hard to get something sensible we can put for line/col numbers so we put arbitrary values.
108
108
DEFAULT_AST_ARGS = dict (lineno = 1 , col_offset = 1 )
109
+ # Some AST types have different requirements:
110
+ DEFAULT_AST_ARGS_MODULE = dict ()
111
+ DEFAULT_AST_ARGS_ADD = dict ()
112
+ DEFAULT_AST_ARGS_ARGUMENTS = dict ()
109
113
110
114
111
115
class Scope :
@@ -353,12 +357,12 @@ def __init__(self):
353
357
Block .__init__ (self , scope )
354
358
355
359
def as_ast (self ):
356
- return ast .Module (body = self .as_ast_list (), type_ignores = [], ** DEFAULT_AST_ARGS )
360
+ return ast .Module (body = self .as_ast_list (), type_ignores = [], ** DEFAULT_AST_ARGS_MODULE )
357
361
358
362
def as_multiple_module_ast (self ):
359
363
retval = []
360
364
for item in self .as_ast_list ():
361
- mod = ast .Module (body = [item ], type_ignores = [], ** DEFAULT_AST_ARGS )
365
+ mod = ast .Module (body = [item ], type_ignores = [], ** DEFAULT_AST_ARGS_MODULE )
362
366
if hasattr (item , "filename" ):
363
367
# For use by compile_messages
364
368
mod .filename = item .filename
@@ -388,6 +392,7 @@ def as_ast(self):
388
392
for arg in self .args :
389
393
if not allowable_name (arg ):
390
394
raise AssertionError (f"Expected '{ arg } ' to be a valid Python identifier" )
395
+
391
396
func_def = ast .FunctionDef (
392
397
name = self .func_name ,
393
398
args = ast .arguments (
@@ -398,7 +403,7 @@ def as_ast(self):
398
403
kw_defaults = [],
399
404
kwarg = None ,
400
405
defaults = [],
401
- ** DEFAULT_AST_ARGS ,
406
+ ** DEFAULT_AST_ARGS_ARGUMENTS ,
402
407
),
403
408
body = self .body .as_ast_list (allow_empty = False ),
404
409
decorator_list = [],
@@ -466,7 +471,7 @@ def finalize(self):
466
471
def as_ast (self ):
467
472
if len (self .if_blocks ) == 0 :
468
473
raise AssertionError ("Should have called `finalize` on If" )
469
- if_ast = ast .If (orelse = [], ** DEFAULT_AST_ARGS )
474
+ if_ast = ast .If (test = None , orelse = [], ** DEFAULT_AST_ARGS )
470
475
current_if = if_ast
471
476
previous_if = None
472
477
for condition , if_block in zip (self .conditions , self .if_blocks ):
@@ -476,7 +481,7 @@ def as_ast(self):
476
481
previous_if .orelse .append (current_if )
477
482
478
483
previous_if = current_if
479
- current_if = ast .If (orelse = [], ** DEFAULT_AST_ARGS )
484
+ current_if = ast .If (test = None , orelse = [], ** DEFAULT_AST_ARGS )
480
485
481
486
if self .else_block .statements :
482
487
previous_if .orelse = self .else_block .as_ast_list ()
@@ -651,7 +656,7 @@ def as_ast(self):
651
656
right = part .as_ast ()
652
657
left = ast .BinOp (
653
658
left = left ,
654
- op = ast .Add (** DEFAULT_AST_ARGS ),
659
+ op = ast .Add (** DEFAULT_AST_ARGS_ADD ),
655
660
right = right ,
656
661
** DEFAULT_AST_ARGS ,
657
662
)
0 commit comments