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

View File

@ -1164,7 +1164,7 @@ class Exchange:
raise OperationalException(e) from e
@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']:
return self.fetch_dry_run_order(order_id)
try:
@ -1212,7 +1212,7 @@ class Exchange:
and order.get('filled') == 0.0)
@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']:
try:
order = self.fetch_dry_run_order(order_id)

View File

@ -104,7 +104,7 @@ class Ftx(Exchange):
raise OperationalException(e) from e
@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']:
return self.fetch_dry_run_order(order_id)
@ -145,7 +145,7 @@ class Ftx(Exchange):
raise OperationalException(e) from e
@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']:
return {}
try:

View File

@ -71,14 +71,14 @@ class Gateio(Exchange):
}
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(
order_id=order_id,
pair=pair,
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(
order_id=order_id,
pair=pair,