Skip to content

Commit

Permalink
server: more specific types for decorators
Browse files Browse the repository at this point in the history
Use a TypeVar so that type checkers understand that the signature is
same as the decorated functions, so that these functions can be
typechecked.

fixes #88
  • Loading branch information
perrinjerome committed Jan 26, 2020
1 parent ad1a1ba commit ede6d03
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning][semver].

## [Unreleased]

### Changed

- Fixed `@command`, `@feature` and `@thread` decorators to retain type of wrapped functions ([#89])

[#89]: https://github.com/openlawlibrary/pygls/pull/89

### Added

- Add comparisons and repr support to Range and Location types ([#90])
Expand Down
10 changes: 6 additions & 4 deletions pygls/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from concurrent.futures import Future, ThreadPoolExecutor
from multiprocessing.pool import ThreadPool
from threading import Event
from typing import Callable, Dict, List
from typing import Callable, Dict, List, TypeVar

from pygls.types import (ApplyWorkspaceEditResponse, ConfigCallbackType, Diagnostic, MessageType,
RegistrationParams, TextDocumentSyncKind, UnregistrationParams,
Expand All @@ -34,6 +34,8 @@

logger = logging.getLogger(__name__)

F = TypeVar('F', bound=Callable)


async def aio_readline(loop, executor, stop_event, rfile, proxy):
"""Reads data from stdin in separate thread (asynchronously)."""
Expand Down Expand Up @@ -234,7 +236,7 @@ def apply_edit(self, edit: WorkspaceEdit, label: str = None) -> ApplyWorkspaceEd
"""Sends apply edit request to the client."""
return self.lsp.apply_edit(edit, label)

def command(self, command_name: str) -> Callable:
def command(self, command_name: str) -> Callable[[F], F]:
"""Decorator used to register custom commands.
Example:
Expand All @@ -244,7 +246,7 @@ def my_cmd(ls, a, b, c):
"""
return self.lsp.fm.command(command_name)

def feature(self, feature_name: str, **options: Dict) -> Callable:
def feature(self, feature_name: str, **options: Dict) -> Callable[[F], F]:
"""Decorator used to register LSP features.
Example:
Expand Down Expand Up @@ -287,7 +289,7 @@ def show_message_log(self, message, msg_type=MessageType.Log) -> None:
"""Sends message to the client's output channel."""
self.lsp.show_message_log(message, msg_type)

def thread(self) -> Callable:
def thread(self) -> Callable[[F], F]:
"""Decorator that mark function to execute it in a thread."""
return self.lsp.thread()

Expand Down

0 comments on commit ede6d03

Please sign in to comment.