diff --git a/jedi/__init__.py b/jedi/__init__.py index 0c29bca1..9c37f07c 100644 --- a/jedi/__init__.py +++ b/jedi/__init__.py @@ -43,7 +43,7 @@ import sys sys.path.insert(0, __path__[0]) from .api import Script, Interpreter, NotFoundError, set_debug_function, \ - preload_module + preload_module, defined_names from . import settings sys.path.pop(0) diff --git a/jedi/api.py b/jedi/api.py index e148f350..5d2095e4 100644 --- a/jedi/api.py +++ b/jedi/api.py @@ -563,7 +563,7 @@ def defined_names(source, source_path=None, source_encoding='utf-8'): modules.source_to_unicode(source, source_encoding), module_path=source_path, ) - return api_classes._defined_names(parser.scope) + return api_classes._defined_names(parser.module) def preload_module(*modules): diff --git a/test/test_defined_names.py b/test/test_defined_names.py new file mode 100644 index 00000000..aeb13972 --- /dev/null +++ b/test/test_defined_names.py @@ -0,0 +1,57 @@ +""" +Tests for `api.defined_names`. +""" + +import textwrap + +from jedi import api +from .base import TestBase + + +class TestDefinedNames(TestBase): + + def check_defined_names(self, source, names): + definitions = api.defined_names(textwrap.dedent(source)) + self.assertEqual([d.name for d in definitions], names) + return definitions + + def test_get_definitions_flat(self): + self.check_defined_names(""" + import module + class Class: + pass + def func(): + pass + data = None + """, ['module', 'Class', 'func', 'data']) + + def test_dotted_assignment(self): + self.check_defined_names(""" + x = Class() + x.y.z = None + """, ['x']) + + def test_multiple_assignment(self): + self.check_defined_names(""" + x = y = None + """, ['x', 'y']) + + def test_multiple_imports(self): + self.check_defined_names(""" + from module import a, b + from another_module import * + """, ['a', 'b']) + + def test_nested_definitions(self): + definitions = self.check_defined_names(""" + class Class: + def f(): + pass + def g(): + pass + """, ['Class']) + subdefinitions = definitions[0].defined_names() + self.assertEqual([d.name for d in subdefinitions], + ['f', 'g']) + self.assertEqual([d.full_name for d in subdefinitions], + ['Class.f', 'Class.g']) diff --git a/test/test_regression.py b/test/test_regression.py index 5e111310..cc55773b 100755 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -454,60 +454,6 @@ class TestFeature(TestBase): cache.parser_cache = temp_cache -class TestGetDefinitions(TestBase): - - def test_get_definitions_flat(self): - definitions = api.defined_names(""" - import module - class Class: - pass - def func(): - pass - data = None - """) - self.assertEqual([d.name for d in definitions], - ['module', 'Class', 'func', 'data']) - - def test_dotted_assignment(self): - definitions = api.defined_names(""" - x = Class() - x.y.z = None - """) - self.assertEqual([d.name for d in definitions], - ['x']) - - def test_multiple_assignment(self): - definitions = api.defined_names(""" - x = y = None - """) - self.assertEqual([d.name for d in definitions], - ['x', 'y']) - - def test_multiple_imports(self): - definitions = api.defined_names(""" - from module import a, b - from another_module import * - """) - self.assertEqual([d.name for d in definitions], - ['a', 'b']) - - def test_nested_definitions(self): - definitions = api.defined_names(""" - class Class: - def f(): - pass - def g(): - pass - """) - self.assertEqual([d.name for d in definitions], - ['Class']) - subdefinitions = definitions[0].defined_names() - self.assertEqual([d.name for d in subdefinitions], - ['f', 'g']) - self.assertEqual([d.full_name for d in subdefinitions], - ['Class.f', 'Class.g']) - - class TestSpeed(TestBase): def _check_speed(time_per_run, number=4, run_warm=True): """ Speed checks should typically be very tolerant. Some machines are