Skip to content

Commit f7f6837

Browse files
committed
added cast and initial_value methods to datatypes
1 parent b8add05 commit f7f6837

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/fastcs/attributes.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from enum import Enum
55
from typing import Any, Generic, Protocol, runtime_checkable
66

7-
from .datatypes import ATTRIBUTE_TYPES, AttrCallback, DataType, T, validate_value
7+
from .datatypes import ATTRIBUTE_TYPES, AttrCallback, DataType, T
88

99

1010
class AttrMode(Enum):
@@ -126,15 +126,17 @@ def __init__(
126126
allowed_values=allowed_values, # type: ignore
127127
description=description,
128128
)
129-
self._value: T = datatype.dtype() if initial_value is None else initial_value
129+
self._value: T = (
130+
datatype.initial_value if initial_value is None else initial_value
131+
)
130132
self._update_callback: AttrCallback[T] | None = None
131133
self._updater = handler
132134

133135
def get(self) -> T:
134136
return self._value
135137

136138
async def set(self, value: T) -> None:
137-
self._value = self._datatype.dtype(validate_value(self._datatype, value))
139+
self._value = self._datatype.cast(value)
138140

139141
if self._update_callback is not None:
140142
await self._update_callback(self._value)
@@ -177,11 +179,11 @@ async def process(self, value: T) -> None:
177179

178180
async def process_without_display_update(self, value: T) -> None:
179181
if self._process_callback is not None:
180-
await self._process_callback(self._datatype.dtype(value))
182+
await self._process_callback(self._datatype.cast(value))
181183

182184
async def update_display_without_process(self, value: T) -> None:
183185
if self._write_display_callback is not None:
184-
await self._write_display_callback(self._datatype.dtype(value))
186+
await self._write_display_callback(self._datatype.cast(value))
185187

186188
def set_process_callback(self, callback: AttrCallback[T] | None) -> None:
187189
self._process_callback = callback
@@ -221,6 +223,6 @@ def __init__(
221223
)
222224

223225
async def process(self, value: T) -> None:
224-
await self.set(validate_value(self._datatype, value))
226+
await self.set(value)
225227

226228
await super().process(value) # type: ignore

src/fastcs/datatypes.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ class DataType(Generic[T]):
2121
def dtype(self) -> type[T]: # Using property due to lack of Generic ClassVars
2222
pass
2323

24+
def cast(self, value: T) -> T:
25+
"""Cast a value to a more primative datatype for `Attribute` push.
26+
27+
Also validate it against fields in the datatype.
28+
"""
29+
return self.dtype(value)
30+
31+
@property
32+
def initial_value(self) -> T:
33+
return self.dtype()
34+
2435

2536
T_Numerical = TypeVar("T_Numerical", int, float)
2637

@@ -33,6 +44,13 @@ class _Numerical(DataType[T_Numerical]):
3344
min_alarm: int | None = None
3445
max_alarm: int | None = None
3546

47+
def cast(self, value: T_Numerical) -> T_Numerical:
48+
if self.min is not None and value < self.min:
49+
raise ValueError(f"Value {value} is less than minimum {self.min}")
50+
if self.max is not None and value > self.max:
51+
raise ValueError(f"Value {value} is greater than maximum {self.max}")
52+
return value
53+
3654

3755
@dataclass(frozen=True)
3856
class Int(_Numerical[int]):
@@ -73,15 +91,3 @@ class String(DataType[str]):
7391
@property
7492
def dtype(self) -> type[str]:
7593
return str
76-
77-
78-
def validate_value(datatype: DataType[T], value: T) -> T:
79-
"""Validate a value against a datatype."""
80-
81-
if isinstance(datatype, (Int | Float)):
82-
assert isinstance(value, (int | float)), f"Value {value} is not a number"
83-
if datatype.min is not None and value < datatype.min:
84-
raise ValueError(f"Value {value} is less than minimum {datatype.min}")
85-
if datatype.max is not None and value > datatype.max:
86-
raise ValueError(f"Value {value} is greater than maximum {datatype.max}")
87-
return value

0 commit comments

Comments
 (0)