1
0
forked from VimPlug/jedi

Rename Script.names to Script.get_names, fixes #1476

This commit is contained in:
Dave Halter
2020-01-22 01:22:46 +01:00
parent 8cc836e816
commit bff6e95e28
9 changed files with 67 additions and 67 deletions

View File

@@ -24,7 +24,7 @@ Changelog
- ``goto_definitions`` deprecated, use ``infer`` instead - ``goto_definitions`` deprecated, use ``infer`` instead
- ``call_signatures`` deprecated, use ``get_signatures`` instead - ``call_signatures`` deprecated, use ``get_signatures`` instead
- ``usages`` deprecated, use ``get_references`` instead - ``usages`` deprecated, use ``get_references`` instead
- ``jedi.names`` deprecated, use ``jedi.Script(...).names()`` - ``jedi.names`` deprecated, use ``jedi.Script(...).get_names()``
- ``BaseDefinition.goto_assignments`` renamed to ``BaseDefinition.goto`` - ``BaseDefinition.goto_assignments`` renamed to ``BaseDefinition.goto``
- Python 2 support deprecated. For this release it is best effort. Python 2 has - Python 2 support deprecated. For this release it is best effort. Python 2 has
reached the end of its life and now it's just about a smooth transition. reached the end of its life and now it's just about a smooth transition.

View File

@@ -150,8 +150,8 @@ Static Analysis
------------------------ ------------------------
To do all forms of static analysis, please try to use To do all forms of static analysis, please try to use
``jedi.Script(...).names``. It will return a list of names that you can use to ``jedi.Script(...).get_names``. It will return a list of names that you can use
infer types and so on. to infer types and so on.
Refactoring Refactoring

View File

@@ -106,8 +106,8 @@ def Script(environment):
@pytest.fixture(scope='session') @pytest.fixture(scope='session')
def names(Script): def get_names(Script):
return lambda code, **kwargs: Script(code).names(**kwargs) return lambda code, **kwargs: Script(code).get_names(**kwargs)
@pytest.fixture(scope='session', params=['goto', 'infer']) @pytest.fixture(scope='session', params=['goto', 'infer'])

View File

@@ -6,7 +6,7 @@ Features and Caveats
Jedi obviously supports autocompletion. It's also possible to get it working in Jedi obviously supports autocompletion. It's also possible to get it working in
(:ref:`your REPL (IPython, etc.) <repl-completion>`). (:ref:`your REPL (IPython, etc.) <repl-completion>`).
Static analysis is also possible by using ``jedi.Script(...).names``. Static analysis is also possible by using ``jedi.Script(...).get_names``.
Jedi would in theory support refactoring, but we have never publicized it, Jedi would in theory support refactoring, but we have never publicized it,
because it's not production ready. If you're interested in helping out here, because it's not production ready. If you're interested in helping out here,

View File

@@ -502,7 +502,7 @@ class Script(object):
finally: finally:
self._inference_state.is_analysis = False self._inference_state.is_analysis = False
def names(self, **kwargs): def get_names(self, **kwargs):
""" """
Returns a list of `Definition` objects, containing name parts. Returns a list of `Definition` objects, containing name parts.
This means you can call ``Definition.goto()`` and get the This means you can call ``Definition.goto()`` and get the
@@ -598,12 +598,12 @@ class Interpreter(Script):
def names(source=None, path=None, encoding='utf-8', all_scopes=False, def names(source=None, path=None, encoding='utf-8', all_scopes=False,
definitions=True, references=False, environment=None): definitions=True, references=False, environment=None):
warnings.warn( warnings.warn(
"Deprecated since version 0.16.0. Use Script(...).names instead.", "Deprecated since version 0.16.0. Use Script(...).get_names instead.",
DeprecationWarning, DeprecationWarning,
stacklevel=2 stacklevel=2
) )
return Script(source, path=path, encoding=encoding).names( return Script(source, path=path, encoding=encoding).get_names(
all_scopes=all_scopes, all_scopes=all_scopes,
definitions=definitions, definitions=definitions,
references=references, references=references,

View File

@@ -18,7 +18,7 @@ def test_is_keyword(Script):
assert len(results) == 1 and results[0].is_keyword is False assert len(results) == 1 and results[0].is_keyword is False
def test_basedefinition_type(Script, names): def test_basedefinition_type(Script, get_names):
def make_definitions(): def make_definitions():
""" """
Return a list of definitions for parametrized tests. Return a list of definitions for parametrized tests.
@@ -43,7 +43,7 @@ def test_basedefinition_type(Script, names):
""") """)
definitions = [] definitions = []
definitions += names(source) definitions += get_names(source)
source += dedent(""" source += dedent("""
variable = sys or C or x or f or g or g() or h""") variable = sys or C or x or f or g or g() or h""")
@@ -101,8 +101,8 @@ def test_function_signature_in_doc(Script):
assert "f(x, y=1, z='a')" in str(doc) assert "f(x, y=1, z='a')" in str(doc)
def test_param_docstring(names): def test_param_docstring(get_names):
param = names("def test(parameter): pass", all_scopes=True)[1] param = get_names("def test(parameter): pass", all_scopes=True)[1]
assert param.name == 'parameter' assert param.name == 'parameter'
assert param.docstring() == '' assert param.docstring() == ''
@@ -232,8 +232,8 @@ def test_param_endings(Script):
('a = f(x)', 2, 'x', False), ('a = f(x)', 2, 'x', False),
] ]
) )
def test_is_definition(names, code, index, name, is_definition): def test_is_definition(get_names, code, index, name, is_definition):
d = names( d = get_names(
dedent(code), dedent(code),
references=True, references=True,
all_scopes=True, all_scopes=True,
@@ -249,8 +249,8 @@ def test_is_definition(names, code, index, name, is_definition):
('from x.z import y', [False, False, True]), ('from x.z import y', [False, False, True]),
) )
) )
def test_is_definition_import(names, code, expected): def test_is_definition_import(get_names, code, expected):
ns = names(dedent(code), references=True, all_scopes=True) ns = get_names(dedent(code), references=True, all_scopes=True)
# Assure that names are definitely sorted. # Assure that names are definitely sorted.
ns = sorted(ns, key=lambda name: (name.line, name.column)) ns = sorted(ns, key=lambda name: (name.line, name.column))
assert [name.is_definition() for name in ns] == expected assert [name.is_definition() for name in ns] == expected
@@ -292,7 +292,7 @@ def test_parent_on_completion_and_else(Script):
assert parent.name == 'Foo' assert parent.name == 'Foo'
assert parent.type == 'class' assert parent.type == 'class'
param, name, = [d for d in script.names(all_scopes=True, references=True) param, name, = [d for d in script.get_names(all_scopes=True, references=True)
if d.name == 'name'] if d.name == 'name']
parent = name.parent() parent = name.parent()
assert parent.name == 'bar' assert parent.name == 'bar'
@@ -320,7 +320,7 @@ def test_parent_on_closure(Script):
def inner(): foo def inner(): foo
return inner''')) return inner'''))
names = script.names(all_scopes=True, references=True) names = script.get_names(all_scopes=True, references=True)
inner_func, inner_reference = filter(lambda d: d.name == 'inner', names) inner_func, inner_reference = filter(lambda d: d.name == 'inner', names)
foo, = filter(lambda d: d.name == 'foo', names) foo, = filter(lambda d: d.name == 'foo', names)
@@ -339,7 +339,7 @@ def test_parent_on_comprehension(Script):
ns = Script('''\ ns = Script('''\
def spam(): def spam():
return [i for i in range(5)] return [i for i in range(5)]
''').names(all_scopes=True) ''').get_names(all_scopes=True)
assert [name.name for name in ns] == ['spam', 'i'] assert [name.name for name in ns] == ['spam', 'i']
@@ -380,8 +380,8 @@ different as an implementation.
""" """
def test_goto_repetition(names): def test_goto_repetition(get_names):
defs = names('a = 1; a', references=True, definitions=False) defs = get_names('a = 1; a', references=True, definitions=False)
# Repeat on the same variable. Shouldn't change once we're on a # Repeat on the same variable. Shouldn't change once we're on a
# definition. # definition.
for _ in range(3): for _ in range(3):
@@ -390,34 +390,34 @@ def test_goto_repetition(names):
assert ass[0].description == 'a = 1' assert ass[0].description == 'a = 1'
def test_goto_named_params(names): def test_goto_named_params(get_names):
src = """\ src = """\
def foo(a=1, bar=2): def foo(a=1, bar=2):
pass pass
foo(bar=1) foo(bar=1)
""" """
bar = names(dedent(src), references=True)[-1] bar = get_names(dedent(src), references=True)[-1]
param = bar.goto()[0] param = bar.goto()[0]
assert (param.line, param.column) == (1, 13) assert (param.line, param.column) == (1, 13)
assert param.type == 'param' assert param.type == 'param'
def test_class_call(names): def test_class_call(get_names):
src = 'from threading import Thread; Thread(group=1)' src = 'from threading import Thread; Thread(group=1)'
n = names(src, references=True)[-1] n = get_names(src, references=True)[-1]
assert n.name == 'group' assert n.name == 'group'
param_def = n.goto()[0] param_def = n.goto()[0]
assert param_def.name == 'group' assert param_def.name == 'group'
assert param_def.type == 'param' assert param_def.type == 'param'
def test_parentheses(names): def test_parentheses(get_names):
n = names('("").upper', references=True)[-1] n = get_names('("").upper', references=True)[-1]
assert n.goto()[0].name == 'upper' assert n.goto()[0].name == 'upper'
def test_import(names): def test_import(get_names):
nms = names('from json import load', references=True) nms = get_names('from json import load', references=True)
assert nms[0].name == 'json' assert nms[0].name == 'json'
assert nms[0].type == 'module' assert nms[0].type == 'module'
n = nms[0].goto()[0] n = nms[0].goto()[0]
@@ -430,7 +430,7 @@ def test_import(names):
assert n.name == 'load' assert n.name == 'load'
assert n.type == 'function' assert n.type == 'function'
nms = names('import os; os.path', references=True) nms = get_names('import os; os.path', references=True)
assert nms[0].name == 'os' assert nms[0].name == 'os'
assert nms[0].type == 'module' assert nms[0].type == 'module'
n = nms[0].goto()[0] n = nms[0].goto()[0]
@@ -442,7 +442,7 @@ def test_import(names):
assert all(n.type == 'module' for n in nms) assert all(n.type == 'module' for n in nms)
assert 'posixpath' in {n.name for n in nms} assert 'posixpath' in {n.name for n in nms}
nms = names('import os.path', references=True) nms = get_names('import os.path', references=True)
n = nms[0].goto()[0] n = nms[0].goto()[0]
assert n.name == 'os' assert n.name == 'os'
assert n.type == 'module' assert n.type == 'module'
@@ -453,8 +453,8 @@ def test_import(names):
assert n.type == 'module' assert n.type == 'module'
def test_import_alias(names): def test_import_alias(get_names):
nms = names('import json as foo', references=True) nms = get_names('import json as foo', references=True)
assert nms[0].name == 'json' assert nms[0].name == 'json'
assert nms[0].type == 'module' assert nms[0].type == 'module'
assert nms[0]._name.tree_name.parent.type == 'dotted_as_name' assert nms[0]._name.tree_name.parent.type == 'dotted_as_name'

View File

@@ -5,18 +5,18 @@ Tests for `api.names`.
from textwrap import dedent from textwrap import dedent
def _assert_definition_names(definitions, names_): def _assert_definition_names(definitions, names):
assert [d.name for d in definitions] == names_ assert [d.name for d in definitions] == names
def _check_names(names, source, names_): def _check_names(get_names, source, names):
definitions = names(dedent(source)) definitions = get_names(dedent(source))
_assert_definition_names(definitions, names_) _assert_definition_names(definitions, names)
return definitions return definitions
def test_get_definitions_flat(names): def test_get_definitions_flat(get_names):
_check_names(names, """ _check_names(get_names, """
import module import module
class Class: class Class:
pass pass
@@ -26,26 +26,26 @@ def test_get_definitions_flat(names):
""", ['module', 'Class', 'func', 'data']) """, ['module', 'Class', 'func', 'data'])
def test_dotted_assignment(names): def test_dotted_assignment(get_names):
_check_names(names, """ _check_names(get_names, """
x = Class() x = Class()
x.y.z = None x.y.z = None
""", ['x', 'z']) # TODO is this behavior what we want? """, ['x', 'z']) # TODO is this behavior what we want?
def test_multiple_assignment(names): def test_multiple_assignment(get_names):
_check_names(names, "x = y = None", ['x', 'y']) _check_names(get_names, "x = y = None", ['x', 'y'])
def test_multiple_imports(names): def test_multiple_imports(get_names):
_check_names(names, """ _check_names(get_names, """
from module import a, b from module import a, b
from another_module import * from another_module import *
""", ['a', 'b']) """, ['a', 'b'])
def test_nested_definitions(names): def test_nested_definitions(get_names):
definitions = _check_names(names, """ definitions = _check_names(get_names, """
class Class: class Class:
def f(): def f():
pass pass
@@ -57,8 +57,8 @@ def test_nested_definitions(names):
assert [d.full_name for d in subdefinitions] == ['__main__.Class.f', '__main__.Class.g'] assert [d.full_name for d in subdefinitions] == ['__main__.Class.f', '__main__.Class.g']
def test_nested_class(names): def test_nested_class(get_names):
definitions = _check_names(names, """ definitions = _check_names(get_names, """
class L1: class L1:
class L2: class L2:
class L3: class L3:
@@ -74,8 +74,8 @@ def test_nested_class(names):
_assert_definition_names(subsubdefs[0].defined_names(), ['f']) _assert_definition_names(subsubdefs[0].defined_names(), ['f'])
def test_class_fields_with_all_scopes_false(names): def test_class_fields_with_all_scopes_false(get_names):
definitions = _check_names(names, """ definitions = _check_names(get_names, """
from module import f from module import f
g = f(f) g = f(f)
class C: class C:
@@ -91,8 +91,8 @@ def test_class_fields_with_all_scopes_false(names):
_assert_definition_names(foo_subdefs, ['x', 'bar']) _assert_definition_names(foo_subdefs, ['x', 'bar'])
def test_async_stmt_with_all_scopes_false(names): def test_async_stmt_with_all_scopes_false(get_names):
definitions = _check_names(names, """ definitions = _check_names(get_names, """
from module import f from module import f
import asyncio import asyncio
@@ -130,30 +130,30 @@ def test_async_stmt_with_all_scopes_false(names):
_assert_definition_names(cinst_subdefs, []) _assert_definition_names(cinst_subdefs, [])
def test_follow_imports(names): def test_follow_imports(get_names):
# github issue #344 # github issue #344
imp = names('import datetime')[0] imp = get_names('import datetime')[0]
assert imp.name == 'datetime' assert imp.name == 'datetime'
datetime_names = [str(d.name) for d in imp.defined_names()] datetime_names = [str(d.name) for d in imp.defined_names()]
assert 'timedelta' in datetime_names assert 'timedelta' in datetime_names
def test_names_twice(names): def test_names_twice(get_names):
code = dedent(''' code = dedent('''
def lol(): def lol():
pass pass
''') ''')
defs = names(code) defs = get_names(code)
assert defs[0].defined_names() == [] assert defs[0].defined_names() == []
def test_simple_name(names): def test_simple_name(get_names):
defs = names('foo', references=True) defs = get_names('foo', references=True)
assert not defs[0]._name.infer() assert not defs[0]._name.infer()
def test_no_error(names): def test_no_error(get_names):
code = dedent(""" code = dedent("""
def foo(a, b): def foo(a, b):
if a == 10: if a == 10:
@@ -161,7 +161,7 @@ def test_no_error(names):
print("foo") print("foo")
a = 20 a = 20
""") """)
func_name, = names(code) func_name, = get_names(code)
a, b, a20 = func_name.defined_names() a, b, a20 = func_name.defined_names()
assert a.name == 'a' assert a.name == 'a'
assert b.name == 'b' assert b.name == 'b'

View File

@@ -64,7 +64,7 @@ class TestFullNameWithCompletions(MixinTestFullName, TestCase):
class TestFullDefinedName(TestCase): class TestFullDefinedName(TestCase):
""" """
Test combination of ``obj.full_name`` and ``jedi.defined_names``. Test combination of ``obj.full_name`` and ``jedi.Script.get_names``.
""" """
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def init(self, environment): def init(self, environment):
@@ -72,7 +72,7 @@ class TestFullDefinedName(TestCase):
def check(self, source, desired): def check(self, source, desired):
script = jedi.Script(textwrap.dedent(source), environment=self.environment) script = jedi.Script(textwrap.dedent(source), environment=self.environment)
definitions = script.names() definitions = script.get_names()
full_names = [d.full_name for d in definitions] full_names = [d.full_name for d in definitions]
self.assertEqual(full_names, desired) self.assertEqual(full_names, desired)

View File

@@ -34,7 +34,7 @@ def test_additional_dynamic_modules(monkeypatch, Script):
assert not Script('def some_func(f):\n f.').complete() assert not Script('def some_func(f):\n f.').complete()
def test_cropped_file_size(monkeypatch, names, Script): def test_cropped_file_size(monkeypatch, get_names, Script):
code = 'class Foo(): pass\n' code = 'class Foo(): pass\n'
monkeypatch.setattr( monkeypatch.setattr(
settings, settings,
@@ -42,7 +42,7 @@ def test_cropped_file_size(monkeypatch, names, Script):
len(code) len(code)
) )
foo, = names(code + code) foo, = get_names(code + code)
assert foo.line == 1 assert foo.line == 1
# It should just not crash if we are outside of the cropped range. # It should just not crash if we are outside of the cropped range.