Skip to content

Commit f30128b

Browse files
authored
Use SimpleHandler on AttrRW only (#186)
1 parent ee8a5e2 commit f30128b

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/fastcs/attributes.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,7 @@ def __init__(
187187
)
188188
self._process_callbacks: list[AttrCallback[T]] | None = None
189189
self._write_display_callbacks: list[AttrCallback[T]] | None = None
190-
191-
if handler is not None:
192-
self._setter = handler
193-
else:
194-
self._setter = SimpleAttrHandler()
190+
self._setter = handler
195191

196192
async def process(self, value: T) -> None:
197193
await self.process_without_display_update(value)
@@ -221,7 +217,7 @@ def add_write_display_callback(self, callback: AttrCallback[T]) -> None:
221217
self._write_display_callbacks.append(callback)
222218

223219
@property
224-
def sender(self) -> AttrHandlerW:
220+
def sender(self) -> AttrHandlerW | None:
225221
return self._setter
226222

227223

@@ -241,7 +237,7 @@ def __init__(
241237
datatype, # type: ignore
242238
access_mode,
243239
group=group,
244-
handler=handler,
240+
handler=handler if handler else SimpleAttrHandler(),
245241
initial_value=initial_value,
246242
description=description,
247243
)

tests/example_p4p_ioc.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import numpy as np
55

6-
from fastcs.attributes import AttrR, AttrRW, AttrW
6+
from fastcs.attributes import AttrHandlerW, AttrR, AttrRW, AttrW
77
from fastcs.controller import Controller, SubController
88
from fastcs.datatypes import Bool, Enum, Float, Int, Table, Waveform
99
from fastcs.launch import FastCS
@@ -14,6 +14,11 @@
1414
from fastcs.wrappers import command, scan
1515

1616

17+
class SimpleAttributeSetter(AttrHandlerW):
18+
async def put(self, attr, value):
19+
await attr.update_display_without_process(value)
20+
21+
1722
class FEnum(enum.Enum):
1823
A = 0
1924
B = 1
@@ -25,7 +30,7 @@ class FEnum(enum.Enum):
2530
class ParentController(Controller):
2631
description = "some controller"
2732
a: AttrRW = AttrRW(Int(max=400_000, max_alarm=40_000))
28-
b: AttrW = AttrW(Float(min=-1, min_alarm=-0.5))
33+
b: AttrW = AttrW(Float(min=-1, min_alarm=-0.5), handler=SimpleAttributeSetter())
2934

3035
table: AttrRW = AttrRW(
3136
Table([("A", np.int32), ("B", "i"), ("C", "?"), ("D", np.float64)])
@@ -34,7 +39,7 @@ class ParentController(Controller):
3439

3540
class ChildController(SubController):
3641
fail_on_next_e = True
37-
c: AttrW = AttrW(Int())
42+
c: AttrW = AttrW(Int(), handler=SimpleAttributeSetter())
3843

3944
@command()
4045
async def d(self):

tests/test_attribute.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import pytest
44
from pytest_mock import MockerFixture
55

6-
from fastcs.attributes import AttrHandlerR, AttrHandlerRW, AttrR, AttrRW, AttrW
6+
from fastcs.attributes import (
7+
AttrHandlerR,
8+
AttrHandlerRW,
9+
AttrR,
10+
AttrRW,
11+
AttrW,
12+
)
713
from fastcs.datatypes import Int, String
814

915

@@ -34,29 +40,23 @@ async def device_add():
3440
assert ui["number"] == 2
3541

3642

37-
@pytest.mark.asyncio
38-
async def test_simple_handler_w(mocker: MockerFixture):
39-
attr = AttrW(Int())
40-
update_display_mock = mocker.patch.object(attr, "update_display_without_process")
41-
42-
# This is called by the transport when it receives a put
43-
await attr.sender.put(attr, 1)
44-
45-
# The callback to update the transport display should be called
46-
update_display_mock.assert_called_once_with(1)
47-
48-
4943
@pytest.mark.asyncio
5044
async def test_simple_handler_rw(mocker: MockerFixture):
5145
attr = AttrRW(Int())
52-
update_display_mock = mocker.patch.object(attr, "update_display_without_process")
53-
set_mock = mocker.patch.object(attr, "set")
5446

47+
attr.update_display_without_process = mocker.MagicMock(
48+
wraps=attr.update_display_without_process
49+
)
50+
attr.set = mocker.MagicMock(wraps=attr.set)
51+
52+
assert attr.sender
53+
# This is called by the transport when it receives a put
5554
await attr.sender.put(attr, 1)
5655

57-
update_display_mock.assert_called_once_with(1)
5856
# The Sender of the attribute should just set the value on the attribute
59-
set_mock.assert_awaited_once_with(1)
57+
attr.update_display_without_process.assert_called_once_with(1)
58+
attr.set.assert_called_once_with(1)
59+
assert attr.get() == 1
6060

6161

6262
class SimpleUpdater(AttrHandlerR):

0 commit comments

Comments
 (0)