mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 23:04:48 +08:00
deprecate 'source_path' as a Script parameter in favour of 'path'
This commit is contained in:
34
jedi/api.py
34
jedi/api.py
@@ -46,15 +46,19 @@ class Script(object):
|
||||
:type line: int
|
||||
:param col: The column of the cursor (starting with 0).
|
||||
:type col: int
|
||||
:param source_path: The path of the file in the file system, or ``''`` if
|
||||
:param path: The path of the file in the file system, or ``''`` if
|
||||
it hasn't been saved yet.
|
||||
:type source_path: str or None
|
||||
:type path: str or None
|
||||
:param source_encoding: The encoding of ``source``, if it is not a
|
||||
``unicode`` object (default ``'utf-8'``).
|
||||
:type source_encoding: str
|
||||
"""
|
||||
def __init__(self, source, line=None, column=None, source_path=None,
|
||||
source_encoding='utf-8'):
|
||||
def __init__(self, source, line=None, column=None, path=None,
|
||||
source_encoding='utf-8', source_path=None):
|
||||
if source_path is not None:
|
||||
warnings.warn("Use path instead of source_path.", DeprecationWarning)
|
||||
path = source_path
|
||||
|
||||
lines = source.splitlines()
|
||||
line = len(lines) if line is None else line
|
||||
column = len(lines[-1]) if column is None else column
|
||||
@@ -64,12 +68,21 @@ class Script(object):
|
||||
self.source = modules.source_to_unicode(source, source_encoding)
|
||||
self.pos = line, column
|
||||
self._module = modules.ModuleWithCursor(
|
||||
source_path, source=self.source, position=self.pos)
|
||||
self._source_path = source_path
|
||||
self.source_path = None if source_path is None \
|
||||
else os.path.abspath(source_path)
|
||||
path, source=self.source, position=self.pos)
|
||||
self._source_path = path
|
||||
self.path = None if path is None else os.path.abspath(path)
|
||||
debug.speed('init')
|
||||
|
||||
@property
|
||||
def source_path(self):
|
||||
"""
|
||||
.. deprecated:: 0.7.0
|
||||
Use :attr:`.path` instead.
|
||||
.. todo:: Remove!
|
||||
"""
|
||||
warnings.warn("Use path instead of source_path.", DeprecationWarning)
|
||||
return self.path
|
||||
|
||||
def __repr__(self):
|
||||
return '<%s: %s>' % (self.__class__.__name__, repr(self._source_path))
|
||||
|
||||
@@ -556,12 +569,13 @@ class Interpreter(Script):
|
||||
"""
|
||||
super(Interpreter, self).__init__(source, **kwds)
|
||||
|
||||
# Here we add the namespaces to the current parser.
|
||||
importer = interpret.ObjectImporter(self._parser.user_scope)
|
||||
for ns in namespaces:
|
||||
importer.import_raw_namespace(ns)
|
||||
|
||||
|
||||
def defined_names(source, source_path=None, source_encoding='utf-8'):
|
||||
def defined_names(source, path=None, source_encoding='utf-8'):
|
||||
"""
|
||||
Get all definitions in `source` sorted by its position.
|
||||
|
||||
@@ -575,7 +589,7 @@ def defined_names(source, source_path=None, source_encoding='utf-8'):
|
||||
"""
|
||||
parser = parsing.Parser(
|
||||
modules.source_to_unicode(source, source_encoding),
|
||||
module_path=source_path,
|
||||
module_path=path,
|
||||
)
|
||||
return api_classes._defined_names(parser.module)
|
||||
|
||||
|
||||
@@ -155,7 +155,7 @@ def extract(script, new_name):
|
||||
indent = user_stmt.start_pos[1]
|
||||
new = "%s%s = %s" % (' ' * indent, new_name, text)
|
||||
new_lines.insert(line_index, new)
|
||||
dct[script.source_path] = script.source_path, old_lines, new_lines
|
||||
dct[script.path] = script.path, old_lines, new_lines
|
||||
return Refactoring(dct)
|
||||
|
||||
|
||||
@@ -196,7 +196,7 @@ def inline(script):
|
||||
|
||||
dct = _rename(inlines, replace_str)
|
||||
# remove the empty line
|
||||
new_lines = dct[script.source_path][2]
|
||||
new_lines = dct[script.path][2]
|
||||
if line.strip():
|
||||
new_lines[index] = line
|
||||
else:
|
||||
|
||||
2
test/completion/thirdparty/jedi_.py
vendored
2
test/completion/thirdparty/jedi_.py
vendored
@@ -10,7 +10,7 @@ el.description
|
||||
|
||||
|
||||
scopes, path, dot, like = \
|
||||
api._prepare_goto(source, row, column, source_path, True)
|
||||
api._prepare_goto(source, row, column, path, True)
|
||||
|
||||
# has problems with that (sometimes) very deep nesting.
|
||||
#? set()
|
||||
|
||||
@@ -178,7 +178,7 @@ class IntegrationTestCase(object):
|
||||
return should_str
|
||||
|
||||
script = self.script()
|
||||
should_str = definition(self.correct, self.start, script.source_path)
|
||||
should_str = definition(self.correct, self.start, script.path)
|
||||
result = script.goto_definitions()
|
||||
is_str = set(r.desc_with_module for r in result)
|
||||
return compare_cb(self, is_str, should_str)
|
||||
|
||||
@@ -17,11 +17,11 @@ def test_goto_definition_on_import():
|
||||
@cwd_at('jedi')
|
||||
def test_complete_on_empty_import():
|
||||
# should just list the files in the directory
|
||||
assert 10 < len(Script("from .", source_path='').completions()) < 30
|
||||
assert 10 < len(Script("from .", path='').completions()) < 30
|
||||
assert 10 < len(Script("from . import", 1, 5, '').completions()) < 30
|
||||
assert 10 < len(Script("from . import classes", 1, 5, '').completions()) < 30
|
||||
assert len(Script("import").completions()) == 0
|
||||
assert len(Script("import import", source_path='').completions()) > 0
|
||||
assert len(Script("import import", path='').completions()) > 0
|
||||
|
||||
# 111
|
||||
assert Script("from datetime import").completions()[0].name == 'import'
|
||||
@@ -32,11 +32,11 @@ def test_imports_on_global_namespace_without_path():
|
||||
"""If the path is None, there shouldn't be any import problem"""
|
||||
completions = Script("import operator").completions()
|
||||
assert [c.name for c in completions] == ['operator']
|
||||
completions = Script("import operator", source_path= 'example.py').completions()
|
||||
completions = Script("import operator", path= 'example.py').completions()
|
||||
assert [c.name for c in completions] == ['operator']
|
||||
|
||||
# the first one has a path the second doesn't
|
||||
completions = Script("import keyword", source_path='example.py').completions()
|
||||
completions = Script("import keyword", path='example.py').completions()
|
||||
assert [c.name for c in completions] == ['keyword']
|
||||
completions = Script("import keyword").completions()
|
||||
assert [c.name for c in completions] == ['keyword']
|
||||
|
||||
@@ -81,7 +81,7 @@ class TestRegression(TestCase):
|
||||
src2 = 'from .. import setup; setup.ret(1)'
|
||||
# .parser to load the module
|
||||
api.modules.Module(os.path.abspath('dynamic.py'), src2).parser
|
||||
result = Script(src1, source_path='../setup.py').goto_definitions()
|
||||
result = Script(src1, path='../setup.py').goto_definitions()
|
||||
assert len(result) == 1
|
||||
assert result[0].description == 'class int'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user