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 6, 2020
1 parent d0cc817 commit 34d8d69
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 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,7 @@

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 +235,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 +245,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

0 comments on commit 34d8d69

Please sign in to comment.