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
- ``call_signatures`` deprecated, use ``get_signatures`` 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``
- 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.

View File

@@ -150,8 +150,8 @@ Static Analysis
------------------------
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
infer types and so on.
``jedi.Script(...).get_names``. It will return a list of names that you can use
to infer types and so on.
Refactoring

View File

@@ -106,8 +106,8 @@ def Script(environment):
@pytest.fixture(scope='session')
def names(Script):
return lambda code, **kwargs: Script(code).names(**kwargs)
def get_names(Script):
return lambda code, **kwargs: Script(code).get_names(**kwargs)
@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
(: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,
because it's not production ready. If you're interested in helping out here,

View File

@@ -502,7 +502,7 @@ class Script(object):
finally:
self._inference_state.is_analysis = False
def names(self, **kwargs):
def get_names(self, **kwargs):
"""
Returns a list of `Definition` objects, containing name parts.
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,
definitions=True, references=False, environment=None):
warnings.warn(
"Deprecated since version 0.16.0. Use Script(...).names instead.",
"Deprecated since version 0.16.0. Use Script(...).get_names instead.",
DeprecationWarning,
stacklevel=2
)
return Script(source, path=path, encoding=encoding).names(
return Script(source, path=path, encoding=encoding).get_names(
all_scopes=all_scopes,
definitions=definitions,
references=references,

View File

@@ -18,7 +18,7 @@ def test_is_keyword(Script):
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():
"""
Return a list of definitions for parametrized tests.
@@ -43,7 +43,7 @@ def test_basedefinition_type(Script, names):
""")
definitions = []
definitions += names(source)
definitions += get_names(source)
source += dedent("""
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)
def test_param_docstring(names):
param = names("def test(parameter): pass", all_scopes=True)[1]
def test_param_docstring(get_names):
param = get_names("def test(parameter): pass", all_scopes=True)[1]
assert param.name == 'parameter'
assert param.docstring() == ''
@@ -232,8 +232,8 @@ def test_param_endings(Script):
('a = f(x)', 2, 'x', False),
]
)
def test_is_definition(names, code, index, name, is_definition):
d = names(
def test_is_definition(get_names, code, index, name, is_definition):
d = get_names(
dedent(code),
references=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]),
)
)
def test_is_definition_import(names, code, expected):
ns = names(dedent(code), references=True, all_scopes=True)
def test_is_definition_import(get_names, code, expected):
ns = get_names(dedent(code), references=True, all_scopes=True)
# Assure that names are definitely sorted.
ns = sorted(ns, key=lambda name: (name.line, name.column))
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.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']
parent = name.parent()
assert parent.name == 'bar'
@@ -320,7 +320,7 @@ def test_parent_on_closure(Script):
def inner(): foo
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)
foo, = filter(lambda d: d.name == 'foo', names)
@@ -339,7 +339,7 @@ def test_parent_on_comprehension(Script):
ns = Script('''\
def spam():
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']
@@ -380,8 +380,8 @@ different as an implementation.
"""
def test_goto_repetition(names):
defs = names('a = 1; a', references=True, definitions=False)
def test_goto_repetition(get_names):
defs = get_names('a = 1; a', references=True, definitions=False)
# Repeat on the same variable. Shouldn't change once we're on a
# definition.
for _ in range(3):
@@ -390,34 +390,34 @@ def test_goto_repetition(names):
assert ass[0].description == 'a = 1'
def test_goto_named_params(names):
def test_goto_named_params(get_names):
src = """\
def foo(a=1, bar=2):
pass
foo(bar=1)
"""
bar = names(dedent(src), references=True)[-1]
bar = get_names(dedent(src), references=True)[-1]
param = bar.goto()[0]
assert (param.line, param.column) == (1, 13)
assert param.type == 'param'
def test_class_call(names):
def test_class_call(get_names):
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'
param_def = n.goto()[0]
assert param_def.name == 'group'
assert param_def.type == 'param'
def test_parentheses(names):
n = names('("").upper', references=True)[-1]
def test_parentheses(get_names):
n = get_names('("").upper', references=True)[-1]
assert n.goto()[0].name == 'upper'
def test_import(names):
nms = names('from json import load', references=True)
def test_import(get_names):
nms = get_names('from json import load', references=True)
assert nms[0].name == 'json'
assert nms[0].type == 'module'
n = nms[0].goto()[0]
@@ -430,7 +430,7 @@ def test_import(names):
assert n.name == 'load'
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].type == 'module'
n = nms[0].goto()[0]
@@ -442,7 +442,7 @@ def test_import(names):
assert all(n.type == 'module' 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]
assert n.name == 'os'
assert n.type == 'module'
@@ -453,8 +453,8 @@ def test_import(names):
assert n.type == 'module'
def test_import_alias(names):
nms = names('import json as foo', references=True)
def test_import_alias(get_names):
nms = get_names('import json as foo', references=True)
assert nms[0].name == 'json'
assert nms[0].type == 'module'
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
def _assert_definition_names(definitions, names_):
assert [d.name for d in definitions] == names_
def _assert_definition_names(definitions, names):
assert [d.name for d in definitions] == names
def _check_names(names, source, names_):
definitions = names(dedent(source))
_assert_definition_names(definitions, names_)
def _check_names(get_names, source, names):
definitions = get_names(dedent(source))
_assert_definition_names(definitions, names)
return definitions
def test_get_definitions_flat(names):
_check_names(names, """
def test_get_definitions_flat(get_names):
_check_names(get_names, """
import module
class Class:
pass
@@ -26,26 +26,26 @@ def test_get_definitions_flat(names):
""", ['module', 'Class', 'func', 'data'])
def test_dotted_assignment(names):
_check_names(names, """
def test_dotted_assignment(get_names):
_check_names(get_names, """
x = Class()
x.y.z = None
""", ['x', 'z']) # TODO is this behavior what we want?
def test_multiple_assignment(names):
_check_names(names, "x = y = None", ['x', 'y'])
def test_multiple_assignment(get_names):
_check_names(get_names, "x = y = None", ['x', 'y'])
def test_multiple_imports(names):
_check_names(names, """
def test_multiple_imports(get_names):
_check_names(get_names, """
from module import a, b
from another_module import *
""", ['a', 'b'])
def test_nested_definitions(names):
definitions = _check_names(names, """
def test_nested_definitions(get_names):
definitions = _check_names(get_names, """
class Class:
def f():
pass
@@ -57,8 +57,8 @@ def test_nested_definitions(names):
assert [d.full_name for d in subdefinitions] == ['__main__.Class.f', '__main__.Class.g']
def test_nested_class(names):
definitions = _check_names(names, """
def test_nested_class(get_names):
definitions = _check_names(get_names, """
class L1:
class L2:
class L3:
@@ -74,8 +74,8 @@ def test_nested_class(names):
_assert_definition_names(subsubdefs[0].defined_names(), ['f'])
def test_class_fields_with_all_scopes_false(names):
definitions = _check_names(names, """
def test_class_fields_with_all_scopes_false(get_names):
definitions = _check_names(get_names, """
from module import f
g = f(f)
class C:
@@ -91,8 +91,8 @@ def test_class_fields_with_all_scopes_false(names):
_assert_definition_names(foo_subdefs, ['x', 'bar'])
def test_async_stmt_with_all_scopes_false(names):
definitions = _check_names(names, """
def test_async_stmt_with_all_scopes_false(get_names):
definitions = _check_names(get_names, """
from module import f
import asyncio
@@ -130,30 +130,30 @@ def test_async_stmt_with_all_scopes_false(names):
_assert_definition_names(cinst_subdefs, [])
def test_follow_imports(names):
def test_follow_imports(get_names):
# github issue #344
imp = names('import datetime')[0]
imp = get_names('import datetime')[0]
assert imp.name == 'datetime'
datetime_names = [str(d.name) for d in imp.defined_names()]
assert 'timedelta' in datetime_names
def test_names_twice(names):
def test_names_twice(get_names):
code = dedent('''
def lol():
pass
''')
defs = names(code)
defs = get_names(code)
assert defs[0].defined_names() == []
def test_simple_name(names):
defs = names('foo', references=True)
def test_simple_name(get_names):
defs = get_names('foo', references=True)
assert not defs[0]._name.infer()
def test_no_error(names):
def test_no_error(get_names):
code = dedent("""
def foo(a, b):
if a == 10:
@@ -161,7 +161,7 @@ def test_no_error(names):
print("foo")
a = 20
""")
func_name, = names(code)
func_name, = get_names(code)
a, b, a20 = func_name.defined_names()
assert a.name == 'a'
assert b.name == 'b'

View File

@@ -64,7 +64,7 @@ class TestFullNameWithCompletions(MixinTestFullName, 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)
def init(self, environment):
@@ -72,7 +72,7 @@ class TestFullDefinedName(TestCase):
def check(self, source, desired):
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]
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()
def test_cropped_file_size(monkeypatch, names, Script):
def test_cropped_file_size(monkeypatch, get_names, Script):
code = 'class Foo(): pass\n'
monkeypatch.setattr(
settings,
@@ -42,7 +42,7 @@ def test_cropped_file_size(monkeypatch, names, Script):
len(code)
)
foo, = names(code + code)
foo, = get_names(code + code)
assert foo.line == 1
# It should just not crash if we are outside of the cropped range.