diff --git a/jedi/evaluate/finder.py b/jedi/evaluate/finder.py index c60ecd49..ad2ff36c 100644 --- a/jedi/evaluate/finder.py +++ b/jedi/evaluate/finder.py @@ -299,6 +299,8 @@ class NameFinder(object): types += self._eval_param(typ) elif typ.isinstance(pr.ExprStmt): types += self._remove_statements(typ, name) + elif typ.isinstance(pr.WithStmt): + types += evaluator.eval_element(typ.node_from_name(name)) elif isinstance(typ, pr.Import): types += imports.ImportWrapper(self._evaluator, name).follow() elif isinstance(typ, pr.TryStmt): diff --git a/jedi/parser/representation.py b/jedi/parser/representation.py index d9d4d97d..7470c278 100644 --- a/jedi/parser/representation.py +++ b/jedi/parser/representation.py @@ -282,7 +282,7 @@ class Name(Leaf): elif isinstance(stmt, TryStmt): return self.prev_sibling() == 'as' else: - return isinstance(stmt, (ExprStmt, Import, CompFor)) \ + return isinstance(stmt, (ExprStmt, Import, CompFor, WithStmt)) \ and self in stmt.get_defined_names() def assignment_indexes(self): @@ -918,7 +918,20 @@ class TryStmt(Flow): class WithStmt(Flow): - pass + def get_defined_names(self): + names = [] + for with_item in self.children[1:-2:2]: + # Check with items for 'as' names. + if is_node(with_item, 'with_item'): + names += _defined_names(with_item.children[2]) + return names + + def node_from_name(self, name): + node = name + while True: + node = node.parent + if is_node(node, 'with_item'): + return node.children[0] class Flow_old(Scope):