mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 05:54:25 +08:00
Remove submodule dict issues from namespace packages
This commit is contained in:
@@ -37,7 +37,37 @@ class ModuleName(ContextNameMixin, AbstractNameDefinition):
|
||||
return self._name
|
||||
|
||||
|
||||
class ModuleMixin(object):
|
||||
class SubModuleDictMixin(object):
|
||||
@evaluator_method_cache()
|
||||
def _sub_modules_dict(self):
|
||||
"""
|
||||
Lists modules in the directory of this module (if this module is a
|
||||
package).
|
||||
"""
|
||||
from jedi.evaluate.imports import SubModuleName
|
||||
|
||||
names = {}
|
||||
try:
|
||||
method = self.py__path__
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
for path in method():
|
||||
mods = iter_modules([path])
|
||||
for module_loader, name, is_pkg in mods:
|
||||
# It's obviously a relative import to the current module.
|
||||
names[name] = SubModuleName(self, name)
|
||||
|
||||
# TODO add something like this in the future, its cleaner than the
|
||||
# import hacks.
|
||||
# ``os.path`` is a hardcoded exception, because it's a
|
||||
# ``sys.modules`` modification.
|
||||
# if str(self.name) == 'os':
|
||||
# names.append(Name('path', parent_context=self))
|
||||
|
||||
return names
|
||||
|
||||
class ModuleMixin(SubModuleDictMixin):
|
||||
def get_filters(self, search_global=False, until_position=None, origin_scope=None):
|
||||
yield MergedFilter(
|
||||
ParserTreeFilter(
|
||||
@@ -77,35 +107,6 @@ class ModuleMixin(object):
|
||||
# Remove PEP 3149 names
|
||||
return re.sub(r'\.[a-z]+-\d{2}[mud]{0,3}$', '', r.group(1))
|
||||
|
||||
@evaluator_method_cache()
|
||||
def _sub_modules_dict(self):
|
||||
"""
|
||||
Lists modules in the directory of this module (if this module is a
|
||||
package).
|
||||
"""
|
||||
from jedi.evaluate.imports import SubModuleName
|
||||
|
||||
names = {}
|
||||
try:
|
||||
method = self.py__path__
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
for path in method():
|
||||
mods = iter_modules([path])
|
||||
for module_loader, name, is_pkg in mods:
|
||||
# It's obviously a relative import to the current module.
|
||||
names[name] = SubModuleName(self, name)
|
||||
|
||||
# TODO add something like this in the future, its cleaner than the
|
||||
# import hacks.
|
||||
# ``os.path`` is a hardcoded exception, because it's a
|
||||
# ``sys.modules`` modification.
|
||||
# if str(self.name) == 'os':
|
||||
# names.append(Name('path', parent_context=self))
|
||||
|
||||
return names
|
||||
|
||||
@evaluator_method_cache()
|
||||
def _module_attributes_dict(self):
|
||||
names = ['__file__', '__package__', '__doc__', '__name__']
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import os
|
||||
from itertools import chain
|
||||
|
||||
from jedi.evaluate.cache import evaluator_method_cache
|
||||
from jedi.evaluate import imports
|
||||
from jedi.evaluate.filters import DictFilter, AbstractNameDefinition, ContextNameMixin
|
||||
from jedi.evaluate.base_context import Context
|
||||
from jedi.evaluate.context.module import SubModuleDictMixin
|
||||
|
||||
|
||||
class ImplicitNSName(ContextNameMixin, AbstractNameDefinition):
|
||||
@@ -17,7 +14,7 @@ class ImplicitNSName(ContextNameMixin, AbstractNameDefinition):
|
||||
self.string_name = string_name
|
||||
|
||||
|
||||
class ImplicitNamespaceContext(Context):
|
||||
class ImplicitNamespaceContext(Context, SubModuleDictMixin):
|
||||
"""
|
||||
Provides support for implicit namespace packages
|
||||
"""
|
||||
@@ -56,20 +53,5 @@ class ImplicitNamespaceContext(Context):
|
||||
def py__name__(self):
|
||||
return self._fullname
|
||||
|
||||
@evaluator_method_cache()
|
||||
def _sub_modules_dict(self):
|
||||
names = {}
|
||||
|
||||
file_names = chain.from_iterable(os.listdir(path) for path in self._paths)
|
||||
mods = [
|
||||
file_name.rpartition('.')[0] if '.' in file_name else file_name
|
||||
for file_name in file_names
|
||||
if file_name != '__pycache__'
|
||||
]
|
||||
|
||||
for name in mods:
|
||||
names[name] = imports.SubModuleName(self, name)
|
||||
return names
|
||||
|
||||
def __repr__(self):
|
||||
return '<%s: %s>' % (self.__class__.__name__, self._fullname)
|
||||
|
||||
@@ -59,11 +59,25 @@ def test_find_module_package_zipped(Script, evaluator, environment):
|
||||
@pytest.mark.parametrize(
|
||||
'code, file, package, path', [
|
||||
('import pkg', '__init__.py', 'pkg', 'pkg'),
|
||||
#('import pkg', '__init__.py', 'pkg', 'pkg'),
|
||||
|
||||
('from pkg import module', 'module.py', 'pkg', None),
|
||||
#('from pkg.module', 'module.py', 'pkg', None),
|
||||
|
||||
('from pkg import nested', os.path.join('nested', '__init__.py'),
|
||||
'pkg.nested', os.path.join('pkg', 'nested')),
|
||||
#('from pkg.nested', os.path.join('nested', '__init__.py'),
|
||||
# 'pkg.nested', os.path.join('pkg', 'nested')),
|
||||
|
||||
('from pkg.nested import nested_module',
|
||||
os.path.join('nested', 'nested_module.py'), 'pkg.nested', None),
|
||||
#('from pkg.nested.nested_module',
|
||||
# os.path.join('nested', 'nested_module.py'), 'pkg.nested', None),
|
||||
|
||||
('from pkg.namespace import namespace_module',
|
||||
os.path.join('namespace', 'namespace_module.py'), 'pkg.namespace', None),
|
||||
#('from pkg.namespace.namespace_module',
|
||||
# os.path.join('namespace', 'namespace_module.py'), 'pkg.namespace', None),
|
||||
]
|
||||
|
||||
)
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user