mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Make is_package a function and call it consistently
This commit is contained in:
@@ -89,6 +89,7 @@ class CompiledObject(Value):
|
||||
return None
|
||||
return map(cast_path, paths)
|
||||
|
||||
@property
|
||||
def is_package(self):
|
||||
return self.py__path__() is not None
|
||||
|
||||
|
||||
@@ -302,7 +302,6 @@ class ModuleContext(TreeContextMixin, ValueContext):
|
||||
def py__package__(self):
|
||||
return self._value.py__package__
|
||||
|
||||
@property
|
||||
def is_package(self):
|
||||
return self._value.is_package
|
||||
|
||||
@@ -392,6 +391,13 @@ class CompiledModuleContext(CompiledContext):
|
||||
def py__file__(self):
|
||||
return self._value.py__file__()
|
||||
|
||||
@property
|
||||
def py__package__(self):
|
||||
return self._value.py__package__
|
||||
|
||||
def is_package(self):
|
||||
return self._value.is_package()
|
||||
|
||||
|
||||
def _get_global_filters_for_name(context, name_or_none, position):
|
||||
# For functions and classes the defaults don't belong to the
|
||||
|
||||
@@ -235,7 +235,7 @@ def _load_from_typeshed(inference_state, python_value_set, parent_module_value,
|
||||
map_ = _cache_stub_file_map(inference_state.grammar.version_info)
|
||||
import_name = _IMPORT_MAP.get(import_name, import_name)
|
||||
elif isinstance(parent_module_value, ModuleValue):
|
||||
if not parent_module_value.is_package:
|
||||
if not parent_module_value.is_package():
|
||||
# Only if it's a package (= a folder) something can be
|
||||
# imported.
|
||||
return None
|
||||
|
||||
@@ -93,7 +93,7 @@ class AbstractTreeName(AbstractNameDefinition):
|
||||
# In case of level == 1, it works always, because it's like a submodule
|
||||
# lookup.
|
||||
if import_node is not None and not (import_node.level == 1
|
||||
and self.get_root_context().is_package):
|
||||
and self.get_root_context().is_package()):
|
||||
# TODO improve the situation for when level is present.
|
||||
if include_module_names and not import_node.level:
|
||||
return tuple(n.value for n in import_node.get_path_for_name(self.tree_name))
|
||||
|
||||
@@ -83,7 +83,7 @@ class SubModuleDictMixin(object):
|
||||
package).
|
||||
"""
|
||||
names = {}
|
||||
if self.is_package:
|
||||
if self.is_package():
|
||||
mods = iter_module_names(self.inference_state, self.py__path__())
|
||||
for name in mods:
|
||||
# It's obviously a relative import to the current module.
|
||||
@@ -200,7 +200,7 @@ class ModuleValue(ModuleMixin, TreeValue):
|
||||
self._path = file_io.path
|
||||
self.string_names = string_names # Optional[Tuple[str, ...]]
|
||||
self.code_lines = code_lines
|
||||
self.is_package = is_package
|
||||
self._is_package = is_package
|
||||
|
||||
def is_stub(self):
|
||||
if self._path is not None and self._path.endswith('.pyi'):
|
||||
@@ -224,8 +224,11 @@ class ModuleValue(ModuleMixin, TreeValue):
|
||||
|
||||
return os.path.abspath(self._path)
|
||||
|
||||
def is_package(self):
|
||||
return self._is_package
|
||||
|
||||
def py__package__(self):
|
||||
if self.is_package:
|
||||
if self._is_package:
|
||||
return self.string_names
|
||||
return self.string_names[:-1]
|
||||
|
||||
@@ -235,7 +238,7 @@ class ModuleValue(ModuleMixin, TreeValue):
|
||||
is a list of paths (strings).
|
||||
Returns None if the module is not a package.
|
||||
"""
|
||||
if not self.is_package:
|
||||
if not self._is_package:
|
||||
return None
|
||||
|
||||
# A namespace package is typically auto generated and ~10 lines long.
|
||||
|
||||
@@ -61,7 +61,6 @@ class ImplicitNamespaceValue(Value, SubModuleDictMixin):
|
||||
def is_stub(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def is_package(self):
|
||||
return True
|
||||
|
||||
|
||||
@@ -91,7 +91,7 @@ def test_correct_zip_package_behavior(Script, inference_state, environment, code
|
||||
value, = pkg._name.infer()
|
||||
assert value.py__file__() == os.path.join(pkg_zip_path, 'pkg', file)
|
||||
assert '.'.join(value.py__package__()) == package
|
||||
assert value.is_package is (path is not None)
|
||||
assert value.is_package() is (path is not None)
|
||||
if path is not None:
|
||||
assert value.py__path__() == [os.path.join(pkg_zip_path, path)]
|
||||
|
||||
@@ -343,7 +343,7 @@ def test_get_modules_containing_name(inference_state, path, goal, is_package):
|
||||
def test_load_module_from_path(inference_state, path, base_names, is_package, names):
|
||||
file_io = KnownContentFileIO(path, '')
|
||||
m = imports._load_module_from_path(inference_state, file_io, base_names)
|
||||
assert m.is_package == is_package
|
||||
assert m.is_package() == is_package
|
||||
assert m.string_names == names
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user