mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 05:54:25 +08:00
Make infer public on classes
This commit is contained in:
@@ -320,8 +320,7 @@ class BaseDefinition(object):
|
||||
names = self._evaluator.goto(self._name.parent_context, self._name.tree_name)
|
||||
return [Definition(self._evaluator, n) for n in names]
|
||||
|
||||
def _goto_definitions(self):
|
||||
# TODO make this function public.
|
||||
def infer(self):
|
||||
return [Definition(self._evaluator, d.name) for d in self._name.infer()]
|
||||
|
||||
@property
|
||||
|
||||
@@ -72,7 +72,7 @@ class ContextNameMixin(object):
|
||||
return ContextSet([self._context])
|
||||
|
||||
def get_root_context(self):
|
||||
if self.parent_context is None:
|
||||
if self.parent_context is None: # A module
|
||||
return self._context
|
||||
return super(ContextNameMixin, self).get_root_context()
|
||||
|
||||
|
||||
@@ -214,7 +214,8 @@ class StubName(NameWrapper):
|
||||
return self._wrapped_name.infer()
|
||||
|
||||
typ = self._wrapped_name.tree_name.parent.type
|
||||
# TODO is this if a performance optimization?
|
||||
# Only for these two we want to merge, the function doesn't support
|
||||
# anything else.
|
||||
if typ in ('classdef', 'funcdef'):
|
||||
actual_context, = self._wrapped_name.infer()
|
||||
return _add_stub_if_possible(self.parent_context, actual_context, stub_contexts)
|
||||
@@ -244,6 +245,7 @@ def _add_stub_if_possible(parent_context, actual_context, stub_contexts):
|
||||
|
||||
|
||||
def with_stub_context_if_possible(actual_context):
|
||||
assert actual_context.tree_node.type in ('classdef', 'funcdef')
|
||||
names = actual_context.get_qualified_names()
|
||||
stub_module = actual_context.get_root_context().stub_context
|
||||
if stub_module is None:
|
||||
|
||||
@@ -207,9 +207,9 @@ def test_goto_assignments_follow_imports(Script):
|
||||
assert definition.name == 'p'
|
||||
result, = definition.goto_assignments()
|
||||
assert result.name == 'p'
|
||||
result, = definition._goto_definitions()
|
||||
result, = definition.infer()
|
||||
assert result.name == 'int'
|
||||
result, = result._goto_definitions()
|
||||
result, = result.infer()
|
||||
assert result.name == 'int'
|
||||
|
||||
definition, = script.goto_assignments()
|
||||
|
||||
@@ -29,7 +29,7 @@ def test_in_empty_space(Script):
|
||||
comps = Script(code, 3, 7).completions()
|
||||
self, = [c for c in comps if c.name == 'self']
|
||||
assert self.name == 'self'
|
||||
def_, = self._goto_definitions()
|
||||
def_, = self.infer()
|
||||
assert def_.name == 'X'
|
||||
|
||||
|
||||
|
||||
@@ -244,8 +244,8 @@ def test_completion_params():
|
||||
script = jedi.Interpreter('foo', [locals()])
|
||||
c, = script.completions()
|
||||
assert [p.name for p in c.params] == ['a', 'b']
|
||||
assert c.params[0]._goto_definitions() == []
|
||||
t, = c.params[1]._goto_definitions()
|
||||
assert c.params[0].infer() == []
|
||||
t, = c.params[1].infer()
|
||||
assert t.name == 'int'
|
||||
|
||||
|
||||
@@ -258,9 +258,9 @@ def test_completion_param_annotations():
|
||||
script = jedi.Interpreter('foo', [locals()])
|
||||
c, = script.completions()
|
||||
a, b, c = c.params
|
||||
assert a._goto_definitions() == []
|
||||
assert [d.name for d in b._goto_definitions()] == ['str']
|
||||
assert {d.name for d in c._goto_definitions()} == {'int', 'float'}
|
||||
assert a.infer() == []
|
||||
assert [d.name for d in b.infer()] == ['str']
|
||||
assert {d.name for d in c.infer()} == {'int', 'float'}
|
||||
|
||||
|
||||
def test_keyword_argument():
|
||||
@@ -340,7 +340,7 @@ def test_dir_magic_method():
|
||||
assert 'bar' in names
|
||||
|
||||
foo = [c for c in completions if c.name == 'foo'][0]
|
||||
assert foo._goto_definitions() == []
|
||||
assert foo.infer() == []
|
||||
|
||||
|
||||
def test_name_not_findable():
|
||||
|
||||
@@ -3,5 +3,5 @@ def test_module_attributes(Script):
|
||||
assert def_.name == '__name__'
|
||||
assert def_.line is None
|
||||
assert def_.column is None
|
||||
str_, = def_._goto_definitions()
|
||||
str_, = def_.infer()
|
||||
assert str_.name == 'str'
|
||||
|
||||
@@ -185,6 +185,13 @@ def test_goto_stubs(Script):
|
||||
os_module, = s.goto_assignments()
|
||||
|
||||
|
||||
def _assert_is_same(d1, d2):
|
||||
assert d1.name == d2.name
|
||||
assert d1.module_path == d2.module_path
|
||||
assert d1.line == d2.line
|
||||
assert d1.column == d2.column
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'code', [
|
||||
'import os; os.walk',
|
||||
@@ -197,15 +204,30 @@ def test_goto_stubs_on_itself(Script, code):
|
||||
"""
|
||||
s = Script(code)
|
||||
def_, = s.goto_definitions()
|
||||
stub, = def_.goto_stubs()
|
||||
#stub, = def_.goto_stubs()
|
||||
|
||||
script_on_source = Script(
|
||||
path=def_.module_path,
|
||||
line=def_.line,
|
||||
column=def_.column
|
||||
)
|
||||
print('GO')
|
||||
definition, = script_on_source.goto_definitions()
|
||||
print('\ta', definition._name._context, definition._name._context.parent_context)
|
||||
return
|
||||
same_stub, = definition.goto_stubs()
|
||||
assert stub.module_path == same_stub.module_path
|
||||
assert stub.line == same_stub.line
|
||||
assert stub.column == same_stub.column
|
||||
_assert_is_same(same_stub, stub)
|
||||
_assert_is_same(definition, def_)
|
||||
assert same_stub.module_path != def_.module_path
|
||||
|
||||
# And the reverse.
|
||||
script_on_source = Script(
|
||||
path=same_stub.module_path,
|
||||
line=same_stub.line,
|
||||
column=same_stub.column
|
||||
)
|
||||
|
||||
same_definition, = script_on_source.goto_definitions()
|
||||
same_definition2, = same_stub.infer()
|
||||
_assert_is_same(same_definition, definition)
|
||||
_assert_is_same(same_definition, same_definition2)
|
||||
|
||||
Reference in New Issue
Block a user