Add typehints to strategy wrapper

This commit is contained in:
Matthias 2022-04-16 06:53:38 +02:00
parent 07ec3b27fe
commit 7f4161ff78

View File

@ -1,5 +1,7 @@
import logging import logging
from copy import deepcopy from copy import deepcopy
from functools import wraps
from typing import Any, Callable, TypeVar, cast
from freqtrade.exceptions import StrategyError from freqtrade.exceptions import StrategyError
@ -7,12 +9,16 @@ from freqtrade.exceptions import StrategyError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def strategy_safe_wrapper(f, message: str = "", default_retval=None, supress_error=False): F = TypeVar('F', bound=Callable[..., Any])
def strategy_safe_wrapper(f: F, message: str = "", default_retval=None, supress_error=False) -> F:
""" """
Wrapper around user-provided methods and functions. Wrapper around user-provided methods and functions.
Caches all exceptions and returns either the default_retval (if it's not None) or raises Caches all exceptions and returns either the default_retval (if it's not None) or raises
a StrategyError exception, which then needs to be handled by the calling method. a StrategyError exception, which then needs to be handled by the calling method.
""" """
@wraps(f)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
try: try:
if 'trade' in kwargs: if 'trade' in kwargs:
@ -37,4 +43,4 @@ def strategy_safe_wrapper(f, message: str = "", default_retval=None, supress_err
raise StrategyError(str(error)) from error raise StrategyError(str(error)) from error
return default_retval return default_retval
return wrapper return cast(F, wrapper)