Make BaseParameter get_space abstract

This commit is contained in:
Matthias 2021-03-28 19:31:54 +02:00
parent 8022386404
commit 20f7e9b4b7
2 changed files with 8 additions and 5 deletions

View File

@ -3,6 +3,7 @@ IHyperStrategy interface, hyperoptable Parameter class.
This module defines a base class for auto-hyperoptable strategies. This module defines a base class for auto-hyperoptable strategies.
""" """
import logging import logging
from abc import ABC, abstractmethod
from contextlib import suppress from contextlib import suppress
from typing import Any, Iterator, Optional, Sequence, Tuple, Union from typing import Any, Iterator, Optional, Sequence, Tuple, Union
@ -16,7 +17,7 @@ from freqtrade.exceptions import OperationalException
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class BaseParameter(object): class BaseParameter(ABC):
""" """
Defines a parameter that can be optimized by hyperopt. Defines a parameter that can be optimized by hyperopt.
""" """
@ -46,8 +47,11 @@ class BaseParameter(object):
def __repr__(self): def __repr__(self):
return f'{self.__class__.__name__}({self.value})' return f'{self.__class__.__name__}({self.value})'
@abstractmethod
def get_space(self, name: str) -> Union['Integer', 'Real', 'Categorical']: def get_space(self, name: str) -> Union['Integer', 'Real', 'Categorical']:
raise NotImplementedError() """
Get-space - will be used by Hyperopt to get the hyperopt Space
"""
class IntParameter(BaseParameter): class IntParameter(BaseParameter):

View File

@ -571,9 +571,8 @@ def test_hyperopt_parameters():
with pytest.raises(OperationalException, match=r"FloatParameter space invalid\."): with pytest.raises(OperationalException, match=r"FloatParameter space invalid\."):
FloatParameter([0, 10], high=7, default=5, space='buy') FloatParameter([0, 10], high=7, default=5, space='buy')
x = BaseParameter(opt_range=[0, 1], default=1, space='buy') with pytest.raises(TypeError):
with pytest.raises(NotImplementedError): BaseParameter(opt_range=[0, 1], default=1, space='buy')
x.get_space('space')
fltpar = IntParameter(low=0, high=5, default=1, space='buy') fltpar = IntParameter(low=0, high=5, default=1, space='buy')
assert fltpar.value == 1 assert fltpar.value == 1