diff --git a/jedi/api.py b/jedi/api.py index c3bda853..394df66b 100644 --- a/jedi/api.py +++ b/jedi/api.py @@ -170,7 +170,7 @@ class Script(object): x.word.lower())) def _prepare_goto(self, goto_path, is_like_search=False): - """ Base for complete, goto and get_definition. Basically it returns + """ Base for complete, goto and definition. Basically it returns the resolved scopes under cursor. """ debug.dbg('start: %s in %s' % (goto_path, self._parser.user_scope)) @@ -201,11 +201,20 @@ class Script(object): return stmt def get_definition(self): + """ + .. deprecated:: 0.5.0 + Use :attr:`.function_definition` instead. + .. todo:: Remove! + """ + warnings.warn("Use line instead.", DeprecationWarning) + return self.definition() + + def definition(self): """ Return the definitions of a the path under the cursor. This is not a goto function! This follows complicated paths and returns the end, not the first definition. The big difference between :meth:`goto` and - :meth:`get_definition` is that :meth:`goto` doesn't follow imports and + :meth:`definition` is that :meth:`goto` doesn't follow imports and statements. Multiple objects may be returned, because Python itself is a dynamic language, which means depending on an option you can have two different versions of a function. diff --git a/jedi/api_classes.py b/jedi/api_classes.py index 35c3d76b..a30ea147 100644 --- a/jedi/api_classes.py +++ b/jedi/api_classes.py @@ -263,7 +263,7 @@ class Completion(BaseDefinition): class Definition(BaseDefinition): """ *Definition* objects are returned from :meth:`api.Script.goto` or - :meth:`api.Script.get_definition`. + :meth:`api.Script.definition`. """ def __init__(self, definition): super(Definition, self).__init__(definition, definition.start_pos) diff --git a/test/base.py b/test/base.py index fc78c437..f78e939b 100644 --- a/test/base.py +++ b/test/base.py @@ -32,6 +32,7 @@ sys.argv = sys.argv[:1] + args summary = [] tests_fail = 0 + def get_test_list(): # get test list, that should be executed test_files = {} @@ -46,6 +47,7 @@ def get_test_list(): last = arg return test_files + class TestBase(unittest.TestCase): def get_script(self, src, pos, path=None): if pos is None: @@ -53,9 +55,9 @@ class TestBase(unittest.TestCase): pos = len(lines), len(lines[-1]) return jedi.Script(src, pos[0], pos[1], path) - def get_def(self, src, pos=None): + def definition(self, src, pos=None): script = self.get_script(src, pos) - return script.get_definition() + return script.definition() def complete(self, src, pos=None, path=None): script = self.get_script(src, pos, path) @@ -69,9 +71,9 @@ class TestBase(unittest.TestCase): script = self.get_script(src, pos) return script.function_definition() + def print_summary(): print('\nSummary: (%s fails of %s tests) in %.3fs' % \ (tests_fail, test_sum, time.time() - t_start)) for s in summary: print(s) - diff --git a/test/regression.py b/test/regression.py index 97e9f37b..fdf431f9 100755 --- a/test/regression.py +++ b/test/regression.py @@ -41,7 +41,7 @@ class TestRegression(TestBase): self.function_definition(s, pos) assert self.function_definition(s, pos) - def test_get_definition_cursor(self): + def test_definition_cursor(self): s = ("class A():\n" " def _something(self):\n" @@ -60,7 +60,7 @@ class TestRegression(TestBase): diff_line = 4, 10 should2 = 8, 10 - get_def = lambda pos: [d.description for d in self.get_def(s, pos)] + get_def = lambda pos: [d.description for d in self.definition(s, pos)] in_name = get_def(in_name) under_score = get_def(under_score) should1 = get_def(should1) @@ -71,32 +71,31 @@ class TestRegression(TestBase): assert should1 == in_name assert should1 == under_score - #print should2, diff_line assert should2 == diff_line self.assertRaises(jedi.NotFoundError, get_def, cls) def test_keyword_doc(self): - r = list(self.get_def("or", (1, 1))) + r = list(self.definition("or", (1, 1))) assert len(r) == 1 if not is_py25: assert len(r[0].doc) > 100 - r = list(self.get_def("asfdasfd", (1, 1))) + r = list(self.definition("asfdasfd", (1, 1))) assert len(r) == 0 def test_operator_doc(self): - r = list(self.get_def("a == b", (1, 3))) + r = list(self.definition("a == b", (1, 3))) assert len(r) == 1 if not is_py25: assert len(r[0].doc) > 100 - def test_get_definition_at_zero(self): - assert self.get_def("a", (1, 1)) == [] - s = self.get_def("str", (1, 1)) + def test_definition_at_zero(self): + assert self.definition("a", (1, 1)) == [] + s = self.definition("str", (1, 1)) assert len(s) == 1 assert list(s)[0].description == 'class str' - assert self.get_def("", (1, 0)) == [] + assert self.definition("", (1, 0)) == [] def test_complete_at_zero(self): s = self.complete("str", (1, 3)) @@ -106,9 +105,9 @@ class TestRegression(TestBase): s = self.complete("", (1, 0)) assert len(s) > 0 - def test_get_definition_on_import(self): - assert self.get_def("import sys_blabla", (1, 8)) == [] - assert len(self.get_def("import sys", (1, 8))) == 1 + def test_definition_on_import(self): + assert self.definition("import sys_blabla", (1, 8)) == [] + assert len(self.definition("import sys", (1, 8))) == 1 def test_complete_on_empty_import(self): # should just list the files in the directory @@ -222,15 +221,15 @@ class TestRegression(TestBase): # .parser to load the module api.modules.Module(os.path.abspath('dynamic.py'), src2).parser script = jedi.Script(src1, 1, len(src1), '../setup.py') - result = script.get_definition() + result = script.definition() assert len(result) == 1 assert result[0].description == 'class int' def test_named_import(self): """ named import - jedi-vim issue #8 """ s = "import time as dt" - assert len(jedi.Script(s, 1, 15, '/').get_definition()) == 1 - assert len(jedi.Script(s, 1, 10, '/').get_definition()) == 1 + assert len(jedi.Script(s, 1, 15, '/').definition()) == 1 + assert len(jedi.Script(s, 1, 10, '/').definition()) == 1 def test_unicode_script(self): """ normally no unicode objects are being used. (<=2.7) """ @@ -275,10 +274,10 @@ class TestRegression(TestBase): def test_keyword_definition_doc(self): """ github jedi-vim issue #44 """ - defs = self.get_def("print") + defs = self.definition("print") assert [d.doc for d in defs] - defs = self.get_def("import") + defs = self.definition("import") assert len(defs) == 1 assert [d.doc for d in defs] @@ -325,7 +324,7 @@ class TestFeature(TestBase): assert self.complete('import os; os.path.join')[0].full_name \ == 'os.path.join' # issue #94 - defs = self.get_def("""import os; os.path.join(""") + defs = self.definition("""import os; os.path.join(""") assert defs[0].full_name is None def test_full_name_builtin(self): @@ -336,7 +335,7 @@ class TestFeature(TestBase): import re any_re = re.compile('.*') any_re""" - self.assertEqual(self.get_def(s)[0].full_name, 're.RegexObject') + self.assertEqual(self.definition(s)[0].full_name, 're.RegexObject') def test_quick_completion(self): sources = [ diff --git a/test/run.py b/test/run.py index 67c527f8..b9571a80 100755 --- a/test/run.py +++ b/test/run.py @@ -36,7 +36,7 @@ def run_definition_test(script, should_str, line_nr): Runs tests for definitions. Return if the test was a fail or not, with 1 for fail and 0 for success. """ - result = script.get_definition() + result = script.definition() is_str = set(r.desc_with_module for r in result) if is_str != should_str: print('Solution @%s not right, received %s, wanted %s' \ @@ -120,10 +120,10 @@ def run_test(source, f_name, lines_to_execute): >>> #? int() >>> ab = 3; ab """ - def get_defs(correct, correct_start, path): + def definition(correct, correct_start, path): def defs(line_nr, indent): script = jedi.Script(source, line_nr, indent, path) - return set(script.get_definition()) + return set(script.definition()) should_be = set() number = 0 @@ -166,7 +166,7 @@ def run_test(source, f_name, lines_to_execute): else: index = len(line) - 1 # -1 for the \n # if a list is wanted, use the completion test, otherwise the - # get_definition test + # definition test path = completion_test_dir + os.path.sep + f_name try: script = jedi.Script(source, line_nr, index, path) @@ -177,7 +177,7 @@ def run_test(source, f_name, lines_to_execute): elif correct.startswith('['): fails += run_completion_test(script, correct, line_nr) else: - should_str = get_defs(correct, start, path) + should_str = definition(correct, start, path) fails += run_definition_test(script, should_str, line_nr) except Exception: print(traceback.format_exc())