forked from VimPlug/jedi
Differentiate between namespace and module as a type
Also fixed a bug related to implicit namespace contexts, fixes #1033.
This commit is contained in:
@@ -153,13 +153,13 @@ class BaseDefinition(object):
|
|||||||
"""The path to a module/class/function definition."""
|
"""The path to a module/class/function definition."""
|
||||||
def to_reverse():
|
def to_reverse():
|
||||||
name = self._name
|
name = self._name
|
||||||
if name.api_type == 'module':
|
if name.api_type in ('module', 'namespace'):
|
||||||
try:
|
try:
|
||||||
name = list(name.infer())[0].name
|
name = list(name.infer())[0].name
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if name.api_type == 'module':
|
if name.api_type in ('module', 'namespace'):
|
||||||
module_contexts = name.infer()
|
module_contexts = name.infer()
|
||||||
if module_contexts:
|
if module_contexts:
|
||||||
module_context, = module_contexts
|
module_context, = module_contexts
|
||||||
@@ -521,7 +521,7 @@ class Definition(BaseDefinition):
|
|||||||
"""
|
"""
|
||||||
typ = self.type
|
typ = self.type
|
||||||
tree_name = self._name.tree_name
|
tree_name = self._name.tree_name
|
||||||
if typ in ('function', 'class', 'module', 'instance') or tree_name is None:
|
if typ in ('function', 'class', 'module', 'instance', 'namespace') or tree_name is None:
|
||||||
if typ == 'function':
|
if typ == 'function':
|
||||||
# For the description we want a short and a pythonic way.
|
# For the description we want a short and a pythonic way.
|
||||||
typ = 'def'
|
typ = 'def'
|
||||||
|
|||||||
@@ -13,21 +13,21 @@ class ImplicitNSName(AbstractNameDefinition):
|
|||||||
This object will prevent Jedi from raising exceptions
|
This object will prevent Jedi from raising exceptions
|
||||||
"""
|
"""
|
||||||
def __init__(self, implicit_ns_context, string_name):
|
def __init__(self, implicit_ns_context, string_name):
|
||||||
self.implicit_ns_context = implicit_ns_context
|
self.parent_context = implicit_ns_context
|
||||||
self.string_name = string_name
|
self.string_name = string_name
|
||||||
|
|
||||||
def infer(self):
|
def infer(self):
|
||||||
return NO_CONTEXTS
|
return NO_CONTEXTS
|
||||||
|
|
||||||
def get_root_context(self):
|
def get_root_context(self):
|
||||||
return self.implicit_ns_context
|
return self.parent_context
|
||||||
|
|
||||||
|
|
||||||
class ImplicitNamespaceContext(TreeContext):
|
class ImplicitNamespaceContext(TreeContext):
|
||||||
"""
|
"""
|
||||||
Provides support for implicit namespace packages
|
Provides support for implicit namespace packages
|
||||||
"""
|
"""
|
||||||
api_type = u'module'
|
api_type = u'namespace'
|
||||||
parent_context = None
|
parent_context = None
|
||||||
|
|
||||||
def __init__(self, evaluator, fullname, paths):
|
def __init__(self, evaluator, fullname, paths):
|
||||||
|
|||||||
@@ -419,7 +419,7 @@ class Importer(object):
|
|||||||
|
|
||||||
for context in self.follow():
|
for context in self.follow():
|
||||||
# Non-modules are not completable.
|
# Non-modules are not completable.
|
||||||
if context.api_type != 'module': # not a module
|
if context.api_type not in ('namespace', 'module'): # not a module
|
||||||
continue
|
continue
|
||||||
# namespace packages
|
# namespace packages
|
||||||
if isinstance(context, ModuleContext) and context.py__file__().endswith('__init__.py'):
|
if isinstance(context, ModuleContext) and context.py__file__().endswith('__init__.py'):
|
||||||
|
|||||||
@@ -51,12 +51,15 @@ def test_implicit_nested_namespace_package(Script, environment):
|
|||||||
if environment.version_info < (3, 4):
|
if environment.version_info < (3, 4):
|
||||||
pytest.skip()
|
pytest.skip()
|
||||||
|
|
||||||
CODE = 'from implicit_nested_namespaces.namespace.pkg.module import CONST'
|
code = 'from implicit_nested_namespaces.namespace.pkg.module import CONST'
|
||||||
|
|
||||||
sys_path = [dirname(__file__)]
|
sys_path = [dirname(__file__)]
|
||||||
|
|
||||||
script = Script(sys_path=sys_path, source=CODE, line=1, column=61)
|
script = Script(sys_path=sys_path, source=code, line=1, column=61)
|
||||||
|
|
||||||
result = script.goto_definitions()
|
result = script.goto_definitions()
|
||||||
|
|
||||||
assert len(result) == 1
|
assert len(result) == 1
|
||||||
|
|
||||||
|
implicit_pkg, = Script(code, column=10, sys_path=sys_path).goto_definitions()
|
||||||
|
assert implicit_pkg.type == 'namespace'
|
||||||
|
|||||||
Reference in New Issue
Block a user