Skip to content

Commit

Permalink
Differentiate between namespace and module as a type
Browse files Browse the repository at this point in the history
Also fixed a bug related to implicit namespace contexts, fixes davidhalter#1033.
  • Loading branch information
davidhalter committed Jan 25, 2018
1 parent 33c9d21 commit 04fba28
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions jedi/api/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ def _path(self):
"""The path to a module/class/function definition."""
def to_reverse():
name = self._name
if name.api_type == 'module':
if name.api_type in ('module', 'namespace'):
try:
name = list(name.infer())[0].name
except IndexError:
pass

if name.api_type == 'module':
if name.api_type in ('module', 'namespace'):
module_contexts = name.infer()
if module_contexts:
module_context, = module_contexts
Expand Down Expand Up @@ -521,7 +521,7 @@ def description(self):
"""
typ = self.type
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':
# For the description we want a short and a pythonic way.
typ = 'def'
Expand Down
6 changes: 3 additions & 3 deletions jedi/evaluate/context/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ class ImplicitNSName(AbstractNameDefinition):
This object will prevent Jedi from raising exceptions
"""
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

def infer(self):
return NO_CONTEXTS

def get_root_context(self):
return self.implicit_ns_context
return self.parent_context


class ImplicitNamespaceContext(TreeContext):
"""
Provides support for implicit namespace packages
"""
api_type = u'module'
api_type = u'namespace'
parent_context = None

def __init__(self, evaluator, fullname, paths):
Expand Down
2 changes: 1 addition & 1 deletion jedi/evaluate/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def completion_names(self, evaluator, only_modules=False):

for context in self.follow():
# 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
# namespace packages
if isinstance(context, ModuleContext) and context.py__file__().endswith('__init__.py'):
Expand Down
7 changes: 5 additions & 2 deletions test/test_evaluate/test_implicit_namespace_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ def test_implicit_nested_namespace_package(Script, environment):
if environment.version_info < (3, 4):
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__)]

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()

assert len(result) == 1

implicit_pkg, = Script(code, column=10, sys_path=sys_path).goto_definitions()
assert implicit_pkg.type == 'namespace'

0 comments on commit 04fba28

Please sign in to comment.