Skip to content

Commit def2021

Browse files
committed
Skip definitions without a source path
1 parent a362006 commit def2021

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎pylsp/plugins/definition.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def pylsp_definitions(
7272
}
7373
for d in definitions
7474
if d.is_definition()
75+
and d.module_path is not None
7576
and d.line is not None
7677
and d.column is not None
7778
and (follow_builtin_defns or _not_internal_definition(d))

‎test/plugins/test_definitions.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55

66
import jedi
7+
import pytest
78

89
from pylsp import uris
910
from pylsp.plugins.definition import pylsp_definitions
@@ -199,3 +200,44 @@ def is_definition(self) -> bool:
199200
# follow_builtin_definitions defaults to True, so the None-guard in the
200201
# return comprehension is the only thing preventing the crash.
201202
assert pylsp_definitions(config, doc, {"line": 3, "character": 6}) == []
203+
204+
205+
@pytest.mark.parametrize("follow_builtin_definitions", [True, False])
206+
@pytest.mark.parametrize(
207+
"character, expected_range",
208+
[
209+
pytest.param(3, None, id="generated-attribute"),
210+
pytest.param(
211+
0,
212+
{
213+
"start": {"line": 4, "character": 0},
214+
"end": {"line": 4, "character": 1},
215+
},
216+
id="source-backed-instance",
217+
),
218+
],
219+
)
220+
def test_namedtuple_definitions(
221+
config, workspace, follow_builtin_definitions, character, expected_range
222+
) -> None:
223+
source = """from collections import namedtuple
224+
225+
Access = namedtuple("Access", ["address", "type"])
226+
227+
a = Access(100, "w")
228+
a.address
229+
"""
230+
doc = Document(DOC_URI, workspace, source)
231+
config.update(
232+
{
233+
"plugins": {
234+
"jedi_definition": {
235+
"follow_builtin_definitions": follow_builtin_definitions
236+
}
237+
}
238+
}
239+
)
240+
expected = [{"uri": DOC_URI, "range": expected_range}] if expected_range else []
241+
assert (
242+
pylsp_definitions(config, doc, {"line": 5, "character": character}) == expected
243+
)

0 commit comments

Comments
 (0)