Skip to content

Commit

Permalink
Improve type annotations for decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann committed Oct 26, 2023
1 parent 024e4ba commit 74d4c09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
17 changes: 10 additions & 7 deletions plugin/core/input_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union
from .typing import cast
from .typing import Any, Callable, Dict, List, Optional, ParamSpec, Tuple, Union
from .typing import final
from abc import ABCMeta
from abc import abstractmethod
Expand All @@ -15,15 +14,19 @@
ListItemsReturn = Union[List[str], Tuple[List[str], int], List[Tuple[str, Any]], Tuple[List[Tuple[str, Any]], int],
List[sublime.ListInputItem], Tuple[List[sublime.ListInputItem], int]]

T_Callable = TypeVar('T_Callable', bound=Callable[..., Any])
P = ParamSpec('P')


def debounced(user_function: T_Callable) -> T_Callable:
""" Yet another debounce implementation, to be used as a decorator for the debounced function. """
def debounced(user_function: Callable[P, Any]) -> Callable[P, None]:
""" A decorator which debounces the calls to a function.
Note that the return value of the function will be discarded, so it only makes sense to use this decorator for
functions that return None.
"""
DEBOUNCE_TIME = 0.5 # seconds

@functools.wraps(user_function)
def wrapped_function(*args: Any, **kwargs: Any) -> None:
def wrapped_function(*args: P.args, **kwargs: P.kwargs) -> None:
def call_function():
if hasattr(wrapped_function, '_timer'):
delattr(wrapped_function, '_timer')
Expand All @@ -35,7 +38,7 @@ def call_function():
timer.start()
setattr(wrapped_function, '_timer', timer)
setattr(wrapped_function, '_timer', None)
return cast(T_Callable, wrapped_function)
return wrapped_function


class PreselectedListInputHandler(sublime_plugin.ListInputHandler, metaclass=ABCMeta):
Expand Down
9 changes: 8 additions & 1 deletion plugin/core/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import Mapping
from typing import NotRequired
from typing import Optional
from typing import ParamSpec
from typing import Protocol
from typing import Required
from typing import Sequence
Expand All @@ -35,7 +36,7 @@
def cast(typ, val): # type: ignore
return val

def final(func):
def final(func): # type: ignore
return func

def _make_type(name: str) -> '_TypeMeta':
Expand Down Expand Up @@ -139,3 +140,9 @@ class NotRequired(Type): # type: ignore

def TypeVar(*args, **kwargs) -> Any: # type: ignore
return object

class ParamSpec(Type): # type: ignore
args = ...
kwargs = ...
def __init__(*args, **kwargs) -> None: # type: ignore
pass

0 comments on commit 74d4c09

Please sign in to comment.