@@ -106,6 +106,10 @@ def child_elements(self):
106106# `compile` builtin needs these attributes on AST nodes.
107107# It's hard to get something sensible we can put for line/col numbers so we put arbitrary values.
108108DEFAULT_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 ()
109113
110114
111115class Scope :
@@ -353,12 +357,12 @@ def __init__(self):
353357 Block .__init__ (self , scope )
354358
355359 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 )
357361
358362 def as_multiple_module_ast (self ):
359363 retval = []
360364 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 )
362366 if hasattr (item , "filename" ):
363367 # For use by compile_messages
364368 mod .filename = item .filename
@@ -388,6 +392,7 @@ def as_ast(self):
388392 for arg in self .args :
389393 if not allowable_name (arg ):
390394 raise AssertionError (f"Expected '{ arg } ' to be a valid Python identifier" )
395+
391396 func_def = ast .FunctionDef (
392397 name = self .func_name ,
393398 args = ast .arguments (
@@ -398,7 +403,7 @@ def as_ast(self):
398403 kw_defaults = [],
399404 kwarg = None ,
400405 defaults = [],
401- ** DEFAULT_AST_ARGS ,
406+ ** DEFAULT_AST_ARGS_ARGUMENTS ,
402407 ),
403408 body = self .body .as_ast_list (allow_empty = False ),
404409 decorator_list = [],
@@ -466,7 +471,7 @@ def finalize(self):
466471 def as_ast (self ):
467472 if len (self .if_blocks ) == 0 :
468473 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 )
470475 current_if = if_ast
471476 previous_if = None
472477 for condition , if_block in zip (self .conditions , self .if_blocks ):
@@ -476,7 +481,7 @@ def as_ast(self):
476481 previous_if .orelse .append (current_if )
477482
478483 previous_if = current_if
479- current_if = ast .If (orelse = [], ** DEFAULT_AST_ARGS )
484+ current_if = ast .If (test = None , orelse = [], ** DEFAULT_AST_ARGS )
480485
481486 if self .else_block .statements :
482487 previous_if .orelse = self .else_block .as_ast_list ()
@@ -651,7 +656,7 @@ def as_ast(self):
651656 right = part .as_ast ()
652657 left = ast .BinOp (
653658 left = left ,
654- op = ast .Add (** DEFAULT_AST_ARGS ),
659+ op = ast .Add (** DEFAULT_AST_ARGS_ADD ),
655660 right = right ,
656661 ** DEFAULT_AST_ARGS ,
657662 )
0 commit comments