1
0
forked from VimPlug/jedi

Understand context managers correctly

Fixes #812.
This commit is contained in:
Dave Halter
2017-09-13 11:00:10 +02:00
parent 5ff7e3dbbe
commit 4c2d1ea7e7
4 changed files with 21 additions and 1 deletions

View File

@@ -39,6 +39,9 @@ class file():
def readlines(self):
return ['']
def __enter__(self):
return self
class xrange():
# Attention: this function doesn't exist in Py3k (there it is range).

View File

@@ -7,3 +7,6 @@ class TextIOWrapper():
def readlines(self):
return ['']
def __enter__(self):
return self

View File

@@ -221,7 +221,12 @@ def _name_to_types(evaluator, context, tree_name):
elif typ == 'expr_stmt':
types = _remove_statements(evaluator, context, node, tree_name)
elif typ == 'with_stmt':
types = context.eval_node(node.get_test_node_from_name(tree_name))
context_managers = context.eval_node(node.get_test_node_from_name(tree_name))
enter_methods = unite(
context_manager.py__getattribute__('__enter__')
for context_manager in context_managers
)
types = unite(method.execute_evaluated() for method in enter_methods)
elif typ in ('import_from', 'import_name'):
types = imports.infer_import(context, tree_name)
elif typ in ('funcdef', 'classdef'):

View File

@@ -227,3 +227,12 @@ z = zipfile.ZipFile("foo")
# It's too slow. So we don't run it at the moment.
##? ['upper']
z.read('name').upper
# -----------------
# contextlib
# -----------------
import contextlib
with contextlib.closing('asd') as string:
#? str()
string