Fix issues where references were identified as definitions

This commit is contained in:
Dave Halter
2020-03-06 14:23:12 +01:00
parent 6e3bd38600
commit c7a862ec19
3 changed files with 24 additions and 8 deletions

View File

@@ -550,17 +550,18 @@ class Script(object):
return parso_to_jedi_errors(self._inference_state.grammar, self._module_node) return parso_to_jedi_errors(self._inference_state.grammar, self._module_node)
def _names(self, all_scopes=False, definitions=True, references=False): def _names(self, all_scopes=False, definitions=True, references=False):
def def_ref_filter(name):
is_def = name.tree_name.is_definition()
return definitions and is_def or references and not is_def
# Set line/column to a random position, because they don't matter. # Set line/column to a random position, because they don't matter.
module_context = self._get_module_context() module_context = self._get_module_context()
defs = [ defs = [
module_context.create_name(name) module_context.create_name(name)
for name in get_module_names(self._module_node, all_scopes) for name in get_module_names(
self._module_node,
all_scopes=all_scopes,
definitions=definitions,
references=references,
)
] ]
return sorted(filter(def_ref_filter, defs), key=lambda x: x.start_pos) return sorted(defs, key=lambda x: x.start_pos)
@no_py2_support @no_py2_support
def rename(self, line=None, column=None, **kwargs): def rename(self, line=None, column=None, **kwargs):

View File

@@ -122,11 +122,15 @@ def get_names_of_node(node):
return list(chain.from_iterable(get_names_of_node(c) for c in children)) return list(chain.from_iterable(get_names_of_node(c) for c in children))
def get_module_names(module, all_scopes): def get_module_names(module, all_scopes, definitions=True, references=False):
""" """
Returns a dictionary with name parts as keys and their call paths as Returns a dictionary with name parts as keys and their call paths as
values. values.
""" """
def def_ref_filter(name):
is_def = name.is_definition()
return definitions and is_def or references and not is_def
names = list(chain.from_iterable(module.get_used_names().values())) names = list(chain.from_iterable(module.get_used_names().values()))
if not all_scopes: if not all_scopes:
# We have to filter all the names that don't have the module as a # We have to filter all the names that don't have the module as a
@@ -142,7 +146,7 @@ def get_module_names(module, all_scopes):
return parent_scope in (module, None) return parent_scope in (module, None)
names = [n for n in names if is_module_scope_name(n)] names = [n for n in names if is_module_scope_name(n)]
return names return filter(def_ref_filter, names)
def is_string(value): def is_string(value):

View File

@@ -51,10 +51,21 @@ def test_load_save_project(tmpdir):
dict(complete=True)), dict(complete=True)),
('test_load_save_p', ['test_api.test_project.test_load_save_project'], ('test_load_save_p', ['test_api.test_project.test_load_save_project'],
dict(complete=True, all_scopes=True)), dict(complete=True, all_scopes=True)),
('some_search_test_var', [], {}),
('some_search_test_var', ['test_api.test_project.test_search.some_search_test_var'],
dict(all_scopes=True)),
('some_search_test_var', ['test_api.test_project.test_search.some_search_test_var'],
dict(complete=True, all_scopes=True)),
('sample_int', ['helpers.sample_int'], {}),
('sample_int', ['helpers.sample_int'], dict(all_scopes=True)),
('sample_int.real', ['builtins.int.real'], {}),
] ]
) )
@pytest.mark.skipif(sys.version_info < (3, 6), reason="Ignore Python 2, because EOL") @pytest.mark.skipif(sys.version_info < (3, 6), reason="Ignore Python 2, because EOL")
def test_search(string, full_names, kwargs, skip_pre_python36): def test_search(string, full_names, kwargs, skip_pre_python36):
some_search_test_var = 1.0
project = Project(test_dir) project = Project(test_dir)
defs = project.search(string, **kwargs) defs = project.search(string, **kwargs)
assert [d.full_name for d in defs] == full_names assert [d.full_name for d in defs] == full_names