diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 0a854142..1a864356 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -287,9 +287,14 @@ class Script(object): if user_stmt is None: # Set the start_pos to a pseudo position, that doesn't exist but works # perfectly well (for both completions in docstrings and statements). - stmt.start_pos = self._pos + pos = self._pos else: - stmt.start_pos = user_stmt.start_pos + pos = user_stmt.start_pos + + child = stmt + while hasattr(child, 'children'): + child = child.children[0] + child.start_pos = pos stmt.parent = self._parser.user_scope() return stmt diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index 3655ef47..4e8e2503 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -130,7 +130,7 @@ class Evaluator(object): if isinstance(stmt, FakeStatement): return expression_list # Already contains the results. - result = self.eval_expression_list(expression_list) + result = self.eval_element(stmt.children[0]) ass_details = stmt.assignment_details if ass_details and ass_details[0][1] != '=' and not isinstance(stmt, er.InstanceElement): # TODO don't check for this. @@ -161,6 +161,14 @@ class Evaluator(object): result = new_result return result + def eval_element(self, element): + if isinstance(element, pr.Name): + stmt = element.get_parent_until(pr.ExprStmt) + return self.find_types(stmt.parent, element, stmt.start_pos, + search_global=True) + else: + raise NotImplementedError + def eval_expression_list(self, expression_list): """ `expression_list` can be either `pr.Array` or `list of list`. diff --git a/jedi/parser/__init__.py b/jedi/parser/__init__.py index 909240b2..8c43e574 100644 --- a/jedi/parser/__init__.py +++ b/jedi/parser/__init__.py @@ -57,7 +57,8 @@ class Parser(object): # and only if the refactor method's write parameter was True. logger = logging.getLogger("RefactoringTool") d = Driver(pytree.python_grammar, convert=pytree.convert, logger=logger) - self.module = d.parse_string(source) + print(repr(source)) + self.module = d.parse_string(source).get_parent_until() def __init__old__(self, source, module_path=None, no_docstr=False, tokenizer=None, top_module=None): diff --git a/jedi/parser/representation.py b/jedi/parser/representation.py index 70580857..34ad14d8 100644 --- a/jedi/parser/representation.py +++ b/jedi/parser/representation.py @@ -175,6 +175,7 @@ class _Leaf(Base): self.value = value self.start_pos = start_pos self.prefix = prefix + self.parent = None @property def end_pos(self): @@ -262,9 +263,10 @@ class Simple(Base): """ Initialize :class:`Simple`. - :type children: :class:`SubModule` :param children: The module in which this Python object locates. """ + for c in children: + c.parent = self self.children = children self.parent = None @@ -319,14 +321,13 @@ class Scope(Simple, DocstringMixin): :param start_pos: The position (line and column) of the scope. :type start_pos: tuple(int, int) """ - __slots__ = ('subscopes', 'imports', 'statements', '_doc_token', 'asserts', + __slots__ = ('subscopes', 'imports', '_doc_token', 'asserts', 'returns', 'is_generator', '_names_dict') def __init__(self, children): super(Scope, self).__init__(children) self.subscopes = [] self.imports = [] - self.statements = [] self._doc_token = None self.asserts = [] # Needed here for fast_parser, because the fast_parser splits and @@ -335,6 +336,10 @@ class Scope(Simple, DocstringMixin): self._names_dict = defaultdict(_return_empty_list) self.is_generator = False + @property + def statements(self): + return [c for c in self.children if isinstance(c, ExprStmt)] + def is_scope(self): return True @@ -927,6 +932,7 @@ class Statement(Simple, DocstringMixin): self.expression_list() def get_defined_names(self): + return [] """Get the names for the statement.""" if self._set_vars is None: @@ -997,8 +1003,6 @@ class Statement(Simple, DocstringMixin): would result in ``[(Name(x), '='), (Array([Name(y), Name(z)]), '=')]``. """ - # parse statement which creates the assignment details. - self.expression_list() return self._assignment_details @cache.underscore_memoization