mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Make the Project API public, fixes #778
This commit is contained in:
@@ -41,6 +41,7 @@ from jedi import settings
|
|||||||
from jedi.api.environment import find_virtualenvs, find_system_environments, \
|
from jedi.api.environment import find_virtualenvs, find_system_environments, \
|
||||||
get_default_environment, InvalidPythonEnvironment, create_environment, \
|
get_default_environment, InvalidPythonEnvironment, create_environment, \
|
||||||
get_system_environment
|
get_system_environment
|
||||||
|
from jedi.api.project import Project
|
||||||
from jedi.api.exceptions import InternalError
|
from jedi.api.exceptions import InternalError
|
||||||
# Finally load the internal plugins. This is only internal.
|
# Finally load the internal plugins. This is only internal.
|
||||||
from jedi.plugins import registry
|
from jedi.plugins import registry
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ class Script(object):
|
|||||||
"""
|
"""
|
||||||
def __init__(self, source=None, line=None, column=None, path=None,
|
def __init__(self, source=None, line=None, column=None, path=None,
|
||||||
encoding='utf-8', sys_path=None, environment=None,
|
encoding='utf-8', sys_path=None, environment=None,
|
||||||
_project=None):
|
project=None):
|
||||||
self._orig_path = path
|
self._orig_path = path
|
||||||
# An empty path (also empty string) should always result in no path.
|
# An empty path (also empty string) should always result in no path.
|
||||||
self.path = os.path.abspath(path) if path else None
|
self.path = os.path.abspath(path) if path else None
|
||||||
@@ -103,7 +103,6 @@ class Script(object):
|
|||||||
if sys_path is not None and not is_py3:
|
if sys_path is not None and not is_py3:
|
||||||
sys_path = list(map(force_unicode, sys_path))
|
sys_path = list(map(force_unicode, sys_path))
|
||||||
|
|
||||||
project = _project
|
|
||||||
if project is None:
|
if project is None:
|
||||||
# Load the Python grammar of the current interpreter.
|
# Load the Python grammar of the current interpreter.
|
||||||
project = get_default_project(
|
project = get_default_project(
|
||||||
@@ -112,6 +111,12 @@ class Script(object):
|
|||||||
# TODO deprecate and remove sys_path from the Script API.
|
# TODO deprecate and remove sys_path from the Script API.
|
||||||
if sys_path is not None:
|
if sys_path is not None:
|
||||||
project._sys_path = sys_path
|
project._sys_path = sys_path
|
||||||
|
warnings.warn(
|
||||||
|
"Deprecated since version 0.17.0. Use the project API instead, "
|
||||||
|
"which means Script(project=Project(dir, sys_path=sys_path)) instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2
|
||||||
|
)
|
||||||
self._inference_state = InferenceState(
|
self._inference_state = InferenceState(
|
||||||
project, environment=environment, script_path=self.path
|
project, environment=environment, script_path=self.path
|
||||||
)
|
)
|
||||||
@@ -559,7 +564,7 @@ class Interpreter(Script):
|
|||||||
raise TypeError("The environment needs to be an InterpreterEnvironment subclass.")
|
raise TypeError("The environment needs to be an InterpreterEnvironment subclass.")
|
||||||
|
|
||||||
super(Interpreter, self).__init__(source, environment=environment,
|
super(Interpreter, self).__init__(source, environment=environment,
|
||||||
_project=Project(os.getcwd()), **kwds)
|
project=Project(os.getcwd()), **kwds)
|
||||||
self.namespaces = namespaces
|
self.namespaces = namespaces
|
||||||
self._inference_state.allow_descriptor_getattr = self._allow_descriptor_getattr_default
|
self._inference_state.allow_descriptor_getattr = self._allow_descriptor_getattr_default
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ def test_import_references(Script):
|
|||||||
def test_exclude_builtin_modules(Script):
|
def test_exclude_builtin_modules(Script):
|
||||||
def get(include):
|
def get(include):
|
||||||
from jedi.api.project import Project
|
from jedi.api.project import Project
|
||||||
script = Script(source, _project=Project('', sys_path=[], smart_sys_path=False))
|
script = Script(source, project=Project('', sys_path=[], smart_sys_path=False))
|
||||||
references = script.get_references(column=8, include_builtins=include)
|
references = script.get_references(column=8, include_builtins=include)
|
||||||
return [(d.line, d.column) for d in references]
|
return [(d.line, d.column) for d in references]
|
||||||
source = '''import sys\nprint(sys.path)'''
|
source = '''import sys\nprint(sys.path)'''
|
||||||
|
|||||||
@@ -28,10 +28,10 @@ def test_sqlite3_conversion(Script):
|
|||||||
def test_conversion_of_stub_only(Script):
|
def test_conversion_of_stub_only(Script):
|
||||||
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
|
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
|
||||||
code = 'import stub_only; stub_only.in_stub_only'
|
code = 'import stub_only; stub_only.in_stub_only'
|
||||||
d1, = Script(code, _project=project).goto()
|
d1, = Script(code, project=project).goto()
|
||||||
assert d1.is_stub()
|
assert d1.is_stub()
|
||||||
|
|
||||||
script = Script(path=d1.module_path, _project=project)
|
script = Script(path=d1.module_path, project=project)
|
||||||
d2, = script.goto(line=d1.line, column=d1.column)
|
d2, = script.goto(line=d1.line, column=d1.column)
|
||||||
assert d2.is_stub()
|
assert d2.is_stub()
|
||||||
assert d2.module_path == d1.module_path
|
assert d2.module_path == d1.module_path
|
||||||
@@ -42,7 +42,7 @@ def test_conversion_of_stub_only(Script):
|
|||||||
|
|
||||||
def test_goto_on_file(Script):
|
def test_goto_on_file(Script):
|
||||||
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
|
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
|
||||||
script = Script('import stub_only; stub_only.Foo', _project=project)
|
script = Script('import stub_only; stub_only.Foo', project=project)
|
||||||
d1, = script.goto()
|
d1, = script.goto()
|
||||||
v, = d1._name.infer()
|
v, = d1._name.infer()
|
||||||
foo, bar, obj = v.py__mro__()
|
foo, bar, obj = v.py__mro__()
|
||||||
@@ -51,7 +51,7 @@ def test_goto_on_file(Script):
|
|||||||
assert obj.py__name__() == 'object'
|
assert obj.py__name__() == 'object'
|
||||||
|
|
||||||
# Make sure we go to Bar, because Foo is a bit before: `class Foo(Bar):`
|
# Make sure we go to Bar, because Foo is a bit before: `class Foo(Bar):`
|
||||||
script = Script(path=d1.module_path, _project=project)
|
script = Script(path=d1.module_path, project=project)
|
||||||
d2, = script.goto(line=d1.line, column=d1.column + 4)
|
d2, = script.goto(line=d1.line, column=d1.column + 4)
|
||||||
assert d2.name == 'Bar'
|
assert d2.name == 'Bar'
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import pytest
|
|||||||
def ScriptInStubFolder(Script):
|
def ScriptInStubFolder(Script):
|
||||||
path = get_example_dir('stub_packages')
|
path = get_example_dir('stub_packages')
|
||||||
project = Project(path, sys_path=[path], smart_sys_path=False)
|
project = Project(path, sys_path=[path], smart_sys_path=False)
|
||||||
return partial(Script, _project=project)
|
return partial(Script, project=project)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ def test_infer_and_goto(Script, code, full_name, has_stub, has_python, way,
|
|||||||
has_python = False
|
has_python = False
|
||||||
|
|
||||||
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
|
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
|
||||||
s = Script(code, _project=project)
|
s = Script(code, project=project)
|
||||||
prefer_stubs = kwargs['prefer_stubs']
|
prefer_stubs = kwargs['prefer_stubs']
|
||||||
only_stubs = kwargs['only_stubs']
|
only_stubs = kwargs['only_stubs']
|
||||||
|
|
||||||
|
|||||||
@@ -363,7 +363,7 @@ def test_relative_imports_with_multiple_similar_directories(Script, path, empty_
|
|||||||
script = Script(
|
script = Script(
|
||||||
"from . ",
|
"from . ",
|
||||||
path=os.path.join(dir, path),
|
path=os.path.join(dir, path),
|
||||||
_project=project,
|
project=project,
|
||||||
)
|
)
|
||||||
name, import_ = script.complete()
|
name, import_ = script.complete()
|
||||||
assert import_.name == 'import'
|
assert import_.name == 'import'
|
||||||
@@ -376,14 +376,14 @@ def test_relative_imports_with_outside_paths(Script):
|
|||||||
script = Script(
|
script = Script(
|
||||||
"from ...",
|
"from ...",
|
||||||
path=os.path.join(dir, 'api/whatever/test_this.py'),
|
path=os.path.join(dir, 'api/whatever/test_this.py'),
|
||||||
_project=project,
|
project=project,
|
||||||
)
|
)
|
||||||
assert [c.name for c in script.complete()] == ['api', 'whatever']
|
assert [c.name for c in script.complete()] == ['api', 'whatever']
|
||||||
|
|
||||||
script = Script(
|
script = Script(
|
||||||
"from " + '.' * 100,
|
"from " + '.' * 100,
|
||||||
path=os.path.join(dir, 'api/whatever/test_this.py'),
|
path=os.path.join(dir, 'api/whatever/test_this.py'),
|
||||||
_project=project,
|
project=project,
|
||||||
)
|
)
|
||||||
assert not script.complete()
|
assert not script.complete()
|
||||||
|
|
||||||
@@ -391,13 +391,13 @@ def test_relative_imports_with_outside_paths(Script):
|
|||||||
@cwd_at('test/examples/issue1209/api/whatever/')
|
@cwd_at('test/examples/issue1209/api/whatever/')
|
||||||
def test_relative_imports_without_path(Script):
|
def test_relative_imports_without_path(Script):
|
||||||
project = Project('.', sys_path=[], smart_sys_path=False)
|
project = Project('.', sys_path=[], smart_sys_path=False)
|
||||||
script = Script("from . ", _project=project)
|
script = Script("from . ", project=project)
|
||||||
assert [c.name for c in script.complete()] == ['api_test1', 'import']
|
assert [c.name for c in script.complete()] == ['api_test1', 'import']
|
||||||
|
|
||||||
script = Script("from .. ", _project=project)
|
script = Script("from .. ", project=project)
|
||||||
assert [c.name for c in script.complete()] == ['import', 'whatever']
|
assert [c.name for c in script.complete()] == ['import', 'whatever']
|
||||||
|
|
||||||
script = Script("from ... ", _project=project)
|
script = Script("from ... ", project=project)
|
||||||
assert [c.name for c in script.complete()] == ['api', 'import', 'whatever']
|
assert [c.name for c in script.complete()] == ['api', 'import', 'whatever']
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user