forked from VimPlug/jedi
Only deal with explicit absolute_import
This commit is contained in:
@@ -91,11 +91,6 @@ class ImportPath(pr.Base):
|
|||||||
and len(self.import_stmt.namespace.names) > 1 \
|
and len(self.import_stmt.namespace.names) > 1 \
|
||||||
and not self.direct_resolve
|
and not self.direct_resolve
|
||||||
|
|
||||||
@property
|
|
||||||
def is_absolute(self):
|
|
||||||
module = self.import_stmt.get_parent_until(pr.IsScope)
|
|
||||||
return module.absolute_imports
|
|
||||||
|
|
||||||
def get_nested_import(self, parent):
|
def get_nested_import(self, parent):
|
||||||
"""
|
"""
|
||||||
See documentation of `self.is_nested_import`.
|
See documentation of `self.is_nested_import`.
|
||||||
@@ -257,7 +252,8 @@ class ImportPath(pr.Base):
|
|||||||
|
|
||||||
return importing
|
return importing
|
||||||
|
|
||||||
if self.file_path and not self.is_absolute:
|
parent = self.import_stmt.get_parent_until()
|
||||||
|
if self.file_path and not parent.explicit_absolute_import:
|
||||||
sys_path_mod = list(self.sys_path_with_modifications())
|
sys_path_mod = list(self.sys_path_with_modifications())
|
||||||
sys_path_mod.insert(0, self.file_path)
|
sys_path_mod.insert(0, self.file_path)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -155,6 +155,7 @@ class Scope(Simple, IsScope):
|
|||||||
# returns will be in "normal" modules.
|
# returns will be in "normal" modules.
|
||||||
self.returns = []
|
self.returns = []
|
||||||
self.is_generator = False
|
self.is_generator = False
|
||||||
|
self._explicit_absolute_imports = None
|
||||||
|
|
||||||
def add_scope(self, sub, decorators):
|
def add_scope(self, sub, decorators):
|
||||||
sub.parent = self.use_as_parent
|
sub.parent = self.use_as_parent
|
||||||
@@ -295,26 +296,23 @@ class Scope(Simple, IsScope):
|
|||||||
return p
|
return p
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def absolute_imports(self):
|
def explicit_absolute_import(self):
|
||||||
"""
|
"""
|
||||||
Checks if imports in this scope are absolute.
|
Checks if imports in this scope are explicitly absolute, i.e. there
|
||||||
|
is a ``__future__`` import.
|
||||||
|
|
||||||
In Python 3, this is always true. In Python 2, this is true if there
|
The result of this property is cached; the first time it is
|
||||||
is a ``absolute_import`` ``__future__`` import.
|
called will cause it to walk through all the imports in the
|
||||||
|
parse tree.
|
||||||
|
|
||||||
The result of this property is cached; the first time it is called on
|
|
||||||
Python 2 will cause it to walk through all the imports in the parse
|
|
||||||
tree.
|
|
||||||
"""
|
"""
|
||||||
if self._absolute_imports is not None:
|
if self._explicit_absolute_imports is not None:
|
||||||
return self._absolute_imports
|
return self._explicit_absolute_imports
|
||||||
|
|
||||||
has_import = any(_enables_absolute_import(i) for i in self.imports)
|
has_import = any(_enables_absolute_import(i) for i in self.imports)
|
||||||
self._absolute_imports = has_import
|
self._explicit_absolute_imports = has_import
|
||||||
return has_import
|
return has_import
|
||||||
|
|
||||||
_absolute_imports = True if is_py3k else None
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
try:
|
try:
|
||||||
name = self.path
|
name = self.path
|
||||||
|
|||||||
@@ -3,22 +3,20 @@ from jedi.parsing import Parser
|
|||||||
from . import base
|
from . import base
|
||||||
|
|
||||||
|
|
||||||
@base.py3_only
|
def test_explicit_absolute_imports():
|
||||||
def test_py3k_imports_are_always_absolute():
|
|
||||||
"""
|
"""
|
||||||
By default, imports in Python 3 are absolute.
|
Detect modules with ``from __future__ import absolute_import``.
|
||||||
"""
|
"""
|
||||||
parser = Parser("1", "test.py")
|
parser = Parser("from __future__ import absolute_import", "test.py")
|
||||||
assert parser.scope.absolute_imports
|
assert parser.scope.explicit_absolute_import
|
||||||
|
|
||||||
|
|
||||||
@base.py2_only
|
def test_no_explicit_absolute_imports():
|
||||||
def test_py2_imports_are_not_always_absolute():
|
|
||||||
"""
|
"""
|
||||||
By default, imports in Python 2 are not absolute.
|
Detect modules without ``from __future__ import absolute_import``.
|
||||||
"""
|
"""
|
||||||
parser = Parser("1", "test.py")
|
parser = Parser("1", "test.py")
|
||||||
assert not parser.scope.absolute_imports
|
assert not parser.scope.explicit_absolute_import
|
||||||
|
|
||||||
|
|
||||||
def test_dont_break_imports_without_namespaces():
|
def test_dont_break_imports_without_namespaces():
|
||||||
@@ -28,16 +26,7 @@ def test_dont_break_imports_without_namespaces():
|
|||||||
"""
|
"""
|
||||||
src = "from __future__ import absolute_import\nimport xyzzy"
|
src = "from __future__ import absolute_import\nimport xyzzy"
|
||||||
parser = Parser(src, "test.py")
|
parser = Parser(src, "test.py")
|
||||||
assert parser.scope.absolute_imports
|
assert parser.scope.explicit_absolute_import
|
||||||
|
|
||||||
|
|
||||||
def test_imports_are_absolute_in_modules_with_future_import():
|
|
||||||
"""
|
|
||||||
In any module with the ``absolute_import`` ``__future__`` import, all
|
|
||||||
imports are absolute.
|
|
||||||
"""
|
|
||||||
parser = Parser("from __future__ import absolute_import", "test.py")
|
|
||||||
assert parser.scope.absolute_imports
|
|
||||||
|
|
||||||
|
|
||||||
@base.cwd_at("test/absolute_import")
|
@base.cwd_at("test/absolute_import")
|
||||||
|
|||||||
Reference in New Issue
Block a user