mirror of
https://github.com/davidhalter/jedi.git
synced 2026-01-09 05:22:19 +08:00
@@ -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)
|
||||
|
||||
@@ -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):
|
||||
|
||||
57
test/test_defined_names.py
Normal file
57
test/test_defined_names.py
Normal file
@@ -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'])
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user