From fe62a71f4c621aa06cfbda1f2aec19ab01062ebd Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 12 Mar 2022 10:57:03 +0100 Subject: [PATCH] Simplify implementation of "check_override" by extracting it to function --- freqtrade/resolvers/strategy_resolver.py | 25 +++++++++++++++++------- tests/strategy/test_strategy_loading.py | 1 + 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/freqtrade/resolvers/strategy_resolver.py b/freqtrade/resolvers/strategy_resolver.py index 456e0fee1..7610b6fe8 100644 --- a/freqtrade/resolvers/strategy_resolver.py +++ b/freqtrade/resolvers/strategy_resolver.py @@ -222,18 +222,22 @@ class StrategyResolver(IResolver): if strategy: if strategy.config.get('trading_mode', TradingMode.SPOT) != TradingMode.SPOT: # Require new method - if type(strategy).populate_entry_trend == IStrategy.populate_entry_trend: + if check_override(strategy, IStrategy, 'populate_entry_trend'): raise OperationalException("`populate_entry_trend` must be implemented.") - if type(strategy).populate_exit_trend == IStrategy.populate_exit_trend: + if check_override(strategy, IStrategy, 'populate_exit_trend'): raise OperationalException("`populate_exit_trend` must be implemented.") else: - # TODO: Implementing buy_trend and sell_trend should raise a deprecation. - if (type(strategy).populate_buy_trend == IStrategy.populate_buy_trend - and type(strategy).populate_entry_trend == IStrategy.populate_entry_trend): + # TODO: Implementing buy_trend and sell_trend should show a deprecation warning + if ( + check_override(strategy, IStrategy, 'populate_buy_trend') + and check_override(strategy, IStrategy, 'populate_entry_trend') + ): raise OperationalException( "`populate_entry_trend` or `populate_buy_trend` must be implemented.") - if (type(strategy).populate_sell_trend == IStrategy.populate_sell_trend - and type(strategy).populate_exit_trend == IStrategy.populate_exit_trend): + if ( + check_override(strategy, IStrategy, 'populate_sell_trend') + and check_override(strategy, IStrategy, 'populate_exit_trend') + ): raise OperationalException( "`populate_exit_trend` or `populate_sell_trend` must be implemented.") @@ -253,3 +257,10 @@ class StrategyResolver(IResolver): f"Impossible to load Strategy '{strategy_name}'. This class does not exist " "or contains Python code errors." ) + + +def check_override(object, parentclass, attribute): + """ + Checks if a object overrides the parent class attribute. + """ + return getattr(type(object), attribute) == getattr(parentclass, attribute) diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index 955db038b..7e0c796f4 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -417,6 +417,7 @@ def test_missing_implements(result, default_conf): match=r"`populate_entry_trend` must be implemented.*"): StrategyResolver.load_strategy(default_conf) + @pytest.mark.filterwarnings("ignore:deprecated") def test_call_deprecated_function(result, default_conf, caplog): default_location = Path(__file__).parent / "strats"