Coverage for src / c41811 / config / basic / number.py: 100%
54 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-06 06:04 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-06 06:04 +0000
1# cython: language_level = 3 # noqa: ERA001
4"""
5数值类型配置数据实现
7.. versionadded:: 0.2.0
8"""
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
19from ._generate_operators import generate
20from ._generate_operators import operate
21from .core import BasicSingleConfigData
24@generate
25class NumberConfigData[D: int | float | Number](BasicSingleConfigData[D]):
26 """
27 数值配置数据
29 .. versionadded:: 0.1.5
30 """
32 _data: D
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))
43 @property
44 @override
45 def data_read_only(self) -> Literal[False]:
46 """
47 配置数据是否为只读
49 :return: 配置数据是否为只读
50 :rtype: Literal[False]
52 .. note::
53 该配置数据类始终认为配置数据非只读,使其能正确作为配置数据容器使用
54 """ # noqa: RUF002
55 return False
57 @property # type: ignore[explicit-override] # mypy抽风
58 @override
59 def data(self) -> D:
60 """
61 配置的原始数据
63 .. caution::
64 未默认做深拷贝,可能导致非预期的行为
66 .. versionchanged:: 0.3.0
67 现在是可写属性
68 """ # noqa: RUF002
69 return self._data
71 @data.setter
72 def data(self, data: D) -> None:
73 self._data = data
75 def __int__(self) -> int:
76 return int(self._data) # type: ignore[arg-type]
78 def __float__(self) -> float:
79 return float(self._data) # type: ignore[arg-type]
81 @operate(operator.add, operator.iadd)
82 def __add__(self, other: Any) -> Self: # type: ignore[empty-body]
83 ...
85 @operate(operator.sub, operator.isub)
86 def __sub__(self, other: Any) -> Self: # type: ignore[empty-body]
87 ...
89 @operate(operator.mul, operator.imul)
90 def __mul__(self, other: Any) -> Self: # type: ignore[empty-body]
91 ...
93 @operate(operator.truediv, operator.itruediv)
94 def __truediv__(self, other: Any) -> Self: # type: ignore[empty-body]
95 ...
97 @operate(operator.floordiv, operator.ifloordiv)
98 def __floordiv__(self, other: Any) -> Self: # type: ignore[empty-body]
99 ...
101 @operate(operator.mod, operator.imod)
102 def __mod__(self, other: Any) -> Self: # type: ignore[empty-body]
103 ...
105 @operate(operator.pow, operator.ipow)
106 def __pow__(self, other: Any) -> Self: # type: ignore[empty-body]
107 ...
109 @operate(operator.and_, operator.iand)
110 def __and__(self, other: Any) -> Self: # type: ignore[empty-body]
111 ...
113 @operate(operator.or_, operator.ior)
114 def __or__(self, other: Any) -> Self: # type: ignore[empty-body]
115 ...
117 @operate(operator.xor, operator.ixor)
118 def __xor__(self, other: Any) -> Self: # type: ignore[empty-body]
119 ...
121 @operate(operator.matmul, operator.imatmul)
122 def __matmul__(self, other: Any) -> Self: # type: ignore[empty-body]
123 ...
125 @operate(operator.lshift, operator.ilshift)
126 def __lshift__(self, other: Any) -> Self: # type: ignore[empty-body]
127 ...
129 @operate(operator.rshift, operator.irshift)
130 def __rshift__(self, other: Any) -> Self: # type: ignore[empty-body]
131 ...
133 def __radd__(self, other: Any) -> Self: # type: ignore[empty-body]
134 ...
136 def __rsub__(self, other: Any) -> Self: # type: ignore[empty-body]
137 ...
139 def __rmul__(self, other: Any) -> Self: # type: ignore[empty-body]
140 ...
142 def __rtruediv__(self, other: Any) -> Self: # type: ignore[empty-body]
143 ...
145 def __rfloordiv__(self, other: Any) -> Self: # type: ignore[empty-body]
146 ...
148 def __rmod__(self, other: Any) -> Self: # type: ignore[empty-body]
149 ...
151 def __rpow__(self, other: Any) -> Self: # type: ignore[empty-body]
152 ...
154 def __rand__(self, other: Any) -> Self: # type: ignore[empty-body]
155 ...
157 def __ror__(self, other: Any) -> Self: # type: ignore[empty-body]
158 ...
160 def __rxor__(self, other: Any) -> Self: # type: ignore[empty-body]
161 ...
163 def __rmatmul__(self, other: Any) -> Self: # type: ignore[empty-body]
164 ...
166 def __rlshift__(self, other: Any) -> Self: # type: ignore[empty-body]
167 ...
169 def __rrshift__(self, other: Any) -> Self: # type: ignore[empty-body]
170 ...
172 def __invert__(self) -> Any:
173 return ~self._data # type: ignore[operator]
175 def __neg__(self) -> Any:
176 return -self._data # type: ignore[operator]
178 def __pos__(self) -> Any:
179 return +self._data # type: ignore[operator]
181 def __abs__(self) -> D:
182 return abs(self._data) # type: ignore[return-value,arg-type]
184 # noinspection SpellCheckingInspection
185 def __round__(self, ndigits: int | None = None) -> Any:
186 return round(self._data, ndigits) # type: ignore[arg-type]
188 def __trunc__(self) -> Any:
189 return math.trunc(self._data) # type: ignore[arg-type]
191 def __floor__(self) -> Any:
192 return math.floor(self._data) # type: ignore[arg-type]
194 def __ceil__(self) -> Any:
195 return math.ceil(self._data) # type: ignore[arg-type]
197 def __index__(self) -> Any:
198 return self._data.__index__() # type: ignore[union-attr]
201class BoolConfigData[D: bool](NumberConfigData[D]):
202 # noinspection GrazieInspection
203 """
204 布尔值配置数据
206 .. versionadded:: 0.1.5
208 .. versionchanged:: 0.2.0
209 直接对参数调用 :py:class:`bool`
210 """
212 _data: D
213 data: D
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)))
223__all__ = (
224 "BoolConfigData",
225 "NumberConfigData",
226)