Skip to content

Commit bfca4b0

Browse files
committed
chore: type improvements
1 parent e235a4b commit bfca4b0

13 files changed

+166
-145
lines changed

st4_py38/lsp_utils/_client_handler/abstract_plugin.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations
2-
from .._util import weak_method
2+
from ..api_wrapper_interface import ApiNotificationHandler
3+
from ..api_wrapper_interface import ApiRequestHandler
34
from ..api_wrapper_interface import ApiWrapperInterface
45
from ..server_resource_interface import ServerStatus
56
from .api_decorator import register_decorated_handlers
67
from .interface import ClientHandlerInterface
8+
from abc import ABCMeta
79
from functools import partial
810
from LSP.plugin import AbstractPlugin
911
from LSP.plugin import ClientConfig
@@ -16,58 +18,56 @@
1618
from LSP.plugin import WorkspaceFolder
1719
from LSP.plugin.core.rpc import method2attr
1820
from os import path
19-
from typing import Any, Callable, Dict, List, Optional, Tuple, TypedDict
20-
from weakref import ref
21+
from typing import Any, Callable
22+
from typing_extensions import override
23+
from weakref import WeakMethod, ref
2124
import sublime
2225

2326
__all__ = ['ClientHandler']
2427

25-
LanguagesDict = TypedDict('LanguagesDict', {
26-
'document_selector': Optional[str],
27-
'languageId': Optional[str],
28-
'scopes': Optional[List[str]],
29-
'syntaxes': Optional[List[str]],
30-
}, total=False)
31-
ApiNotificationHandler = Callable[[Any], None]
32-
ApiRequestHandler = Callable[[Any, Callable[[Any], None]], None]
33-
3428

3529
class ApiWrapper(ApiWrapperInterface):
3630
def __init__(self, plugin: 'ref[AbstractPlugin]'):
3731
self.__plugin = plugin
3832

39-
def __session(self) -> Optional[Session]:
33+
def __session(self) -> Session | None:
4034
plugin = self.__plugin()
4135
return plugin.weaksession() if plugin else None
4236

4337
# --- ApiWrapperInterface -----------------------------------------------------------------------------------------
4438

39+
@override
4540
def on_notification(self, method: str, handler: ApiNotificationHandler) -> None:
46-
def handle_notification(weak_handler: ApiNotificationHandler, params: Any) -> None:
47-
weak_handler(params)
41+
def handle_notification(weak_handler: WeakMethod[ApiNotificationHandler], params: Any) -> None:
42+
if handler := weak_handler():
43+
handler(params)
4844

4945
plugin = self.__plugin()
5046
if plugin:
51-
setattr(plugin, method2attr(method), partial(handle_notification, weak_method(handler)))
47+
setattr(plugin, method2attr(method), partial(handle_notification, WeakMethod(handler)))
5248

49+
@override
5350
def on_request(self, method: str, handler: ApiRequestHandler) -> None:
5451
def send_response(request_id: Any, result: Any) -> None:
5552
session = self.__session()
5653
if session:
5754
session.send_response(Response(request_id, result))
5855

59-
def on_response(weak_handler: ApiRequestHandler, params: Any, request_id: Any) -> None:
60-
weak_handler(params, lambda result: send_response(request_id, result))
56+
def on_response(weak_handler: WeakMethod[ApiRequestHandler], params: Any, request_id: Any) -> None:
57+
if handler := weak_handler():
58+
handler(params, lambda result: send_response(request_id, result))
6159

6260
plugin = self.__plugin()
6361
if plugin:
64-
setattr(plugin, method2attr(method), partial(on_response, weak_method(handler)))
62+
setattr(plugin, method2attr(method), partial(on_response, WeakMethod(handler)))
6563

64+
@override
6665
def send_notification(self, method: str, params: Any) -> None:
6766
session = self.__session()
6867
if session:
6968
session.send_notification(Notification(method, params))
7069

70+
@override
7171
def send_request(self, method: str, params: Any, handler: Callable[[Any, bool], None]) -> None:
7272
session = self.__session()
7373
if session:
@@ -77,41 +77,47 @@ def send_request(self, method: str, params: Any, handler: Callable[[Any, bool],
7777
handler(None, True)
7878

7979

80-
class ClientHandler(AbstractPlugin, ClientHandlerInterface):
80+
class ClientHandler(AbstractPlugin, ClientHandlerInterface, metaclass=ABCMeta):
8181
"""
8282
The base class for creating an LSP plugin.
8383
"""
8484

8585
# --- AbstractPlugin handlers -------------------------------------------------------------------------------------
8686

8787
@classmethod
88+
@override
8889
def name(cls) -> str:
8990
return cls.get_displayed_name()
9091

9192
@classmethod
92-
def configuration(cls) -> Tuple[sublime.Settings, str]:
93+
@override
94+
def configuration(cls) -> tuple[sublime.Settings, str]:
9395
return cls.read_settings()
9496

9597
@classmethod
96-
def additional_variables(cls) -> Dict[str, str]:
98+
@override
99+
def additional_variables(cls) -> dict[str, str]:
97100
return cls.get_additional_variables()
98101

99102
@classmethod
103+
@override
100104
def needs_update_or_installation(cls) -> bool:
101105
if cls.manages_server():
102106
server = cls.get_server()
103107
return bool(server and server.needs_installation())
104108
return False
105109

106110
@classmethod
111+
@override
107112
def install_or_update(cls) -> None:
108113
server = cls.get_server()
109114
if server:
110115
server.install_or_update()
111116

112117
@classmethod
118+
@override
113119
def can_start(cls, window: sublime.Window, initiating_view: sublime.View,
114-
workspace_folders: List[WorkspaceFolder], configuration: ClientConfig) -> Optional[str]:
120+
workspace_folders: list[WorkspaceFolder], configuration: ClientConfig) -> str | None:
115121
if cls.manages_server():
116122
server = cls.get_server()
117123
if not server or server.get_status() == ServerStatus.ERROR:
@@ -127,8 +133,9 @@ def can_start(cls, window: sublime.Window, initiating_view: sublime.View,
127133
return None
128134

129135
@classmethod
136+
@override
130137
def on_pre_start(cls, window: sublime.Window, initiating_view: sublime.View,
131-
workspace_folders: List[WorkspaceFolder], configuration: ClientConfig) -> Optional[str]:
138+
workspace_folders: list[WorkspaceFolder], configuration: ClientConfig) -> str | None:
132139
extra_paths = cls.get_additional_paths()
133140
if extra_paths:
134141
original_path_raw = configuration.env.get('PATH') or ''
@@ -147,10 +154,12 @@ def on_pre_start(cls, window: sublime.Window, initiating_view: sublime.View,
147154
# --- ClientHandlerInterface --------------------------------------------------------------------------------------
148155

149156
@classmethod
157+
@override
150158
def setup(cls) -> None:
151159
register_plugin(cls)
152160

153161
@classmethod
162+
@override
154163
def cleanup(cls) -> None:
155164
unregister_plugin(cls)
156165

st4_py38/lsp_utils/_client_handler/api_decorator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22
from ..api_wrapper_interface import ApiWrapperInterface
33
from .interface import ClientHandlerInterface
4-
from typing import Any, Callable, List, Optional, TypeVar, Union
4+
from typing import Any, Callable, List, TypeVar, Union
55
import inspect
66

77
__all__ = [
@@ -73,7 +73,7 @@ def register_decorated_handlers(client_handler: ClientHandlerInterface, api: Api
7373
"""
7474
for _, func in inspect.getmembers(client_handler, predicate=inspect.isroutine):
7575
for client_event, handler_mark in _HANDLER_MARKS.items():
76-
message_methods: Optional[List[str]] = getattr(func, handler_mark, None)
76+
message_methods: list[str] | None = getattr(func, handler_mark, None)
7777
if message_methods is None:
7878
continue
7979

st4_py38/lsp_utils/_client_handler/interface.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
from LSP.plugin import ClientConfig
77
from LSP.plugin import DottedDict
88
from LSP.plugin import WorkspaceFolder
9-
from typing import Dict, List, Optional, Tuple
109
import sublime
1110

1211
__all__ = ['ClientHandlerInterface']
1312

1413

1514
class ClientHandlerInterface(metaclass=ABCMeta):
16-
package_name = ''
15+
package_name: str = ''
1716

1817
@classmethod
1918
@abstractmethod
@@ -37,12 +36,12 @@ def package_storage(cls) -> str:
3736

3837
@classmethod
3938
@abstractmethod
40-
def get_additional_variables(cls) -> Dict[str, str]:
39+
def get_additional_variables(cls) -> dict[str, str]:
4140
...
4241

4342
@classmethod
4443
@abstractmethod
45-
def get_additional_paths(cls) -> List[str]:
44+
def get_additional_paths(cls) -> list[str]:
4645
...
4746

4847
@classmethod
@@ -52,7 +51,7 @@ def manages_server(cls) -> bool:
5251

5352
@classmethod
5453
@abstractmethod
55-
def get_command(cls) -> List[str]:
54+
def get_command(cls) -> list[str]:
5655
...
5756

5857
@classmethod
@@ -62,17 +61,17 @@ def binary_path(cls) -> str:
6261

6362
@classmethod
6463
@abstractmethod
65-
def get_server(cls) -> Optional[ServerResourceInterface]:
64+
def get_server(cls) -> ServerResourceInterface | None:
6665
...
6766

6867
@classmethod
6968
@abstractmethod
70-
def get_binary_arguments(cls) -> List[str]:
69+
def get_binary_arguments(cls) -> list[str]:
7170
...
7271

7372
@classmethod
7473
@abstractmethod
75-
def read_settings(cls) -> Tuple[sublime.Settings, str]:
74+
def read_settings(cls) -> tuple[sublime.Settings, str]:
7675
...
7776

7877
@classmethod
@@ -86,9 +85,9 @@ def is_allowed_to_start(
8685
cls,
8786
window: sublime.Window,
8887
initiating_view: sublime.View,
89-
workspace_folders: List[WorkspaceFolder],
88+
workspace_folders: list[WorkspaceFolder],
9089
configuration: ClientConfig,
91-
) -> Optional[str]:
90+
) -> str | None:
9291
...
9392

9493
@abstractmethod
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
from __future__ import annotations
2-
from .weak_method import weak_method
32

43
__all__ = [
5-
'weak_method',
64
]

st4_py38/lsp_utils/_util/weak_method.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

st4_py38/lsp_utils/api_wrapper_interface.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
from abc import ABCMeta, abstractmethod
33
from typing import Any, Callable
44

5-
__all__ = ['ApiWrapperInterface']
5+
__all__ = [
6+
'ApiWrapperInterface',
7+
'ApiNotificationHandler',
8+
'ApiRequestHandler',
9+
]
610

711

8-
NotificationHandler = Callable[[Any], None]
9-
RequestHandler = Callable[[Any, Callable[[Any], None]], None]
12+
ApiNotificationHandler = Callable[[Any], None]
13+
ApiRequestHandler = Callable[[Any, Callable[[Any], None]], None]
1014

1115

1216
class ApiWrapperInterface(metaclass=ABCMeta):
@@ -16,14 +20,14 @@ class ApiWrapperInterface(metaclass=ABCMeta):
1620
"""
1721

1822
@abstractmethod
19-
def on_notification(self, method: str, handler: NotificationHandler) -> None:
23+
def on_notification(self, method: str, handler: ApiNotificationHandler) -> None:
2024
"""
2125
Registers a handler for given notification name. The handler will be called with optional params.
2226
"""
2327
...
2428

2529
@abstractmethod
26-
def on_request(self, method: str, handler: RequestHandler) -> None:
30+
def on_request(self, method: str, handler: ApiRequestHandler) -> None:
2731
"""
2832
Registers a handler for given request name. The handler will be called with two arguments - first the params
2933
sent with the request and second the function that must be used to respond to the request. The response

0 commit comments

Comments
 (0)