Skip to content

Commit 80d6298

Browse files
authored
Expose setting to follow builtin and extension definitions to stub files (python-lsp#321)
1 parent 0487a06 commit 80d6298

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

CONFIGURATION.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This server can be configured using the `workspace/didChangeConfiguration` metho
3232
| `pylsp.plugins.jedi_definition.enabled` | `boolean` | Enable or disable the plugin. | `true` |
3333
| `pylsp.plugins.jedi_definition.follow_imports` | `boolean` | The goto call will follow imports. | `true` |
3434
| `pylsp.plugins.jedi_definition.follow_builtin_imports` | `boolean` | If follow_imports is True will decide if it follow builtin imports. | `true` |
35+
| `pylsp.plugins.jedi_definition.follow_builtin_definitions` | `boolean` | Follow builtin and extension definitions to stubs. | `true` |
3536
| `pylsp.plugins.jedi_hover.enabled` | `boolean` | Enable or disable the plugin. | `true` |
3637
| `pylsp.plugins.jedi_references.enabled` | `boolean` | Enable or disable the plugin. | `true` |
3738
| `pylsp.plugins.jedi_signature_help.enabled` | `boolean` | Enable or disable the plugin. | `true` |

pylsp/config/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@
176176
"default": true,
177177
"description": "If follow_imports is True will decide if it follow builtin imports."
178178
},
179+
"pylsp.plugins.jedi_definition.follow_builtin_definitions": {
180+
"type": "boolean",
181+
"default": true,
182+
"description": "Follow builtin and extension definitions to stubs."
183+
},
179184
"pylsp.plugins.jedi_hover.enabled": {
180185
"type": "boolean",
181186
"default": true,

pylsp/plugins/definition.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def pylsp_definitions(config, workspace, document, position):
1717
follow_builtin_imports=settings.get('follow_builtin_imports', True),
1818
**code_position)
1919

20+
follow_builtin_defns = settings.get("follow_builtin_definitions", True)
2021
return [
2122
{
2223
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
@@ -25,7 +26,7 @@ def pylsp_definitions(config, workspace, document, position):
2526
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
2627
}
2728
}
28-
for d in definitions if d.is_definition() and _not_internal_definition(d)
29+
for d in definitions if d.is_definition() and (follow_builtin_defns or _not_internal_definition(d))
2930
]
3031

3132

test/plugins/test_definitions.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,24 @@ def test_builtin_definition(config, workspace):
4242
# Over 'i' in dict
4343
cursor_pos = {'line': 8, 'character': 24}
4444

45-
# No go-to def for builtins
4645
doc = Document(DOC_URI, workspace, DOC)
47-
assert not pylsp_definitions(config, workspace, doc, cursor_pos)
46+
orig_settings = config.settings()
47+
48+
# Check definition for `dict` goes to `builtins.pyi::dict`
49+
follow_defns_setting = {'follow_builtin_definitions': True}
50+
settings = {'plugins': {'jedi_definition': follow_defns_setting}}
51+
config.update(settings)
52+
defns = pylsp_definitions(config, workspace, doc, cursor_pos)
53+
assert len(defns) == 1
54+
assert defns[0]["uri"].endswith("builtins.pyi")
55+
56+
# Check no definitions for `dict`
57+
follow_defns_setting['follow_builtin_definitions'] = False
58+
config.update(settings)
59+
defns = pylsp_definitions(config, workspace, doc, cursor_pos)
60+
assert not defns
61+
62+
config.update(orig_settings)
4863

4964

5065
def test_assignment(config, workspace):

0 commit comments

Comments
 (0)