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