Coverage for src / c41811 / config / basic / number.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-09 01:06 +0000

1# cython: language_level = 3 # noqa: ERA001 

2 

3 

4""" 

5数值类型配置数据实现 

6 

7.. versionadded:: 0.2.0 

8""" 

9 

10import math 

11import operator 

12from numbers import Number 

13from typing import Any 

14from typing import Literal 

15from typing import Self 

16from typing import cast 

17from typing import override 

18 

19from ._generate_operators import generate 

20from ._generate_operators import operate 

21from .core import BasicSingleConfigData 

22 

23 

24@generate 

25class NumberConfigData[D: int | float | Number](BasicSingleConfigData[D]): 

26 """ 

27 数值配置数据 

28 

29 .. versionadded:: 0.1.5 

30 """ 

31 

32 _data: D 

33 

34 def __init__(self, data: D | None = None): 

35 """ 

36 :param data: 数值数据 

37 :type data: D | None 

38 """ # noqa: D205 

39 if data is None: 

40 data = 0 # type: ignore[assignment] 

41 super().__init__(cast(D, data)) 

42 

43 @property 

44 @override 

45 def data_read_only(self) -> Literal[False]: 

46 """ 

47 配置数据是否为只读 

48 

49 :return: 配置数据是否为只读 

50 :rtype: Literal[False] 

51 

52 .. note:: 

53 该配置数据类始终认为配置数据非只读,使其能正确作为配置数据容器使用 

54 """ # noqa: RUF002 

55 return False 

56 

57 @property # type: ignore[explicit-override] # mypy抽风 

58 @override 

59 def data(self) -> D: 

60 """ 

61 配置的原始数据 

62 

63 .. caution:: 

64 未默认做深拷贝,可能导致非预期的行为 

65 

66 .. versionchanged:: 0.3.0 

67 现在是可写属性 

68 """ # noqa: RUF002 

69 return self._data 

70 

71 @data.setter 

72 def data(self, data: D) -> None: 

73 self._data = data 

74 

75 def __int__(self) -> int: 

76 return int(self._data) # type: ignore[arg-type] 

77 

78 def __float__(self) -> float: 

79 return float(self._data) # type: ignore[arg-type] 

80 

81 @operate(operator.add, operator.iadd) 

82 def __add__(self, other: Any) -> Self: # type: ignore[empty-body] 

83 ... 

84 

85 @operate(operator.sub, operator.isub) 

86 def __sub__(self, other: Any) -> Self: # type: ignore[empty-body] 

87 ... 

88 

89 @operate(operator.mul, operator.imul) 

90 def __mul__(self, other: Any) -> Self: # type: ignore[empty-body] 

91 ... 

92 

93 @operate(operator.truediv, operator.itruediv) 

94 def __truediv__(self, other: Any) -> Self: # type: ignore[empty-body] 

95 ... 

96 

97 @operate(operator.floordiv, operator.ifloordiv) 

98 def __floordiv__(self, other: Any) -> Self: # type: ignore[empty-body] 

99 ... 

100 

101 @operate(operator.mod, operator.imod) 

102 def __mod__(self, other: Any) -> Self: # type: ignore[empty-body] 

103 ... 

104 

105 @operate(operator.pow, operator.ipow) 

106 def __pow__(self, other: Any) -> Self: # type: ignore[empty-body] 

107 ... 

108 

109 @operate(operator.and_, operator.iand) 

110 def __and__(self, other: Any) -> Self: # type: ignore[empty-body] 

111 ... 

112 

113 @operate(operator.or_, operator.ior) 

114 def __or__(self, other: Any) -> Self: # type: ignore[empty-body] 

115 ... 

116 

117 @operate(operator.xor, operator.ixor) 

118 def __xor__(self, other: Any) -> Self: # type: ignore[empty-body] 

119 ... 

120 

121 @operate(operator.matmul, operator.imatmul) 

122 def __matmul__(self, other: Any) -> Self: # type: ignore[empty-body] 

123 ... 

124 

125 @operate(operator.lshift, operator.ilshift) 

126 def __lshift__(self, other: Any) -> Self: # type: ignore[empty-body] 

127 ... 

128 

129 @operate(operator.rshift, operator.irshift) 

130 def __rshift__(self, other: Any) -> Self: # type: ignore[empty-body] 

131 ... 

132 

133 def __radd__(self, other: Any) -> Self: # type: ignore[empty-body] 

134 ... 

135 

136 def __rsub__(self, other: Any) -> Self: # type: ignore[empty-body] 

137 ... 

138 

139 def __rmul__(self, other: Any) -> Self: # type: ignore[empty-body] 

140 ... 

141 

142 def __rtruediv__(self, other: Any) -> Self: # type: ignore[empty-body] 

143 ... 

144 

145 def __rfloordiv__(self, other: Any) -> Self: # type: ignore[empty-body] 

146 ... 

147 

148 def __rmod__(self, other: Any) -> Self: # type: ignore[empty-body] 

149 ... 

150 

151 def __rpow__(self, other: Any) -> Self: # type: ignore[empty-body] 

152 ... 

153 

154 def __rand__(self, other: Any) -> Self: # type: ignore[empty-body] 

155 ... 

156 

157 def __ror__(self, other: Any) -> Self: # type: ignore[empty-body] 

158 ... 

159 

160 def __rxor__(self, other: Any) -> Self: # type: ignore[empty-body] 

161 ... 

162 

163 def __rmatmul__(self, other: Any) -> Self: # type: ignore[empty-body] 

164 ... 

165 

166 def __rlshift__(self, other: Any) -> Self: # type: ignore[empty-body] 

167 ... 

168 

169 def __rrshift__(self, other: Any) -> Self: # type: ignore[empty-body] 

170 ... 

171 

172 def __invert__(self) -> Any: 

173 return ~self._data # type: ignore[operator] 

174 

175 def __neg__(self) -> Any: 

176 return -self._data # type: ignore[operator] 

177 

178 def __pos__(self) -> Any: 

179 return +self._data # type: ignore[operator] 

180 

181 def __abs__(self) -> D: 

182 return abs(self._data) # type: ignore[return-value,arg-type] 

183 

184 # noinspection SpellCheckingInspection 

185 def __round__(self, ndigits: int | None = None) -> Any: 

186 return round(self._data, ndigits) # type: ignore[arg-type] 

187 

188 def __trunc__(self) -> Any: 

189 return math.trunc(self._data) # type: ignore[arg-type] 

190 

191 def __floor__(self) -> Any: 

192 return math.floor(self._data) # type: ignore[arg-type] 

193 

194 def __ceil__(self) -> Any: 

195 return math.ceil(self._data) # type: ignore[arg-type] 

196 

197 def __index__(self) -> Any: 

198 return self._data.__index__() # type: ignore[union-attr] 

199 

200 

201class BoolConfigData[D: bool](NumberConfigData[D]): 

202 # noinspection GrazieInspection 

203 """ 

204 布尔值配置数据 

205 

206 .. versionadded:: 0.1.5 

207 

208 .. versionchanged:: 0.2.0 

209 直接对参数调用 :py:class:`bool` 

210 """ 

211 

212 _data: D 

213 data: D 

214 

215 def __init__(self, data: D | None = None): 

216 """ 

217 :param data: 布尔值数据 

218 :type data: D | None 

219 """ # noqa: D205 

220 super().__init__(cast(D, bool(data))) 

221 

222 

223__all__ = ( 

224 "BoolConfigData", 

225 "NumberConfigData", 

226)