1
1
"""
2
2
Test the API of the symtable module.
3
3
"""
4
+
5
+ import textwrap
4
6
import symtable
5
7
import unittest
6
8
@@ -356,7 +358,7 @@ def test_name(self):
356
358
self .assertEqual (self .spam .lookup ("x" ).get_name (), "x" )
357
359
self .assertEqual (self .Mine .get_name (), "Mine" )
358
360
359
- def test_class_info (self ):
361
+ def test_class_get_methods (self ):
360
362
self .assertEqual (self .Mine .get_methods (), ('a_method' ,))
361
363
362
364
top = symtable .symtable (TEST_COMPLEX_CLASS_CODE , "?" , "exec" )
@@ -377,6 +379,58 @@ def test_class_info(self):
377
379
'glob_assigned_async_meth' , 'glob_assigned_async_meth_pep_695' ,
378
380
))
379
381
382
+ # Test generator expressions that are of type TYPE_FUNCTION
383
+ # but will not be reported by get_methods() since they are
384
+ # not functions per se.
385
+ #
386
+ # Other kind of comprehensions such as list, set or dict
387
+ # expressions do not have the TYPE_FUNCTION type.
388
+
389
+ def check_body (body , expected_methods ):
390
+ indented = textwrap .indent (body , ' ' * 4 )
391
+ top = symtable .symtable (f"class A:\n { indented } " , "?" , "exec" )
392
+ this = find_block (top , "A" )
393
+ self .assertEqual (this .get_methods (), expected_methods )
394
+
395
+ # statements with 'genexpr' inside it
396
+ GENEXPRS = (
397
+ 'x = (x for x in [])' ,
398
+ 'x = (x async for x in [])' ,
399
+ 'type x[genexpr = (x for x in [])] = (x for x in [])' ,
400
+ 'type x[genexpr = (x async for x in [])] = (x async for x in [])' ,
401
+ 'genexpr = (x for x in [])' ,
402
+ 'genexpr = (x async for x in [])' ,
403
+ 'type genexpr[genexpr = (x for x in [])] = (x for x in [])' ,
404
+ 'type genexpr[genexpr = (x async for x in [])] = (x async for x in [])' ,
405
+ )
406
+
407
+ for gen in GENEXPRS :
408
+ # test generator expression
409
+ with self .subTest (gen = gen ):
410
+ check_body (gen , ())
411
+
412
+ # test generator expression + variable named 'genexpr'
413
+ with self .subTest (gen = gen , isvar = True ):
414
+ check_body ('\n ' .join ((gen , 'genexpr = 1' )), ())
415
+ check_body ('\n ' .join (('genexpr = 1' , gen )), ())
416
+
417
+ for paramlist in ('()' , '(x)' , '(x, y)' , '(z: T)' ):
418
+ for func in (
419
+ f'def genexpr{ paramlist } :pass' ,
420
+ f'async def genexpr{ paramlist } :pass' ,
421
+ f'def genexpr[T]{ paramlist } :pass' ,
422
+ f'async def genexpr[T]{ paramlist } :pass' ,
423
+ ):
424
+ with self .subTest (func = func ):
425
+ # test function named 'genexpr'
426
+ check_body (func , ('genexpr' ,))
427
+
428
+ for gen in GENEXPRS :
429
+ with self .subTest (gen = gen , func = func ):
430
+ # test generator expression + function named 'genexpr'
431
+ check_body ('\n ' .join ((gen , func )), ('genexpr' ,))
432
+ check_body ('\n ' .join ((func , gen )), ('genexpr' ,))
433
+
380
434
def test_filename_correct (self ):
381
435
### Bug tickler: SyntaxError file name correct whether error raised
382
436
### while parsing or building symbol table.
0 commit comments