Skip to content

Commit e4ee218

Browse files
metheorytclaude
andcommitted
Fix crash in pylsp_definitions on definitions without a source position
When follow_builtin_definitions is True (the default), the return-list comprehension filter short-circuits `follow_builtin_defns or _not_internal_definition(d)`, so the None-guards inside _not_internal_definition never run. A jedi definition that resolves into a compiled/builtin module has line/column == None, so `d.line - 1` raises `TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'`. Exclude definitions with no line/column unconditionally: they cannot form a valid LSP range regardless of the follow_builtin_definitions setting. Fixes #623 Fixes #624 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent bc1095f commit e4ee218

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

pylsp/plugins/definition.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ def pylsp_definitions(
7171
},
7272
}
7373
for d in definitions
74-
if d.is_definition() and (follow_builtin_defns or _not_internal_definition(d))
74+
if d.is_definition()
75+
and d.line is not None
76+
and d.column is not None
77+
and (follow_builtin_defns or _not_internal_definition(d))
7578
]
7679

7780

test/plugins/test_definitions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import os
55

6+
import jedi
7+
68
from pylsp import uris
79
from pylsp.plugins.definition import pylsp_definitions
810
from pylsp.workspace import Document
@@ -173,3 +175,27 @@ def foo():
173175
assert [{"uri": module_uri, "range": def_range}] == pylsp_definitions(
174176
config, doc, cursor_pos
175177
)
178+
179+
180+
def test_definition_without_source_position(config, workspace, monkeypatch) -> None:
181+
# A definition jedi resolves without a source position (e.g. a compiled
182+
# builtin, where line/column are None) must be dropped, not crash with
183+
# `TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'`.
184+
# Regression test for #623 and #624.
185+
class _NoPositionName:
186+
line = None
187+
column = None
188+
module_path = None
189+
name = "compiled"
190+
191+
def is_definition(self) -> bool:
192+
return True
193+
194+
monkeypatch.setattr(
195+
jedi.api.Script, "goto", lambda *args, **kwargs: [_NoPositionName()]
196+
)
197+
198+
doc = Document(DOC_URI, workspace, DOC)
199+
# follow_builtin_definitions defaults to True, so the None-guard in the
200+
# return comprehension is the only thing preventing the crash.
201+
assert pylsp_definitions(config, doc, {"line": 3, "character": 6}) == []

0 commit comments

Comments
 (0)