mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +08:00
Make sure code_lines works on stubs, even if they are builtins
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -380,6 +380,8 @@ class CompiledContext(ValueContext):
|
||||
|
||||
|
||||
class CompiledModuleContext(CompiledContext):
|
||||
code_lines = None
|
||||
|
||||
def get_value(self):
|
||||
return self._value
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user