Coverage for stricto/float.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-08 17:22 +0100

1"""Module providing the Float() Class""" 

2from .generic import GenericType 

3from .error import Error, ErrorType 

4 

5 

6class Float(GenericType): 

7 """ 

8 A Float type 

9 """ 

10 

11 def __init__(self, **kwargs): 

12 """ 

13 available arguments 

14 

15 min : minimal value 

16 max : maximal value 

17 

18 """ 

19 GenericType.__init__(self, **kwargs) 

20 self._min = kwargs.pop("min", kwargs.pop("minimum", None)) 

21 self._max = kwargs.pop("max", kwargs.pop("maximum", None)) 

22 

23 def check_type(self, value): 

24 if isinstance(value, (float, Float)): 

25 return True 

26 raise Error(ErrorType.WRONGTYPE, "Must be a float", self.path_name()) 

27 

28 def check_constraints(self, value): 

29 

30 GenericType.check_constraints(self, value) 

31 

32 if self._min is not None and value < self._min: 

33 raise Error(ErrorType.LENGTH, "Must be above Minimal", self.path_name()) 

34 if self._max is not None and value > self._max: 

35 raise Error(ErrorType.LENGTH, "Must be below Maximal", self.path_name()) 

36 return True