diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-07-17 13:54:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-17 13:54:40 (GMT) |
commit | c6ef5aca614e3ee1cfe4125148d80e0e0bd77462 (patch) | |
tree | da9994575e53952e09cc2ade190c8e62b8940a43 /Lib/test/test_symtable.py | |
parent | 4395d68c7017eacf0ad643befe1f1e0bc6149f26 (diff) | |
download | cpython-c6ef5aca614e3ee1cfe4125148d80e0e0bd77462.zip cpython-c6ef5aca614e3ee1cfe4125148d80e0e0bd77462.tar.gz cpython-c6ef5aca614e3ee1cfe4125148d80e0e0bd77462.tar.bz2 |
[3.13] gh-119698: fix a special case in `symtable.Class.get_methods` (GH-121802) (#121909)
(cherry picked from commit 6682d916780c1cb305e679a057ee6992b114118e)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Diffstat (limited to 'Lib/test/test_symtable.py')
-rw-r--r-- | Lib/test/test_symtable.py | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py index 2443898..c1b7030 100644 --- a/Lib/test/test_symtable.py +++ b/Lib/test/test_symtable.py @@ -1,6 +1,8 @@ """ Test the API of the symtable module. """ + +import textwrap import symtable import unittest @@ -354,7 +356,7 @@ class SymtableTest(unittest.TestCase): self.assertEqual(self.spam.lookup("x").get_name(), "x") self.assertEqual(self.Mine.get_name(), "Mine") - def test_class_info(self): + def test_class_get_methods(self): self.assertEqual(self.Mine.get_methods(), ('a_method',)) top = symtable.symtable(TEST_COMPLEX_CLASS_CODE, "?", "exec") @@ -375,6 +377,58 @@ class SymtableTest(unittest.TestCase): 'glob_assigned_async_meth', 'glob_assigned_async_meth_pep_695', )) + # Test generator expressions that are of type TYPE_FUNCTION + # but will not be reported by get_methods() since they are + # not functions per se. + # + # Other kind of comprehensions such as list, set or dict + # expressions do not have the TYPE_FUNCTION type. + + def check_body(body, expected_methods): + indented = textwrap.indent(body, ' ' * 4) + top = symtable.symtable(f"class A:\n{indented}", "?", "exec") + this = find_block(top, "A") + self.assertEqual(this.get_methods(), expected_methods) + + # statements with 'genexpr' inside it + GENEXPRS = ( + 'x = (x for x in [])', + 'x = (x async for x in [])', + 'type x[genexpr = (x for x in [])] = (x for x in [])', + 'type x[genexpr = (x async for x in [])] = (x async for x in [])', + 'genexpr = (x for x in [])', + 'genexpr = (x async for x in [])', + 'type genexpr[genexpr = (x for x in [])] = (x for x in [])', + 'type genexpr[genexpr = (x async for x in [])] = (x async for x in [])', + ) + + for gen in GENEXPRS: + # test generator expression + with self.subTest(gen=gen): + check_body(gen, ()) + + # test generator expression + variable named 'genexpr' + with self.subTest(gen=gen, isvar=True): + check_body('\n'.join((gen, 'genexpr = 1')), ()) + check_body('\n'.join(('genexpr = 1', gen)), ()) + + for paramlist in ('()', '(x)', '(x, y)', '(z: T)'): + for func in ( + f'def genexpr{paramlist}:pass', + f'async def genexpr{paramlist}:pass', + f'def genexpr[T]{paramlist}:pass', + f'async def genexpr[T]{paramlist}:pass', + ): + with self.subTest(func=func): + # test function named 'genexpr' + check_body(func, ('genexpr',)) + + for gen in GENEXPRS: + with self.subTest(gen=gen, func=func): + # test generator expression + function named 'genexpr' + check_body('\n'.join((gen, func)), ('genexpr',)) + check_body('\n'.join((func, gen)), ('genexpr',)) + def test_filename_correct(self): ### Bug tickler: SyntaxError file name correct whether error raised ### while parsing or building symbol table. |