mypy: Make decorators.py pass --strict
This commit is contained in:
parent
e662bdee05
commit
6425daae1e
1 changed files with 38 additions and 25 deletions
|
@ -1,54 +1,68 @@
|
||||||
"""
|
"""
|
||||||
Module containing various decorators
|
Module containing various decorators
|
||||||
"""
|
"""
|
||||||
from typing import Any, Callable, List, Optional
|
from typing import (
|
||||||
|
cast,
|
||||||
|
Any,
|
||||||
|
Callable,
|
||||||
|
List,
|
||||||
|
Optional,
|
||||||
|
TypeVar,
|
||||||
|
TYPE_CHECKING,
|
||||||
|
)
|
||||||
|
|
||||||
from poezio import common
|
from poezio import common
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from poezio.tabs import RosterInfoTab
|
||||||
|
|
||||||
|
T = TypeVar('T', bound=Callable[..., Any])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class RefreshWrapper:
|
class RefreshWrapper:
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.core = None
|
self.core = None
|
||||||
|
|
||||||
def conditional(self, func: Callable) -> Callable:
|
def conditional(self, func: T) -> T:
|
||||||
"""
|
"""
|
||||||
Decorator to refresh the UI if the wrapped function
|
Decorator to refresh the UI if the wrapped function
|
||||||
returns True
|
returns True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def wrap(*args, **kwargs):
|
def wrap(*args: Any, **kwargs: Any) -> Any:
|
||||||
ret = func(*args, **kwargs)
|
ret = func(*args, **kwargs)
|
||||||
if self.core and ret:
|
if self.core and ret:
|
||||||
self.core.refresh_window()
|
self.core.refresh_window()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
return wrap
|
return cast(T, wrap)
|
||||||
|
|
||||||
def always(self, func: Callable) -> Callable:
|
def always(self, func: T) -> T:
|
||||||
"""
|
"""
|
||||||
Decorator that refreshs the UI no matter what after the function
|
Decorator that refreshs the UI no matter what after the function
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def wrap(*args, **kwargs):
|
def wrap(*args: Any, **kwargs: Any) -> Any:
|
||||||
ret = func(*args, **kwargs)
|
ret = func(*args, **kwargs)
|
||||||
if self.core:
|
if self.core:
|
||||||
self.core.refresh_window()
|
self.core.refresh_window()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
return wrap
|
return cast(T, wrap)
|
||||||
|
|
||||||
def update(self, func: Callable) -> Callable:
|
def update(self, func: T) -> T:
|
||||||
"""
|
"""
|
||||||
Decorator that only updates the screen
|
Decorator that only updates the screen
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def wrap(*args, **kwargs):
|
def wrap(*args: Any, **kwargs: Any) -> Any:
|
||||||
ret = func(*args, **kwargs)
|
ret = func(*args, **kwargs)
|
||||||
if self.core:
|
if self.core:
|
||||||
self.core.doupdate()
|
self.core.doupdate()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
return wrap
|
return cast(T, wrap)
|
||||||
|
|
||||||
|
|
||||||
refresh_wrapper = RefreshWrapper()
|
refresh_wrapper = RefreshWrapper()
|
||||||
|
@ -61,32 +75,32 @@ class CommandArgParser:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def raw(func: Callable) -> Callable:
|
def raw(func: T) -> T:
|
||||||
"""Just call the function with a single string, which is the original string
|
"""Just call the function with a single string, which is the original string
|
||||||
untouched
|
untouched
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def wrap(self, args, *a, **kw):
|
def wrap(self: Any, args: Any, *a: Any, **kw: Any) -> Any:
|
||||||
return func(self, args, *a, **kw)
|
return func(self, args, *a, **kw)
|
||||||
|
|
||||||
return wrap
|
return cast(T, wrap)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def ignored(func: Callable) -> Callable:
|
def ignored(func: T) -> T:
|
||||||
"""
|
"""
|
||||||
Call the function without any argument
|
Call the function without any argument
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def wrap(self, args=None, *a, **kw):
|
def wrap(self: Any, args: Any = None, *a: Any, **kw: Any) -> Any:
|
||||||
return func(self, *a, **kw)
|
return func(self, *a, **kw)
|
||||||
|
|
||||||
return wrap
|
return cast(T, wrap)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def quoted(mandatory: int,
|
def quoted(mandatory: int,
|
||||||
optional=0,
|
optional: int = 0,
|
||||||
defaults: Optional[List[Any]] = None,
|
defaults: Optional[List[Any]] = None,
|
||||||
ignore_trailing_arguments=False):
|
ignore_trailing_arguments: bool = False) -> Callable[[T], T]:
|
||||||
"""The function receives a list with a number of arguments that is between
|
"""The function receives a list with a number of arguments that is between
|
||||||
the numbers `mandatory` and `optional`.
|
the numbers `mandatory` and `optional`.
|
||||||
|
|
||||||
|
@ -131,8 +145,8 @@ class CommandArgParser:
|
||||||
"""
|
"""
|
||||||
default_args_outer = defaults or []
|
default_args_outer = defaults or []
|
||||||
|
|
||||||
def first(func: Callable):
|
def first(func: T) -> T:
|
||||||
def second(self, args: str, *a, **kw):
|
def second(self: Any, args: str, *a: Any, **kw: Any) -> Any:
|
||||||
default_args = default_args_outer
|
default_args = default_args_outer
|
||||||
if args and args.strip():
|
if args and args.strip():
|
||||||
split_args = common.shell_split(args)
|
split_args = common.shell_split(args)
|
||||||
|
@ -156,8 +170,7 @@ class CommandArgParser:
|
||||||
res[-1] += " " + " ".join(split_args)
|
res[-1] += " " + " ".join(split_args)
|
||||||
return func(self, res, *a, **kw)
|
return func(self, res, *a, **kw)
|
||||||
|
|
||||||
return second
|
return cast(T, second)
|
||||||
|
|
||||||
return first
|
return first
|
||||||
|
|
||||||
|
|
||||||
|
@ -166,11 +179,11 @@ command_args_parser = CommandArgParser()
|
||||||
|
|
||||||
def deny_anonymous(func: Callable) -> Callable:
|
def deny_anonymous(func: Callable) -> Callable:
|
||||||
"""Decorator to disable commands when using an anonymous account."""
|
"""Decorator to disable commands when using an anonymous account."""
|
||||||
def wrap(self: 'RosterInfoTab', *args, **kwargs):
|
def wrap(self: 'RosterInfoTab', *args: Any, **kwargs: Any) -> Any:
|
||||||
if self.core.xmpp.anon:
|
if self.core.xmpp.anon:
|
||||||
return self.core.information(
|
return self.core.information(
|
||||||
'This command is not available for anonymous accounts.',
|
'This command is not available for anonymous accounts.',
|
||||||
'Info'
|
'Info'
|
||||||
)
|
)
|
||||||
return func(self, *args, **kwargs)
|
return func(self, *args, **kwargs)
|
||||||
return wrap
|
return cast(T, wrap)
|
||||||
|
|
Loading…
Reference in a new issue