Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: more specific types for decorators #89

Merged
merged 1 commit into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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