mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 14:54:47 +08:00
Cleanup a few issues with the latest module refactoring
This commit is contained in:
@@ -95,12 +95,12 @@ def _check_for_setattr(instance):
|
|||||||
"""
|
"""
|
||||||
Check if there's any setattr method inside an instance. If so, return True.
|
Check if there's any setattr method inside an instance. If so, return True.
|
||||||
"""
|
"""
|
||||||
from jedi.evaluate.context import ModuleContext
|
|
||||||
module = instance.get_root_context()
|
module = instance.get_root_context()
|
||||||
if not isinstance(module, ModuleContext):
|
node = module.tree_node
|
||||||
|
if node is None:
|
||||||
|
# If it's a compiled module or doesn't have a tree_node
|
||||||
return False
|
return False
|
||||||
|
|
||||||
node = module.tree_node
|
|
||||||
try:
|
try:
|
||||||
stmt_names = node.get_used_names()['setattr']
|
stmt_names = node.get_used_names()['setattr']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ class ImplicitNamespaceContext(Context):
|
|||||||
super(ImplicitNamespaceContext, self).__init__(evaluator, parent_context=None)
|
super(ImplicitNamespaceContext, self).__init__(evaluator, parent_context=None)
|
||||||
self.evaluator = evaluator
|
self.evaluator = evaluator
|
||||||
self._fullname = fullname
|
self._fullname = fullname
|
||||||
self.paths = paths
|
self._paths = paths
|
||||||
|
|
||||||
def get_filters(self, search_global=False, until_position=None, origin_scope=None):
|
def get_filters(self, search_global=False, until_position=None, origin_scope=None):
|
||||||
yield DictFilter(self._sub_modules_dict())
|
yield DictFilter(self._sub_modules_dict())
|
||||||
@@ -51,7 +51,7 @@ class ImplicitNamespaceContext(Context):
|
|||||||
return self._fullname
|
return self._fullname
|
||||||
|
|
||||||
def py__path__(self):
|
def py__path__(self):
|
||||||
return [self.paths]
|
return self._paths
|
||||||
|
|
||||||
def py__name__(self):
|
def py__name__(self):
|
||||||
return self._fullname
|
return self._fullname
|
||||||
@@ -60,7 +60,7 @@ class ImplicitNamespaceContext(Context):
|
|||||||
def _sub_modules_dict(self):
|
def _sub_modules_dict(self):
|
||||||
names = {}
|
names = {}
|
||||||
|
|
||||||
file_names = chain.from_iterable(os.listdir(path) for path in self.paths)
|
file_names = chain.from_iterable(os.listdir(path) for path in self._paths)
|
||||||
mods = [
|
mods = [
|
||||||
file_name.rpartition('.')[0] if '.' in file_name else file_name
|
file_name.rpartition('.')[0] if '.' in file_name else file_name
|
||||||
for file_name in file_names
|
for file_name in file_names
|
||||||
|
|||||||
@@ -328,8 +328,6 @@ class Importer(object):
|
|||||||
:param only_modules: Indicates wheter it's possible to import a
|
:param only_modules: Indicates wheter it's possible to import a
|
||||||
definition that is not defined in a module.
|
definition that is not defined in a module.
|
||||||
"""
|
"""
|
||||||
from jedi.evaluate.context import ModuleContext
|
|
||||||
from jedi.evaluate.context.namespace import ImplicitNamespaceContext
|
|
||||||
names = []
|
names = []
|
||||||
if self.import_path:
|
if self.import_path:
|
||||||
# flask
|
# flask
|
||||||
@@ -351,15 +349,13 @@ class Importer(object):
|
|||||||
if context.api_type != 'module': # not a module
|
if context.api_type != 'module': # not a module
|
||||||
continue
|
continue
|
||||||
# namespace packages
|
# namespace packages
|
||||||
if isinstance(context, ModuleContext) \
|
try:
|
||||||
and context.py__file__().endswith('__init__.py'):
|
path_method = context.py__path__
|
||||||
paths = context.py__path__()
|
except AttributeError:
|
||||||
names += self._get_module_names(paths, in_module=context)
|
pass
|
||||||
|
else:
|
||||||
# implicit namespace packages
|
# For implicit namespace packages and module names.
|
||||||
elif isinstance(context, ImplicitNamespaceContext):
|
names += self._get_module_names(path_method(), in_module=context)
|
||||||
paths = context.paths
|
|
||||||
names += self._get_module_names(paths, in_module=context)
|
|
||||||
|
|
||||||
if only_modules:
|
if only_modules:
|
||||||
# In the case of an import like `from x.` we don't need to
|
# In the case of an import like `from x.` we don't need to
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
from jedi.evaluate import imports
|
from jedi.evaluate import imports
|
||||||
from jedi.evaluate.filters import TreeNameDefinition
|
from jedi.evaluate.filters import TreeNameDefinition
|
||||||
from jedi.evaluate.context import ModuleContext
|
|
||||||
|
|
||||||
|
|
||||||
def _resolve_names(definition_names, avoid_names=()):
|
def _resolve_names(definition_names, avoid_names=()):
|
||||||
@@ -39,7 +38,7 @@ def usages(module_context, tree_name):
|
|||||||
search_name = tree_name.value
|
search_name = tree_name.value
|
||||||
found_names = _find_names(module_context, tree_name)
|
found_names = _find_names(module_context, tree_name)
|
||||||
modules = set(d.get_root_context() for d in found_names.values())
|
modules = set(d.get_root_context() for d in found_names.values())
|
||||||
modules = set(m for m in modules if isinstance(m, ModuleContext))
|
modules = set(m for m in modules if m.is_module())
|
||||||
|
|
||||||
non_matching_usage_maps = {}
|
non_matching_usage_maps = {}
|
||||||
for m in imports.get_modules_containing_name(module_context.evaluator, modules, search_name):
|
for m in imports.get_modules_containing_name(module_context.evaluator, modules, search_name):
|
||||||
|
|||||||
@@ -65,27 +65,31 @@ def test_basedefinition_type(Script, environment):
|
|||||||
'generator', 'statement', 'import', 'param')
|
'generator', 'statement', 'import', 'param')
|
||||||
|
|
||||||
|
|
||||||
def test_basedefinition_type_import(Script):
|
@pytest.mark.parametrize(
|
||||||
def get_types(source, **kwargs):
|
('src', 'expected_result', 'column'), [
|
||||||
return {t.type for t in Script(source, **kwargs).completions()}
|
|
||||||
|
|
||||||
# import one level
|
# import one level
|
||||||
assert get_types('import t') == {'module'}
|
('import t', 'module', None),
|
||||||
assert get_types('import ') == {'module'}
|
('import ', 'module', None),
|
||||||
assert get_types('import datetime; datetime') == {'module'}
|
('import datetime; datetime', 'module', None),
|
||||||
|
|
||||||
# from
|
# from
|
||||||
assert get_types('from datetime import timedelta') == {'class'}
|
('from datetime import timedelta', 'class', None),
|
||||||
assert get_types('from datetime import timedelta; timedelta') == {'class'}
|
('from datetime import timedelta; timedelta', 'class', None),
|
||||||
assert get_types('from json import tool') == {'module'}
|
('from json import tool', 'module', None),
|
||||||
assert get_types('from json import tool; tool') == {'module'}
|
('from json import tool; tool', 'module', None),
|
||||||
|
|
||||||
# import two levels
|
# import two levels
|
||||||
assert get_types('import json.tool; json') == {'module'}
|
('import json.tool; json', 'module', None),
|
||||||
assert get_types('import json.tool; json.tool') == {'module'}
|
('import json.tool; json.tool', 'module', None),
|
||||||
assert get_types('import json.tool; json.tool.main') == {'function'}
|
('import json.tool; json.tool.main', 'function', None),
|
||||||
assert get_types('import json.tool') == {'module'}
|
('import json.tool', 'module', None),
|
||||||
assert get_types('import json.tool', column=9) == {'module'}
|
('import json.tool', 'module', 9),
|
||||||
|
]
|
||||||
|
|
||||||
|
)
|
||||||
|
def test_basedefinition_type_import(Script, src, expected_result, column):
|
||||||
|
types = {t.type for t in Script(src, column=column).completions()}
|
||||||
|
assert types == {expected_result}
|
||||||
|
|
||||||
|
|
||||||
def test_function_call_signature_in_doc(Script):
|
def test_function_call_signature_in_doc(Script):
|
||||||
|
|||||||
Reference in New Issue
Block a user