Coverage for hdl_registers/constant/boolean_constant.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-29 06:41 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the hdl-registers project, an HDL register generator fast enough to run 

5# in real time. 

6# https://hdl-registers.com 

7# https://github.com/hdl-registers/hdl-registers 

8# -------------------------------------------------------------------------------------------------- 

9 

10 

11from .constant import Constant 

12 

13 

14class BooleanConstant(Constant): 

15 """ 

16 Represent a boolean constant. 

17 See :ref:`constant_boolean` for details. 

18 """ 

19 

20 def __init__(self, name: str, value: bool, description: str = "") -> None: 

21 """ 

22 Arguments: 

23 name: The name of the constant. 

24 value: The constant value. 

25 description: Textual description for the constant. 

26 """ 

27 self.name = name 

28 self.description = description 

29 

30 self._value = False 

31 # Assign self._value via setter 

32 self.value = value 

33 

34 @property 

35 def value(self) -> bool: 

36 """ 

37 Getter for value. 

38 """ 

39 return self._value 

40 

41 @value.setter 

42 def value(self, value: bool) -> None: 

43 """ 

44 Setter for value that performs sanity checks. 

45 """ 

46 if not isinstance(value, bool): 

47 raise TypeError( 

48 f'Constant "{self.name}" has invalid data type "{type(value)}". Value: "{value}".' 

49 ) 

50 

51 self._value = value 

52 

53 def __repr__(self) -> str: 

54 return f"""{self.__class__.__name__}(\ 

55name={self.name},\ 

56value={self.value},\ 

57description={self.description},\ 

58)"""