From 1e12e1e318ada17b27cf726734e257339d6d649a Mon Sep 17 00:00:00 2001 From: forest93 Date: Thu, 11 Apr 2019 23:38:55 +0800 Subject: [PATCH] Make get_module_names return top-level async functions when all_scopes=False. --- jedi/evaluate/helpers.py | 3 +++ test/test_api/test_defined_names.py | 24 ++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/jedi/evaluate/helpers.py b/jedi/evaluate/helpers.py index 1b0a7f2a..c087e2fb 100644 --- a/jedi/evaluate/helpers.py +++ b/jedi/evaluate/helpers.py @@ -190,6 +190,9 @@ def get_module_names(module, all_scopes): # but that would be a big change that could break type inference, whereas for now # this discrepancy looks like only a problem for "get_module_names". parent_scope = parent_scope.parent + # async functions have an extra wrapper. Strip it. + if parent_scope and parent_scope.type == 'async_stmt': + parent_scope = parent_scope.parent return parent_scope in (module, None) names = [n for n in names if is_module_scope_name(n)] diff --git a/test/test_api/test_defined_names.py b/test/test_api/test_defined_names.py index 31814e0f..5c8bf05f 100644 --- a/test/test_api/test_defined_names.py +++ b/test/test_api/test_defined_names.py @@ -82,18 +82,34 @@ class TestDefinedNames(TestCase): def test_class_fields_with_all_scopes_false(self): definitions = self.check_defined_names(""" from module import f + import asyncio + g = f(f) class C: h = g + def __init__(self): + pass + + async def __aenter__(self): + pass def foo(x=a): bar = x return bar - """, ['f', 'g', 'C', 'foo']) - C_subdefs = definitions[-2].defined_names() - foo_subdefs = definitions[-1].defined_names() - self.assert_definition_names(C_subdefs, ['h']) + + async def async_foo(duration): + async def wait(): + await asyncio.sleep(100) + for i in range(duration//100): + await wait() + return duration//100*100 + """, ['f', 'asyncio', 'g', 'C', 'foo', 'async_foo']) + C_subdefs = definitions[-3].defined_names() + foo_subdefs = definitions[-2].defined_names() + async_foo_subdefs = definitions[-1].defined_names() + self.assert_definition_names(C_subdefs, ['h', '__init__', '__aenter__']) self.assert_definition_names(foo_subdefs, ['x', 'bar']) + self.assert_definition_names(async_foo_subdefs, ['duration', 'wait', 'i']) def test_follow_imports(environment):