From 6ffeea7eea90ca3d3d754da826d8e4b3da38f2ab Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 1 Dec 2019 19:10:08 +0100 Subject: [PATCH] Make sure code_lines works on stubs, even if they are builtins --- jedi/api/classes.py | 5 ++++- jedi/inference/context.py | 2 ++ test/conftest.py | 6 ++++++ test/test_api/test_api.py | 9 ++++++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/jedi/api/classes.py b/jedi/api/classes.py index 580e6d7b..2be39f91 100644 --- a/jedi/api/classes.py +++ b/jedi/api/classes.py @@ -384,10 +384,13 @@ class BaseDefinition(object): :return str: Returns the line(s) of code or an empty string if it's a builtin. """ - if not self._name.is_value_name or self.in_builtin_module(): + if not self._name.is_value_name: return '' lines = self._name.get_root_context().code_lines + if lines is None: + # Probably a builtin module, just ignore in that case. + return '' index = self._name.start_pos[0] - 1 start_index = max(index - before, 0) diff --git a/jedi/inference/context.py b/jedi/inference/context.py index da8ad781..227cc927 100644 --- a/jedi/inference/context.py +++ b/jedi/inference/context.py @@ -380,6 +380,8 @@ class CompiledContext(ValueContext): class CompiledModuleContext(CompiledContext): + code_lines = None + def get_value(self): return self._value diff --git a/test/conftest.py b/test/conftest.py index bf93aa40..298a8219 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -129,3 +129,9 @@ def inference_state(Script): @pytest.fixture def same_process_inference_state(Script): return Script('', environment=InterpreterEnvironment())._inference_state + + +@pytest.fixture +def disable_typeshed(monkeypatch): + from jedi.inference.gradual import typeshed + monkeypatch.setattr(typeshed, '_load_from_typeshed', lambda *args, **kwargs: None) diff --git a/test/test_api/test_api.py b/test/test_api/test_api.py index bfe305b6..fbb7c540 100644 --- a/test/test_api/test_api.py +++ b/test/test_api/test_api.py @@ -170,7 +170,7 @@ def test_get_line_code(Script): return Script(source, line=line).completions()[0].get_line_code(**kwargs) # On builtin - assert get_line_code('') == '' + assert get_line_code('abs') == 'def abs(__n: SupportsAbs[_T]) -> _T: ...\n' # On custom code first_line = 'def foo():\n' @@ -188,6 +188,13 @@ def test_get_line_code(Script): assert get_line_code(code, line=2, after=3, before=3) == code +def test_get_line_code_on_builtin(Script, disable_typeshed): + abs_ = Script('abs').completions()[0] + assert abs_.name == 'abs' + assert abs_.get_line_code() == '' + assert abs_.line is None + + def test_goto_assignments_follow_imports(Script): code = dedent(""" import inspect