Add typing information to retrier decorator

This commit is contained in:
Matthias 2022-04-15 15:48:37 +02:00
parent 42ae8ba6fb
commit 07ec3b27fe
4 changed files with 24 additions and 9 deletions

View File

@ -2,6 +2,7 @@ import asyncio
import logging import logging
import time import time
from functools import wraps from functools import wraps
from typing import Any, Callable, Optional, TypeVar, cast, overload
from freqtrade.exceptions import DDosProtection, RetryableOrderError, TemporaryError from freqtrade.exceptions import DDosProtection, RetryableOrderError, TemporaryError
from freqtrade.mixins import LoggingMixin from freqtrade.mixins import LoggingMixin
@ -133,8 +134,22 @@ def retrier_async(f):
return wrapper return wrapper
def retrier(_func=None, retries=API_RETRY_COUNT): F = TypeVar('F', bound=Callable[..., Any])
def decorator(f):
# Type shenanigans
@overload
def retrier(_func: F) -> F:
...
@overload
def retrier(*, retries=API_RETRY_COUNT) -> Callable[[F], F]:
...
def retrier(_func: Optional[F] = None, *, retries=API_RETRY_COUNT):
def decorator(f: F) -> F:
@wraps(f) @wraps(f)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
count = kwargs.pop('count', retries) count = kwargs.pop('count', retries)
@ -155,7 +170,7 @@ def retrier(_func=None, retries=API_RETRY_COUNT):
else: else:
logger.warning(msg + 'Giving up.') logger.warning(msg + 'Giving up.')
raise ex raise ex
return wrapper return cast(F, wrapper)
# Support both @retrier and @retrier(retries=2) syntax # Support both @retrier and @retrier(retries=2) syntax
if _func is None: if _func is None:
return decorator return decorator

View File

@ -1164,7 +1164,7 @@ class Exchange:
raise OperationalException(e) from e raise OperationalException(e) from e
@retrier(retries=API_FETCH_ORDER_RETRY_COUNT) @retrier(retries=API_FETCH_ORDER_RETRY_COUNT)
def fetch_order(self, order_id: str, pair: str, params={}) -> Dict: def fetch_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict:
if self._config['dry_run']: if self._config['dry_run']:
return self.fetch_dry_run_order(order_id) return self.fetch_dry_run_order(order_id)
try: try:
@ -1212,7 +1212,7 @@ class Exchange:
and order.get('filled') == 0.0) and order.get('filled') == 0.0)
@retrier @retrier
def cancel_order(self, order_id: str, pair: str, params={}) -> Dict: def cancel_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict:
if self._config['dry_run']: if self._config['dry_run']:
try: try:
order = self.fetch_dry_run_order(order_id) order = self.fetch_dry_run_order(order_id)

View File

@ -104,7 +104,7 @@ class Ftx(Exchange):
raise OperationalException(e) from e raise OperationalException(e) from e
@retrier(retries=API_FETCH_ORDER_RETRY_COUNT) @retrier(retries=API_FETCH_ORDER_RETRY_COUNT)
def fetch_stoploss_order(self, order_id: str, pair: str) -> Dict: def fetch_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict:
if self._config['dry_run']: if self._config['dry_run']:
return self.fetch_dry_run_order(order_id) return self.fetch_dry_run_order(order_id)
@ -145,7 +145,7 @@ class Ftx(Exchange):
raise OperationalException(e) from e raise OperationalException(e) from e
@retrier @retrier
def cancel_stoploss_order(self, order_id: str, pair: str) -> Dict: def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict:
if self._config['dry_run']: if self._config['dry_run']:
return {} return {}
try: try:

View File

@ -71,14 +71,14 @@ class Gateio(Exchange):
} }
return trades return trades
def fetch_stoploss_order(self, order_id: str, pair: str, params={}) -> Dict: def fetch_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict:
return self.fetch_order( return self.fetch_order(
order_id=order_id, order_id=order_id,
pair=pair, pair=pair,
params={'stop': True} params={'stop': True}
) )
def cancel_stoploss_order(self, order_id: str, pair: str, params={}) -> Dict: def cancel_stoploss_order(self, order_id: str, pair: str, params: Dict = {}) -> Dict:
return self.cancel_order( return self.cancel_order(
order_id=order_id, order_id=order_id,
pair=pair, pair=pair,