Fix stub searching for nested modules

This commit is contained in:
Dave Halter
2018-07-27 10:14:37 +02:00
parent e827559340
commit 4e75a35468
4 changed files with 61 additions and 33 deletions
+5 -3
View File
@@ -88,11 +88,13 @@ class ModuleContext(TreeContext):
@property
def _string_name(self):
""" This is used for the goto functions. """
# TODO It's ugly that we even use this, the name is usually well known
# ahead so just pass it when create a ModuleContext.
if self._path is None:
return '' # no path -> empty name
else:
sep = (re.escape(os.path.sep),) * 2
r = re.search(r'([^%s]*?)(%s__init__)?(\.py|\.so)?$' % sep, self._path)
r = re.search(r'([^%s]*?)(%s__init__)?(\.pyi?|\.so)?$' % sep, self._path)
# Remove PEP 3149 names
return re.sub(r'\.[a-z]+-\d{2}[mud]{0,3}$', '', r.group(1))
@@ -106,7 +108,7 @@ class ModuleContext(TreeContext):
:return: The path to the directory of a package. None in case it's not
a package.
"""
for suffix in all_suffixes():
for suffix in all_suffixes() + ['.pyi']:
ending = '__init__' + suffix
py__file__ = self.py__file__()
if py__file__ is not None and py__file__.endswith(ending):
@@ -139,7 +141,7 @@ class ModuleContext(TreeContext):
def _py__path__(self):
search_path = self.evaluator.get_sys_path()
init_path = self.py__file__()
if os.path.basename(init_path) == '__init__.py':
if os.path.basename(init_path) in ('__init__.py', '__init__.pyi'):
with open(init_path, 'rb') as f:
content = python_bytes_to_unicode(f.read(), errors='replace')
# these are strings that need to be used for namespace packages,
+5 -5
View File
@@ -293,10 +293,10 @@ class Importer(object):
self._evaluator.import_module(
self._evaluator,
import_names[:i+1],
module_context,
parent_module_context,
self.sys_path_with_modifications(),
)
for module_context in context_set
for parent_module_context in context_set
])
except JediImportError:
_add_error(self.module_context, name)
@@ -397,7 +397,7 @@ class JediImportError(Exception):
self.import_names = import_names
def import_module(evaluator, import_names, module_context, sys_path):
def import_module(evaluator, import_names, parent_module_context, sys_path):
"""
This method is very similar to importlib's `_gcd_import`.
"""
@@ -415,7 +415,7 @@ def import_module(evaluator, import_names, module_context, sys_path):
except KeyError:
pass
if module_context is None:
if parent_module_context is None:
debug.dbg('global search_module %s', import_names[-1])
# Override the sys.path. It works only good that way.
# Injecting the path directly into `find_module` did not work.
@@ -429,7 +429,7 @@ def import_module(evaluator, import_names, module_context, sys_path):
raise JediImportError(import_names)
else:
try:
method = module_context.py__path__
method = parent_module_context.py__path__
except AttributeError:
# The module is not a package.
raise JediImportError(import_names)