From 8847848a0351d0a64e8fd2ee962701e371b45543 Mon Sep 17 00:00:00 2001 From: jerluc Date: Tue, 16 Nov 2021 12:34:41 -0800 Subject: [PATCH 1/2] Adds support for "async with" via #1818 --- jedi/inference/syntax_tree.py | 7 +++++++ test/completion/async_.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/jedi/inference/syntax_tree.py b/jedi/inference/syntax_tree.py index 192328f3..f55ab56b 100644 --- a/jedi/inference/syntax_tree.py +++ b/jedi/inference/syntax_tree.py @@ -738,6 +738,13 @@ def tree_name_to_values(inference_state, context, tree_name): types = infer_expr_stmt(context, node, tree_name) elif typ == 'with_stmt': value_managers = context.infer_node(node.get_test_node_from_name(tree_name)) + if node.parent.type == 'async_stmt': + # In the case of `async with` statements, we need to + # first get the coroutine from the `__aenter__` method, + # then "unwrap" via the `__await__` method + enter_methods = value_managers.py__getattribute__('__aenter__') + coro = enter_methods.execute_with_values() + return coro.py__await__().py__stop_iteration_returns() enter_methods = value_managers.py__getattribute__('__enter__') return enter_methods.execute_with_values() elif typ in ('import_from', 'import_name'): diff --git a/test/completion/async_.py b/test/completion/async_.py index 2b6ad699..fa59ed3c 100644 --- a/test/completion/async_.py +++ b/test/completion/async_.py @@ -105,3 +105,22 @@ async def f(): f = await C().async_for_classmethod() #? C() f + + +class AsyncCtxMgr: + def some_method(): + pass + + async def __aenter__(self): + return self + + async def __aexit__(self, *args): + pass + + +async def asyncctxmgr(): + async with AsyncCtxMgr() as acm: + #? AsyncCtxMgr() + acm + #? ['some_method'] + acm.som From b2e647d5983c70cc2bf470fa9e0528a059001743 Mon Sep 17 00:00:00 2001 From: jerluc Date: Tue, 16 Nov 2021 16:12:43 -0800 Subject: [PATCH 2/2] Removing invalid test for `async with open(...)` See explanation in https://github.com/davidhalter/jedi/pull/1819#issuecomment-970776091 --- test/completion/async_.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/completion/async_.py b/test/completion/async_.py index fa59ed3c..86c5d71a 100644 --- a/test/completion/async_.py +++ b/test/completion/async_.py @@ -26,11 +26,6 @@ async def y(): x().__await__().__next return 2 -async def x2(): - async with open('asdf') as f: - #? ['readlines'] - f.readlines - class A(): @staticmethod async def b(c=1, d=2):