11import ast
2+ import copy
3+ from typing import cast
24
35from astunparse import unparse
6+
47from func_adl .ast .function_simplifier import FuncADLIndexError , simplify_chained_calls
58from tests .util_debug_ast import normalize_ast
69
@@ -18,10 +21,14 @@ def util_process(ast_in, ast_out):
1821 a_updated_raw = simplify_chained_calls ().visit (a_source )
1922
2023 s_updated = ast .dump (
21- normalize_ast ().visit (a_updated_raw ), annotate_fields = False , include_attributes = False
24+ normalize_ast ().visit (copy .deepcopy (a_updated_raw )),
25+ annotate_fields = False ,
26+ include_attributes = False ,
2227 )
2328 s_expected = ast .dump (
24- normalize_ast ().visit (a_expected ), annotate_fields = False , include_attributes = False
29+ normalize_ast ().visit (copy .deepcopy (a_expected )),
30+ annotate_fields = False ,
31+ include_attributes = False ,
2532 )
2633
2734 print (s_updated )
@@ -183,11 +190,27 @@ def test_tuple_select():
183190 util_process ("(t1,t2)[0]" , "t1" )
184191
185192
193+ def test_tuple_select_copy_made ():
194+ # (t1, t2)[0] should be t1.
195+ root = ast .parse ("(t1,t2)[0]" )
196+ orig = root .body [0 ].value .value .elts [0 ] # type: ignore
197+ r1 = util_process (root , "t1" )
198+ assert r1 .body [0 ].value != orig
199+
200+
186201def test_list_select ():
187202 # [t1, t2][0] should be t1.
188203 util_process ("[t1,t2][0]" , "t1" )
189204
190205
206+ def test_list_select_copy_made ():
207+ # [t1, t2][0] should be t1.
208+ root = ast .parse ("[t1,t2][0]" )
209+ orig = root .body [0 ].value .value .elts [0 ] # type: ignore
210+ r1 = util_process (root , "t1" )
211+ assert r1 .body [0 ].value != orig
212+
213+
191214def test_tuple_select_past_end ():
192215 # This should cause a crash!
193216 try :
@@ -260,3 +283,21 @@ def test_dict_around_first():
260283 'Select(events, lambda e: First(Select(e.jets, lambda j: {"j": j, "e": e})).j)' ,
261284 "Select(events, lambda e: First(e.jets))" ,
262285 )
286+
287+
288+ def test_dict_is_different_attrib ():
289+ "Make sure we are returning different ast's that have the same content"
290+ root = cast (ast .expr , ast .parse ('{"n1": t1, "n2": t2}.n1' ))
291+ orig_name = root .body [0 ].value .value .values [0 ] # type: ignore
292+ r1 = util_process (root , "t1" )
293+
294+ assert r1 .body [0 ].value != orig_name
295+
296+
297+ def test_dict_is_different ():
298+ "Make sure we are returning different ast's that have the same content"
299+ root = cast (ast .expr , ast .parse ('{"n1": t1, "n2": t2}["n1"]' ))
300+ orig_name = root .body [0 ].value .value .values [0 ] # type: ignore
301+ r1 = util_process (root , "t1" )
302+
303+ assert r1 .body [0 ].value != orig_name
0 commit comments