Get the context of a class name right, fixes #1396

This commit is contained in:
Dave Halter
2019-12-09 09:56:03 +01:00
parent ed3fdf8876
commit 8e33fd1931
2 changed files with 26 additions and 3 deletions

View File

@@ -56,8 +56,7 @@ from jedi.plugins import plugin_manager
class ClassName(TreeNameDefinition):
def __init__(self, class_value, tree_name, name_context, apply_decorators):
super(ClassName, self).__init__(class_value.as_context(), tree_name)
self._name_context = name_context
super(ClassName, self).__init__(name_context, tree_name)
self._apply_decorators = apply_decorators
self._class_value = class_value
@@ -66,7 +65,7 @@ class ClassName(TreeNameDefinition):
# We're using a different value to infer, so we cannot call super().
from jedi.inference.syntax_tree import tree_name_to_values
inferred = tree_name_to_values(
self.parent_context.inference_state, self._name_context, self.tree_name)
self.parent_context.inference_state, self.parent_context, self.tree_name)
for result_value in inferred:
if self._apply_decorators:

View File

@@ -3,12 +3,14 @@
from textwrap import dedent
from inspect import cleandoc
import os
import pytest
import jedi
from jedi import __doc__ as jedi_doc
from jedi.inference.compiled import CompiledValueName
from ..helpers import get_example_dir
def test_is_keyword(Script):
@@ -473,3 +475,25 @@ def test_execute(Script, code, description):
else:
d, = definitions
assert d.description == description
@pytest.mark.parametrize('goto_assignment', [False, True, None])
@pytest.mark.parametrize(
'code, name, file_name', [
('from pkg import Foo; Foo.foo', 'foo', '__init__.py'),
('from pkg import Foo; Foo().foo', 'foo', '__init__.py'),
('from pkg import Foo; Foo.bar', 'bar', 'module.py'),
('from pkg import Foo; Foo().bar', 'bar', 'module.py'),
])
def test_inheritance_module_path(Script, goto_assignment, code, name, file_name):
base_path = os.path.join(get_example_dir('inheritance'), 'pkg')
whatever_path = os.path.join(base_path, 'NOT_EXISTING.py')
script = Script(code, path=whatever_path)
if goto_assignment is None:
func, = script.goto_definitions()
else:
func, = script.goto_assignments(follow_imports=goto_assignment)
assert func.type == 'function'
assert func.name == name
assert func.module_path == os.path.join(base_path, file_name)