Skip to content

Commit 15921c4

Browse files
committed
feat: add basedpyright and set it as default python client
1 parent 8442290 commit 15921c4

File tree

3 files changed

+150
-3
lines changed

3 files changed

+150
-3
lines changed

src/lsp_client/clients/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import Final
44

5+
from .basedpyright import BasedpyrightClient
56
from .deno import DenoClient
67
from .gopls import GoplsClient
78
from .pyrefly import PyreflyClient
@@ -11,6 +12,7 @@
1112
from .typescript import TypescriptClient
1213

1314
clients: Final = {
15+
"basedpyright": BasedpyrightClient,
1416
"gopls": GoplsClient,
1517
"pyrefly": PyreflyClient,
1618
"pyright": PyrightClient,
@@ -21,6 +23,7 @@
2123
}
2224

2325
__all__ = [
26+
"BasedpyrightClient",
2427
"DenoClient",
2528
"GoplsClient",
2629
"PyreflyClient",
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
from __future__ import annotations
2+
3+
import shutil
4+
from functools import partial
5+
from subprocess import CalledProcessError
6+
from typing import Any, override
7+
8+
import anyio
9+
from attrs import define
10+
from loguru import logger
11+
12+
from lsp_client.capability.notification import (
13+
WithNotifyDidChangeConfiguration,
14+
)
15+
from lsp_client.capability.request import (
16+
WithRequestCallHierarchy,
17+
WithRequestCodeAction,
18+
WithRequestCompletion,
19+
WithRequestDeclaration,
20+
WithRequestDefinition,
21+
WithRequestDocumentSymbol,
22+
WithRequestHover,
23+
WithRequestReferences,
24+
WithRequestSignatureHelp,
25+
WithRequestTypeDefinition,
26+
WithRequestWorkspaceSymbol,
27+
)
28+
from lsp_client.capability.server_notification import (
29+
WithReceiveLogMessage,
30+
WithReceiveLogTrace,
31+
WithReceivePublishDiagnostics,
32+
WithReceiveShowMessage,
33+
)
34+
from lsp_client.capability.server_request import (
35+
WithRespondConfigurationRequest,
36+
WithRespondInlayHintRefresh,
37+
WithRespondShowDocumentRequest,
38+
WithRespondShowMessageRequest,
39+
WithRespondWorkspaceFoldersRequest,
40+
)
41+
from lsp_client.clients.base import PythonClientBase
42+
from lsp_client.server import DefaultServers, ServerInstallationError
43+
from lsp_client.server.container import ContainerServer
44+
from lsp_client.server.local import LocalServer
45+
from lsp_client.utils.types import lsp_type
46+
47+
BasedpyrightContainerServer = partial(
48+
ContainerServer, image="ghcr.io/lsp-client/basedpyright:latest"
49+
)
50+
51+
52+
async def ensure_basedpyright_installed() -> None:
53+
if shutil.which("basedpyright-langserver"):
54+
return
55+
56+
logger.warning(
57+
"basedpyright-langserver not found, attempting to install via npm..."
58+
)
59+
60+
try:
61+
await anyio.run_process(["npm", "install", "-g", "basedpyright"])
62+
logger.info("Successfully installed basedpyright-langserver via npm")
63+
return
64+
except CalledProcessError as e:
65+
raise ServerInstallationError(
66+
"Could not install basedpyright-langserver. Please install it manually with 'npm install -g basedpyright' or 'pip install basedpyright'. "
67+
"See https://github.com/detachhead/basedpyright for more information."
68+
) from e
69+
70+
71+
BasedpyrightLocalServer = partial(
72+
LocalServer,
73+
program="basedpyright-langserver",
74+
args=["--stdio"],
75+
ensure_installed=ensure_basedpyright_installed,
76+
)
77+
78+
79+
@define
80+
class BasedpyrightClient(
81+
PythonClientBase,
82+
WithNotifyDidChangeConfiguration,
83+
WithRequestCallHierarchy,
84+
WithRequestCodeAction,
85+
WithRequestCompletion,
86+
WithRequestDeclaration,
87+
WithRequestDefinition,
88+
WithRequestDocumentSymbol,
89+
WithRequestHover,
90+
WithRequestReferences,
91+
WithRequestSignatureHelp,
92+
WithRequestTypeDefinition,
93+
WithRequestWorkspaceSymbol,
94+
WithReceiveLogMessage,
95+
WithReceiveLogTrace,
96+
WithReceivePublishDiagnostics,
97+
WithReceiveShowMessage,
98+
WithRespondConfigurationRequest,
99+
WithRespondInlayHintRefresh,
100+
WithRespondShowDocumentRequest,
101+
WithRespondShowMessageRequest,
102+
WithRespondWorkspaceFoldersRequest,
103+
):
104+
"""
105+
- Language: Python
106+
- Homepage: https://github.com/detachhead/basedpyright
107+
- Doc: https://github.com/detachhead/basedpyright
108+
- Github: https://github.com/detachhead/basedpyright
109+
"""
110+
111+
@classmethod
112+
@override
113+
def create_default_servers(cls) -> DefaultServers:
114+
return DefaultServers(
115+
local=BasedpyrightLocalServer(),
116+
container=BasedpyrightContainerServer(),
117+
)
118+
119+
@override
120+
def check_server_compatibility(self, info: lsp_type.ServerInfo | None) -> None:
121+
return
122+
123+
@override
124+
def create_default_config(self) -> dict[str, Any] | None:
125+
"""
126+
https://github.com/detachhead/basedpyright#settings
127+
"""
128+
return {
129+
"basedpyright": {
130+
"analysis": {
131+
"autoImportCompletions": True,
132+
"autoSearchPaths": True,
133+
"diagnosticMode": "openFilesOnly",
134+
"indexing": True,
135+
"typeCheckingMode": "recommended",
136+
"inlayHints": {
137+
"variableTypes": True,
138+
"functionReturnTypes": True,
139+
"callArgumentNames": True,
140+
"pytestParameters": True,
141+
},
142+
}
143+
}
144+
}

src/lsp_client/clients/lang.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
from lsp_client.client.abc import Client
88

9+
from .basedpyright import BasedpyrightClient
910
from .deno.client import DenoClient
1011
from .gopls import GoplsClient
11-
from .pyright import PyrightClient
1212
from .rust_analyzer import RustAnalyzerClient
1313
from .typescript import TypescriptClient
1414

@@ -21,13 +21,13 @@
2121
]
2222

2323
GoClient = GoplsClient
24-
PythonClient = PyrightClient
24+
PythonClient = BasedpyrightClient
2525
RustClient = RustAnalyzerClient
2626
TypeScriptClient = TypescriptClient
2727

2828
lang_clients: Final[dict[Language, type[Client]]] = {
2929
"go": GoplsClient,
30-
"python": PyrightClient,
30+
"python": BasedpyrightClient,
3131
"rust": RustAnalyzerClient,
3232
"typescript": TypescriptClient,
3333
"deno": DenoClient,

0 commit comments

Comments
 (0)