Repository: python-lsp/python-lsp-server

Issue:
Is there a configuration option to enable jumping to builtin module stubs?
I'm using Jupyter Lab's `Jump to definition`, and PR https://github.com/palantir/python-language-server/pull/687 completely disables jumping to `📜extension_module.pyi` for the following package arrangement:

```
📂site-packages
 ┗━📂my_package
   ┣━📜__init__.py
   ┣━📦extension_module.so
   ┗━📜extension_module.pyi
```

Personally, I also find the behaviour in the PR (disabling jumping to the Python standard library builtins stubs) quite frustrating, as I frequently refer to the Python standard library's stubs.

I've temporarily disabled the behaviour by making the following change,

```python
# pylsp/plugins/definition.py

def _not_internal_definition(definition):
    return True
```

but am wondering if there's been an update to exposing a configuration for them (preferably accessible in both JupyterLab and VSCode)?

Comments:

Patch:
diff --git a/CONFIGURATION.md b/CONFIGURATION.md
index aed5627..f2626e4 100644
--- a/CONFIGURATION.md
+++ b/CONFIGURATION.md
@@ -32,6 +32,7 @@ This server can be configured using the `workspace/didChangeConfiguration` metho
 | `pylsp.plugins.jedi_definition.enabled` | `boolean` | Enable or disable the plugin. | `true` |
 | `pylsp.plugins.jedi_definition.follow_imports` | `boolean` | The goto call will follow imports. | `true` |
 | `pylsp.plugins.jedi_definition.follow_builtin_imports` | `boolean` | If follow_imports is True will decide if it follow builtin imports. | `true` |
+| `pylsp.plugins.jedi_definition.follow_builtin_definitions` | `boolean` | Follow builtin and extension definitions to stubs. | `true` |
 | `pylsp.plugins.jedi_hover.enabled` | `boolean` | Enable or disable the plugin. | `true` |
 | `pylsp.plugins.jedi_references.enabled` | `boolean` | Enable or disable the plugin. | `true` |
 | `pylsp.plugins.jedi_signature_help.enabled` | `boolean` | Enable or disable the plugin. | `true` |
diff --git a/pylsp/config/schema.json b/pylsp/config/schema.json
index 7e23500..4ac085d 100644
--- a/pylsp/config/schema.json
+++ b/pylsp/config/schema.json
@@ -176,6 +176,11 @@
       "default": true,
       "description": "If follow_imports is True will decide if it follow builtin imports."
     },
+    "pylsp.plugins.jedi_definition.follow_builtin_definitions": {
+      "type": "boolean",
+      "default": true,
+      "description": "Follow builtin and extension definitions to stubs."
+    },
     "pylsp.plugins.jedi_hover.enabled": {
       "type": "boolean",
       "default": true,
diff --git a/pylsp/plugins/definition.py b/pylsp/plugins/definition.py
index bf707b7..b2110af 100644
--- a/pylsp/plugins/definition.py
+++ b/pylsp/plugins/definition.py
@@ -17,6 +17,7 @@ def pylsp_definitions(config, workspace, document, position):
             follow_builtin_imports=settings.get('follow_builtin_imports', True),
             **code_position)
 
+        follow_builtin_defns = settings.get("follow_builtin_definitions", True)
         return [
             {
                 'uri': uris.uri_with(document.uri, path=str(d.module_path)),
@@ -25,7 +26,7 @@ def pylsp_definitions(config, workspace, document, position):
                     'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
                 }
             }
-            for d in definitions if d.is_definition() and _not_internal_definition(d)
+            for d in definitions if d.is_definition() and (follow_builtin_defns or _not_internal_definition(d))
         ]
 
 
diff --git a/pylsp/plugins/symbols.py b/pylsp/plugins/symbols.py
index 939dcda..d5925db 100644
--- a/pylsp/plugins/symbols.py
+++ b/pylsp/plugins/symbols.py
@@ -91,7 +91,7 @@ def pylsp_document_symbols(config, document):
                 else:
                     continue
 
-        if _include_def(d) and Path(document.path) == d.module_path:
+        if _include_def(d) and Path(document.path) == Path(d.module_path):
             tuple_range = _tuple_range(d)
             if tuple_range in exclude:
                 continue


Test patch:
diff --git a/test/plugins/test_definitions.py b/test/plugins/test_definitions.py
index bcc7648..a8972cd 100644
--- a/test/plugins/test_definitions.py
+++ b/test/plugins/test_definitions.py
@@ -42,9 +42,24 @@ def test_builtin_definition(config, workspace):
     # Over 'i' in dict
     cursor_pos = {'line': 8, 'character': 24}
 
-    # No go-to def for builtins
     doc = Document(DOC_URI, workspace, DOC)
-    assert not pylsp_definitions(config, workspace, doc, cursor_pos)
+    orig_settings = config.settings()
+
+    # Check definition for `dict` goes to `builtins.pyi::dict`
+    follow_defns_setting = {'follow_builtin_definitions': True}
+    settings = {'plugins': {'jedi_definition': follow_defns_setting}}
+    config.update(settings)
+    defns = pylsp_definitions(config, workspace, doc, cursor_pos)
+    assert len(defns) == 1
+    assert defns[0]["uri"].endswith("builtins.pyi")
+
+    # Check no definitions for `dict`
+    follow_defns_setting['follow_builtin_definitions'] = False
+    config.update(settings)
+    defns = pylsp_definitions(config, workspace, doc, cursor_pos)
+    assert not defns
+
+    config.update(orig_settings)
 
 
 def test_assignment(config, workspace):