diff --git a/jedi/api.py b/jedi/api.py index 7ff6c020..c493e679 100644 --- a/jedi/api.py +++ b/jedi/api.py @@ -327,7 +327,7 @@ class Script(object): and self.pos < user_stmt.get_assignment_calls().start_pos: # the search_name might be before `=` definitions = [v for v in user_stmt.set_vars - if unicode(v) == search_name] + if unicode(v.names[-1]) == search_name] if not isinstance(user_stmt, parsing.Import): # import case is looked at with add_import_name option definitions = dynamic.related_name_add_import_modules(definitions, @@ -341,7 +341,7 @@ class Script(object): if isinstance(d, parsing.Module): names.append(api_classes.RelatedName(d, d)) else: - names.append(api_classes.RelatedName(d.names[0], d)) + names.append(api_classes.RelatedName(d.names[-1], d)) return sorted(set(names), key=lambda x: (x.module_path, x.start_pos), reverse=True) diff --git a/jedi/dynamic.py b/jedi/dynamic.py index 16635779..78d74bb7 100644 --- a/jedi/dynamic.py +++ b/jedi/dynamic.py @@ -349,12 +349,14 @@ class ArrayInstance(parsing.Base): def related_names(definitions, search_name, mods): - def strip_ambiguity(definitions): + def compare_array(definitions): + """ `definitions` are being compared by module/start_pos, because + sometimes the id's of the objects change (e.g. executions). + """ result = [] for d in definitions: - if isinstance(d, evaluate.InstanceElement): - d = d.var - result.append(d) + module = d.get_parent_until() + result.append((module, d.start_pos)) return result def check_call(call): @@ -369,11 +371,10 @@ def related_names(definitions, search_name, mods): for f in follow: follow_res, search = evaluate.goto(call.parent_stmt, f) follow_res = related_name_add_import_modules(follow_res, search) - follow_res = strip_ambiguity(follow_res) - #print follow_res, [d.parent for d in follow_res] + compare_follow_res = compare_array(follow_res) # compare to see if they match - if any(r in definitions for r in follow_res): + if any(r in compare_definitions for r in compare_follow_res): scope = call.parent_stmt result.append(api_classes.RelatedName(search, scope)) @@ -395,7 +396,7 @@ def related_names(definitions, search_name, mods): except AssertionError: return False - definitions = strip_ambiguity(definitions) + compare_definitions = compare_array(definitions) mods |= set([d.get_parent_until() for d in definitions]) names = [] for m in get_directory_modules_for_name(mods, search_name): diff --git a/test/completion/renaming.py b/test/completion/renaming.py index 05363e00..09e4c342 100644 --- a/test/completion/renaming.py +++ b/test/completion/renaming.py @@ -113,3 +113,58 @@ class TestClassVar(object): TestClassVar.class_v #< (0,8), (-7, 8) class_v + +class TestInstanceVar(): + def a(self): + #< 13 (4,13), (0,13) + self._instance_var = 3 + + def b(self): + #< (-4,13), (0,13) + self._instance_var + + +# ----------------- +# inheritance +# ----------------- +class Super(object): + #< 4 (0,4), (23,18), (25,13) + base_class = 1 + #< 4 (0,4), + class_var = 1 + + #< 8 (0,8), + def base_method(self): + #< 13 (0,13), (20,13) + self.base_var = 1 + #< 13 (0,13), (29,13) + self.instance_var = 1 + + #< 8 (0,8), + def just_a_method(self): pass + + +#< 20 (0,16), (-18,6) +class TestClass(Super): + #< 4 (0,4), + class_var = 1 + + def x_method(self): + + #< (0,18), (2,13), (-23,4) + TestClass.base_class + #< (-2,18), (0,13), (-25,4) + self.base_class + #< (-20,13), (0,13) + self.base_var + #< + TestClass.base_var + + + #< 13 (5,13), (0,13) + self.instance_var = 3 + + #< 9 (0,8), + def just_a_method(self): + #< (-5,13), (0,13), (-29,13) + self.instance_var diff --git a/test/run.py b/test/run.py index 8bc3726c..9ff9be74 100755 --- a/test/run.py +++ b/test/run.py @@ -88,7 +88,11 @@ def run_related_name_test(script, correct, line_nr): compare = sorted((r.module_name, r.start_pos[0], r.start_pos[1]) for r in result) wanted = [] - for pos_tup in literal_eval(correct): + if not correct: + positions = [] + else: + positions = literal_eval(correct) + for pos_tup in positions: if type(pos_tup[0]) == str: # this means that there is a module specified wanted.append(pos_tup)