@@ -200,7 +200,8 @@ def f():
200200 y = [g for x in [1]]
201201 """
202202 outputs = {"y" : [2 ]}
203- self ._check_in_scopes (code , outputs )
203+ self ._check_in_scopes (code , outputs , scopes = ["module" , "function" ])
204+ self ._check_in_scopes (code , scopes = ["class" ], raises = NameError )
204205
205206 def test_inner_cell_shadows_outer_redefined (self ):
206207 code = """
@@ -328,7 +329,8 @@ def test_nested_2(self):
328329 y = [x for [x ** x for x in range(x)][x - 1] in l]
329330 """
330331 outputs = {"y" : [3 , 3 , 3 ]}
331- self ._check_in_scopes (code , outputs )
332+ self ._check_in_scopes (code , outputs , scopes = ["module" , "function" ])
333+ self ._check_in_scopes (code , scopes = ["class" ], raises = NameError )
332334
333335 def test_nested_3 (self ):
334336 code = """
@@ -379,6 +381,109 @@ def f():
379381 with self .assertRaises (UnboundLocalError ):
380382 f ()
381383
384+ def test_name_error_in_class_scope (self ):
385+ code = """
386+ y = 1
387+ [x + y for x in range(2)]
388+ """
389+ self ._check_in_scopes (code , raises = NameError , scopes = ["class" ])
390+
391+ def test_global_in_class_scope (self ):
392+ code = """
393+ y = 2
394+ vals = [(x, y) for x in range(2)]
395+ """
396+ outputs = {"vals" : [(0 , 1 ), (1 , 1 )]}
397+ self ._check_in_scopes (code , outputs , ns = {"y" : 1 }, scopes = ["class" ])
398+
399+ def test_in_class_scope_inside_function_1 (self ):
400+ code = """
401+ class C:
402+ y = 2
403+ vals = [(x, y) for x in range(2)]
404+ vals = C.vals
405+ """
406+ outputs = {"vals" : [(0 , 1 ), (1 , 1 )]}
407+ self ._check_in_scopes (code , outputs , ns = {"y" : 1 }, scopes = ["function" ])
408+
409+ def test_in_class_scope_inside_function_2 (self ):
410+ code = """
411+ y = 1
412+ class C:
413+ y = 2
414+ vals = [(x, y) for x in range(2)]
415+ vals = C.vals
416+ """
417+ outputs = {"vals" : [(0 , 1 ), (1 , 1 )]}
418+ self ._check_in_scopes (code , outputs , scopes = ["function" ])
419+
420+ def test_in_class_scope_with_global (self ):
421+ code = """
422+ y = 1
423+ class C:
424+ global y
425+ y = 2
426+ # Ensure the listcomp uses the global, not the value in the
427+ # class namespace
428+ locals()['y'] = 3
429+ vals = [(x, y) for x in range(2)]
430+ vals = C.vals
431+ """
432+ outputs = {"vals" : [(0 , 2 ), (1 , 2 )]}
433+ self ._check_in_scopes (code , outputs , scopes = ["module" , "class" ])
434+ outputs = {"vals" : [(0 , 1 ), (1 , 1 )]}
435+ self ._check_in_scopes (code , outputs , scopes = ["function" ])
436+
437+ def test_in_class_scope_with_nonlocal (self ):
438+ code = """
439+ y = 1
440+ class C:
441+ nonlocal y
442+ y = 2
443+ # Ensure the listcomp uses the global, not the value in the
444+ # class namespace
445+ locals()['y'] = 3
446+ vals = [(x, y) for x in range(2)]
447+ vals = C.vals
448+ """
449+ outputs = {"vals" : [(0 , 2 ), (1 , 2 )]}
450+ self ._check_in_scopes (code , outputs , scopes = ["function" ])
451+
452+ def test_nested_has_free_var (self ):
453+ code = """
454+ items = [a for a in [1] if [a for _ in [0]]]
455+ """
456+ outputs = {"items" : [1 ]}
457+ self ._check_in_scopes (code , outputs , scopes = ["class" ])
458+
459+ def test_nested_free_var_not_bound_in_outer_comp (self ):
460+ code = """
461+ z = 1
462+ items = [a for a in [1] if [x for x in [1] if z]]
463+ """
464+ self ._check_in_scopes (code , {"items" : [1 ]}, scopes = ["module" , "function" ])
465+ self ._check_in_scopes (code , {"items" : []}, ns = {"z" : 0 }, scopes = ["class" ])
466+
467+ def test_nested_free_var_in_iter (self ):
468+ code = """
469+ items = [_C for _C in [1] for [0, 1][[x for x in [1] if _C][0]] in [2]]
470+ """
471+ self ._check_in_scopes (code , {"items" : [1 ]})
472+
473+ def test_nested_free_var_in_expr (self ):
474+ code = """
475+ items = [(_C, [x for x in [1] if _C]) for _C in [0, 1]]
476+ """
477+ self ._check_in_scopes (code , {"items" : [(0 , []), (1 , [1 ])]})
478+
479+ def test_nested_listcomp_in_lambda (self ):
480+ code = """
481+ f = [(z, lambda y: [(x, y, z) for x in [3]]) for z in [1]]
482+ (z, func), = f
483+ out = func(2)
484+ """
485+ self ._check_in_scopes (code , {"z" : 1 , "out" : [(3 , 2 , 1 )]})
486+
382487
383488__test__ = {'doctests' : doctests }
384489
0 commit comments