mirror of
https://github.com/davidhalter/jedi.git
synced 2026-01-10 05:52:22 +08:00
Merge pull request #228 from tkf/fix-full_name
Fix full_name for import statements
This commit is contained in:
@@ -142,9 +142,19 @@ class BaseDefinition(object):
|
||||
def path(self):
|
||||
"""The module path."""
|
||||
path = []
|
||||
|
||||
def insert_nonnone(x):
|
||||
if x:
|
||||
path.insert(0, x)
|
||||
|
||||
if not isinstance(self._definition, keywords.Keyword):
|
||||
par = self._definition
|
||||
while par is not None:
|
||||
if isinstance(par, pr.Import):
|
||||
insert_nonnone(par.namespace)
|
||||
insert_nonnone(par.from_ns)
|
||||
if par.relative_count == 0:
|
||||
break
|
||||
with common.ignored(AttributeError):
|
||||
path.insert(0, par.name)
|
||||
par = par.parent
|
||||
|
||||
93
test/test_full_name.py
Normal file
93
test/test_full_name.py
Normal file
@@ -0,0 +1,93 @@
|
||||
"""
|
||||
Tests for :attr:`.BaseDefinition.full_name`.
|
||||
|
||||
There are three kinds of test:
|
||||
|
||||
#. Test classes derived from :class:`MixinTestFullName`.
|
||||
Child class defines :meth:`.get_definitions` to alter how
|
||||
the api definition instance is created.
|
||||
|
||||
#. :class:`TestFullDefinedName` is to test combination of
|
||||
:attr:`.full_name` and :func:`.defined_names`.
|
||||
|
||||
#. Misc single-function tests.
|
||||
|
||||
"""
|
||||
|
||||
import textwrap
|
||||
|
||||
import jedi
|
||||
from jedi import api_classes
|
||||
from .base import TestBase
|
||||
|
||||
|
||||
class MixinTestFullName(object):
|
||||
|
||||
def get_definitions(self, source):
|
||||
"""
|
||||
Get definition objects of the variable at the end of `source`.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def check(self, source, desired):
|
||||
definitions = self.get_definitions(textwrap.dedent(source))
|
||||
self.assertEqual(definitions[0].full_name, desired)
|
||||
|
||||
def test_os_path_join(self):
|
||||
self.check('import os; os.path.join', 'os.path.join')
|
||||
|
||||
def test_builtin(self):
|
||||
self.check('type', 'type')
|
||||
|
||||
def test_from_import(self):
|
||||
self.check('from os import path', 'os.path')
|
||||
|
||||
|
||||
class TestFullNameWithGotoDefinitions(MixinTestFullName, TestBase):
|
||||
|
||||
get_definitions = TestBase.goto_definitions
|
||||
|
||||
def test_tuple_mapping(self):
|
||||
self.check("""
|
||||
import re
|
||||
any_re = re.compile('.*')
|
||||
any_re""", 're.RegexObject')
|
||||
|
||||
|
||||
class TestFullNameWithCompletions(MixinTestFullName, TestBase):
|
||||
get_definitions = TestBase.completions
|
||||
|
||||
|
||||
class TestFullDefinedName(TestBase):
|
||||
|
||||
"""
|
||||
Test combination of :attr:`.full_name` and :func:`.defined_names`.
|
||||
"""
|
||||
|
||||
def check(self, source, desired):
|
||||
definitions = jedi.defined_names(textwrap.dedent(source))
|
||||
full_names = [d.full_name for d in definitions]
|
||||
self.assertEqual(full_names, desired)
|
||||
|
||||
def test_local_names(self):
|
||||
self.check("""
|
||||
def f(): pass
|
||||
class C: pass
|
||||
""", ['f', 'C'])
|
||||
|
||||
def test_imports(self):
|
||||
self.check("""
|
||||
import os
|
||||
from os import path
|
||||
from os.path import join
|
||||
from os import path as opath
|
||||
""", ['os', 'os.path', 'os.path.join', 'os.path'])
|
||||
|
||||
|
||||
def test_keyword_full_name_should_be_none():
|
||||
"""issue #94"""
|
||||
# Using `from jedi.keywords import Keyword` here does NOT work
|
||||
# in Python 3. This is due to the import hack jedi using.
|
||||
Keyword = api_classes.keywords.Keyword
|
||||
d = api_classes.Definition(Keyword('(', (0, 0)))
|
||||
assert d.full_name is None
|
||||
@@ -16,7 +16,6 @@ from .base import TestBase, unittest, cwd_at
|
||||
import jedi
|
||||
from jedi._compatibility import utf8, unicode, is_py33
|
||||
from jedi import api, parsing, common
|
||||
api_classes = api.api_classes
|
||||
|
||||
#jedi.set_debug_function(jedi.debug.print_to_stdout)
|
||||
|
||||
@@ -417,28 +416,6 @@ class TestDocstring(TestBase):
|
||||
|
||||
|
||||
class TestFeature(TestBase):
|
||||
def test_full_name(self):
|
||||
""" feature request #61"""
|
||||
assert self.completions('import os; os.path.join')[0].full_name \
|
||||
== 'os.path.join'
|
||||
|
||||
def test_keyword_full_name_should_be_none(self):
|
||||
"""issue #94"""
|
||||
# Using `from jedi.keywords import Keyword` here does NOT work
|
||||
# in Python 3. This is due to the import hack jedi using.
|
||||
Keyword = api_classes.keywords.Keyword
|
||||
d = api_classes.Definition(Keyword('(', (0, 0)))
|
||||
assert d.full_name is None
|
||||
|
||||
def test_full_name_builtin(self):
|
||||
self.assertEqual(self.completions('type')[0].full_name, 'type')
|
||||
|
||||
def test_full_name_tuple_mapping(self):
|
||||
s = """
|
||||
import re
|
||||
any_re = re.compile('.*')
|
||||
any_re"""
|
||||
self.assertEqual(self.goto_definitions(s)[0].full_name, 're.RegexObject')
|
||||
|
||||
def test_preload_modules(self):
|
||||
def check_loaded(*modules):
|
||||
|
||||
Reference in New Issue
Block a user