Make the Project API public, fixes #778

This commit is contained in:
Dave Halter
2020-01-30 19:18:41 +01:00
parent e930f47861
commit ceccbf3678
7 changed files with 22 additions and 16 deletions

View File

@@ -41,6 +41,7 @@ from jedi import settings
from jedi.api.environment import find_virtualenvs, find_system_environments, \
get_default_environment, InvalidPythonEnvironment, create_environment, \
get_system_environment
from jedi.api.project import Project
from jedi.api.exceptions import InternalError
# Finally load the internal plugins. This is only internal.
from jedi.plugins import registry

View File

@@ -87,7 +87,7 @@ class Script(object):
"""
def __init__(self, source=None, line=None, column=None, path=None,
encoding='utf-8', sys_path=None, environment=None,
_project=None):
project=None):
self._orig_path = path
# An empty path (also empty string) should always result in no path.
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:
sys_path = list(map(force_unicode, sys_path))
project = _project
if project is None:
# Load the Python grammar of the current interpreter.
project = get_default_project(
@@ -112,6 +111,12 @@ class Script(object):
# TODO deprecate and remove sys_path from the Script API.
if sys_path is not None:
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(
project, environment=environment, script_path=self.path
)
@@ -559,7 +564,7 @@ class Interpreter(Script):
raise TypeError("The environment needs to be an InterpreterEnvironment subclass.")
super(Interpreter, self).__init__(source, environment=environment,
_project=Project(os.getcwd()), **kwds)
project=Project(os.getcwd()), **kwds)
self.namespaces = namespaces
self._inference_state.allow_descriptor_getattr = self._allow_descriptor_getattr_default

View File

@@ -6,7 +6,7 @@ def test_import_references(Script):
def test_exclude_builtin_modules(Script):
def get(include):
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)
return [(d.line, d.column) for d in references]
source = '''import sys\nprint(sys.path)'''

View File

@@ -28,10 +28,10 @@ def test_sqlite3_conversion(Script):
def test_conversion_of_stub_only(Script):
project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
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()
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)
assert d2.is_stub()
assert d2.module_path == d1.module_path
@@ -42,7 +42,7 @@ def test_conversion_of_stub_only(Script):
def test_goto_on_file(Script):
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()
v, = d1._name.infer()
foo, bar, obj = v.py__mro__()
@@ -51,7 +51,7 @@ def test_goto_on_file(Script):
assert obj.py__name__() == 'object'
# 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)
assert d2.name == 'Bar'

View File

@@ -9,7 +9,7 @@ import pytest
def ScriptInStubFolder(Script):
path = get_example_dir('stub_packages')
project = Project(path, sys_path=[path], smart_sys_path=False)
return partial(Script, _project=project)
return partial(Script, project=project)
@pytest.mark.parametrize(

View File

@@ -54,7 +54,7 @@ def test_infer_and_goto(Script, code, full_name, has_stub, has_python, way,
has_python = False
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']
only_stubs = kwargs['only_stubs']

View File

@@ -363,7 +363,7 @@ def test_relative_imports_with_multiple_similar_directories(Script, path, empty_
script = Script(
"from . ",
path=os.path.join(dir, path),
_project=project,
project=project,
)
name, import_ = script.complete()
assert import_.name == 'import'
@@ -376,14 +376,14 @@ def test_relative_imports_with_outside_paths(Script):
script = Script(
"from ...",
path=os.path.join(dir, 'api/whatever/test_this.py'),
_project=project,
project=project,
)
assert [c.name for c in script.complete()] == ['api', 'whatever']
script = Script(
"from " + '.' * 100,
path=os.path.join(dir, 'api/whatever/test_this.py'),
_project=project,
project=project,
)
assert not script.complete()
@@ -391,13 +391,13 @@ def test_relative_imports_with_outside_paths(Script):
@cwd_at('test/examples/issue1209/api/whatever/')
def test_relative_imports_without_path(Script):
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']
script = Script("from .. ", _project=project)
script = Script("from .. ", project=project)
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']