mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 06:44:46 +08:00
Merge pull request #1819 from jerluc/master
Adds support for "async with" via #1818
This commit is contained in:
@@ -738,6 +738,13 @@ def tree_name_to_values(inference_state, context, tree_name):
|
|||||||
types = infer_expr_stmt(context, node, tree_name)
|
types = infer_expr_stmt(context, node, tree_name)
|
||||||
elif typ == 'with_stmt':
|
elif typ == 'with_stmt':
|
||||||
value_managers = context.infer_node(node.get_test_node_from_name(tree_name))
|
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__')
|
enter_methods = value_managers.py__getattribute__('__enter__')
|
||||||
return enter_methods.execute_with_values()
|
return enter_methods.execute_with_values()
|
||||||
elif typ in ('import_from', 'import_name'):
|
elif typ in ('import_from', 'import_name'):
|
||||||
|
|||||||
@@ -26,11 +26,6 @@ async def y():
|
|||||||
x().__await__().__next
|
x().__await__().__next
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
async def x2():
|
|
||||||
async with open('asdf') as f:
|
|
||||||
#? ['readlines']
|
|
||||||
f.readlines
|
|
||||||
|
|
||||||
class A():
|
class A():
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def b(c=1, d=2):
|
async def b(c=1, d=2):
|
||||||
@@ -105,3 +100,22 @@ async def f():
|
|||||||
f = await C().async_for_classmethod()
|
f = await C().async_for_classmethod()
|
||||||
#? C()
|
#? C()
|
||||||
f
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user