Skip to content

Commit

Permalink
fixed persistent tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
evalott100 committed Jan 13, 2025
1 parent 7683745 commit 492d13b
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 86 deletions.
6 changes: 3 additions & 3 deletions src/ophyd_async/core/_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ async def prepare(self, value: TriggerInfo) -> None:
value: TriggerInfo describing how to trigger the detector
"""
if value.trigger != DetectorTrigger.INTERNAL:
assert (
value.deadtime
), "Deadtime must be supplied when in externally triggered mode"
assert value.deadtime, (
"Deadtime must be supplied when in externally triggered mode"
)
if value.deadtime:
required = self._controller.get_deadtime(value.livetime)
assert required <= value.deadtime, (
Expand Down
7 changes: 4 additions & 3 deletions src/ophyd_async/core/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ def _caller_locals(self) -> dict[str, Any]:
caller_frame = tb.tb_frame
while caller_frame.f_locals.get("self", None) is self:
caller_frame = caller_frame.f_back
assert (
caller_frame
), "No previous frame to the one with self in it, this shouldn't happen"
assert caller_frame, (
"No previous frame to the one with self in it, "
"this shouldn't happen"
)
return caller_frame.f_locals.copy()

def __enter__(self) -> DeviceProcessor:
Expand Down
6 changes: 3 additions & 3 deletions src/ophyd_async/core/_device_filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ def fill_child_device(
# We need to add a new entry to a DeviceVector
vector = self._ensure_device_vector(name)
vector_device_type = self._vector_device_type[name] or device_type
assert issubclass(
vector_device_type, Device
), f"{vector_device_type} is not a Device"
assert issubclass(vector_device_type, Device), (
f"{vector_device_type} is not a Device"
)
connector = self._device_connector_factory()
vector[vector_index] = vector_device_type(connector=connector)
elif child := getattr(self._device, name, None):
Expand Down
2 changes: 1 addition & 1 deletion src/ophyd_async/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _format_sub_errors(self, name: str, error: Exception, indent="") -> str:
def format_error_string(self, indent="") -> str:
if not isinstance(self._errors, dict) and not isinstance(self._errors, str):
raise RuntimeError(
f"Unexpected type `{type(self._errors)}` " "expected `str` or `dict`"
f"Unexpected type `{type(self._errors)}` expected `str` or `dict`"
)

if isinstance(self._errors, str):
Expand Down
2 changes: 1 addition & 1 deletion src/ophyd_async/epics/adaravis/_aravis_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def prepare(self, trigger_info: TriggerInfo):
else:
image_mode = adcore.ImageMode.MULTIPLE
if (exposure := trigger_info.livetime) is not None:
asyncio.gather(self.driver.acquire_time.set(exposure))
await self.driver.acquire_time.set(exposure)

trigger_mode, trigger_source = self._get_trigger_info(trigger_info.trigger)
# trigger mode must be set first and on it's own!
Expand Down
6 changes: 3 additions & 3 deletions src/ophyd_async/epics/adcore/_core_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def __init__(
self._arm_status: AsyncStatus | None = None

async def prepare(self, trigger_info: TriggerInfo) -> None:
assert (
trigger_info.trigger == DetectorTrigger.INTERNAL
), "fly scanning (i.e. external triggering) is not supported for this device"
assert trigger_info.trigger == DetectorTrigger.INTERNAL, (
"fly scanning (i.e. external triggering) is not supported for this device"
)
self.frame_timeout = (
DEFAULT_TIMEOUT + await self.driver.acquire_time.get_value()
)
Expand Down
6 changes: 3 additions & 3 deletions src/ophyd_async/epics/adcore/_core_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ async def begin_capture(self) -> None:
self.fileio.file_number.set(0),
)

assert (
await self.fileio.file_path_exists.get_value()
), f"File path {info.directory_path} for file plugin does not exist!"
assert await self.fileio.file_path_exists.get_value(), (
f"File path {info.directory_path} for file plugin does not exist!"
)

# Overwrite num_capture to go forever
await self.fileio.num_capture.set(0)
Expand Down
6 changes: 3 additions & 3 deletions src/ophyd_async/epics/core/_aioca.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ async def get_setpoint(self) -> SignalDatatypeT:

def set_callback(self, callback: Callback[Reading[SignalDatatypeT]] | None) -> None:
if callback:
assert (
not self.subscription
), "Cannot set a callback when one is already set"
assert not self.subscription, (
"Cannot set a callback when one is already set"
)
self.subscription = camonitor(
self.read_pv,
lambda v: callback(self._make_reading(v)),
Expand Down
6 changes: 3 additions & 3 deletions src/ophyd_async/epics/core/_p4p.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,9 @@ async def get_setpoint(self) -> SignalDatatypeT:

def set_callback(self, callback: Callback[Reading[SignalDatatypeT]] | None) -> None:
if callback:
assert (
not self.subscription
), "Cannot set a callback when one is already set"
assert not self.subscription, (
"Cannot set a callback when one is already set"
)

async def async_callback(v):
callback(self._make_reading(v))
Expand Down
6 changes: 3 additions & 3 deletions src/ophyd_async/epics/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ async def prepare(self, value: FlyMotorInfo):
@AsyncStatus.wrap
async def kickoff(self):
"""Begin moving motor from prepared position to final position."""
assert (
self._fly_completed_position
), "Motor must be prepared before attempting to kickoff"
assert self._fly_completed_position, (
"Motor must be prepared before attempting to kickoff"
)

self._fly_status = self.set(
self._fly_completed_position, timeout=self._fly_timeout
Expand Down
6 changes: 3 additions & 3 deletions src/ophyd_async/sim/_pattern_detector/_pattern_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def write_data_to_dataset(self, path: str, data_shape: tuple[int, ...], data):
"""Write data to named dataset, resizing to fit and flushing after."""
assert self._handle_for_h5_file, "no file has been opened!"
dset = self._handle_for_h5_file[path]
assert isinstance(
dset, h5py.Dataset
), f"Expected {path} to be dataset, got {dset}"
assert isinstance(dset, h5py.Dataset), (
f"Expected {path} to be dataset, got {dset}"
)
dset.resize((self.image_counter + 1,) + data_shape)
dset[self.image_counter] = data
dset.flush()
Expand Down
13 changes: 6 additions & 7 deletions src/ophyd_async/tango/core/_tango_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ async def wait_for_reply(rd: int, to: float | None):
await asyncio.sleep(A_BIT)
if to and (time.time() - start_time > to):
raise TimeoutError(
f"{self._name} attr put failed:" f" Timeout"
f"{self._name} attr put failed: Timeout"
) from exc
else:
raise RuntimeError(
f"{self._name} device failure:" f" {exc.args[0].desc}"
f"{self._name} device failure: {exc.args[0].desc}"
) from exc

return AsyncStatus(wait_for_reply(rid, timeout))
Expand Down Expand Up @@ -422,7 +422,7 @@ async def _put():
raise TimeoutError(f"{self._name} command failed: Timeout") from te
except DevFailed as de:
raise RuntimeError(
f"{self._name} device" f" failure: {de.args[0].desc}"
f"{self._name} device failure: {de.args[0].desc}"
) from de

else:
Expand All @@ -446,8 +446,7 @@ async def wait_for_reply(rd: int, to: float | None):
) from de_exc
else:
raise RuntimeError(
f"{self._name} device failure:"
f" {de_exc.args[0].desc}"
f"{self._name} device failure: {de_exc.args[0].desc}"
) from de_exc

return AsyncStatus(wait_for_reply(rid, timeout))
Expand Down Expand Up @@ -739,11 +738,11 @@ def set_callback(self, callback: Callback | None) -> None:
self.proxies[self.read_trl].subscribe_callback(callback) # type: ignore
except AssertionError as ae:
raise RuntimeError(
"Cannot set a callback when one" " is already set"
"Cannot set a callback when one is already set"
) from ae
except RuntimeError as exc:
raise RuntimeError(
f"Cannot set callback" f" for {self.read_trl}. {exc}"
f"Cannot set callback for {self.read_trl}. {exc}"
) from exc

else:
Expand Down
24 changes: 12 additions & 12 deletions src/ophyd_async/testing/_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ async def assert_reading(
"""
actual_reading = await readable.read()
assert (
_approx_readable_value(expected_reading) == actual_reading
), _generate_assert_error_msg(
name=readable.name,
expected_result=expected_reading,
actual_result=actual_reading,
assert _approx_readable_value(expected_reading) == actual_reading, (
_generate_assert_error_msg(
name=readable.name,
expected_result=expected_reading,
actual_result=actual_reading,
)
)


Expand All @@ -109,12 +109,12 @@ async def assert_configuration(
"""
actual_configurable = await configurable.read_configuration()
assert (
_approx_readable_value(configuration) == actual_configurable
), _generate_assert_error_msg(
name=configurable.name,
expected_result=configuration,
actual_result=actual_configurable,
assert _approx_readable_value(configuration) == actual_configurable, (
_generate_assert_error_msg(
name=configurable.name,
expected_result=configuration,
actual_result=actual_configurable,
)
)


Expand Down
6 changes: 3 additions & 3 deletions src/ophyd_async/testing/_mock_signal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def get_mock(device: Device | Signal) -> Mock:
def _get_mock_signal_backend(signal: Signal) -> MockSignalBackend:
connector = signal._connector # noqa: SLF001
assert isinstance(connector, SignalConnector), f"Expected Signal, got {signal}"
assert isinstance(
connector.backend, MockSignalBackend
), f"Signal {signal} not connected in mock mode"
assert isinstance(connector.backend, MockSignalBackend), (
f"Signal {signal} not connected in mock mode"
)
return connector.backend


Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def _error_and_kill_pending_tasks(
task
for task in asyncio.all_tasks(loop)
if (coro := task.get_coro()) is not None
and hasattr(coro, "__name__")
and coro.__name__ not in _ALLOWED_PYTEST_TASKS
and not task.done()
}
Expand All @@ -112,7 +113,7 @@ def _error_and_kill_pending_tasks(


@pytest.fixture(autouse=True, scope="function")
def fail_test_on_unclosed_tasks(request: FixtureRequest):
async def fail_test_on_unclosed_tasks(request: FixtureRequest):
"""
Used on every test to ensure failure if there are pending tasks
by the end of the test.
Expand Down
4 changes: 1 addition & 3 deletions tests/core/test_mock_signal_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,7 @@ async def test_reset_mock_put_calls(mock_signals):
# Replacing spaces because they change between runners
# (e.g the github actions runner has more)
assert str(exc.value).replace(" ", "").replace("\n", "") == (
"expectedcallnotfound."
"Expected:put('test_value',wait=<ANY>)"
"Actual:notcalled."
"expectedcallnotfound.Expected:put('test_value',wait=<ANY>)Actual:notcalled."
)


Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ async def test_combining_top_level_signal_and_child_device():
async def test_format_error_string_input():
with pytest.raises(
RuntimeError,
match=("Unexpected type `<class 'int'>` " "expected `str` or `dict`"),
match=("Unexpected type `<class 'int'>` expected `str` or `dict`"),
):
not_connected = NotConnected(123)
str(not_connected)
Expand Down
34 changes: 21 additions & 13 deletions tests/epics/adaravis/test_aravis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import cast
from unittest.mock import AsyncMock, patch

import pytest

Expand Down Expand Up @@ -133,17 +134,24 @@ async def test_can_decribe_collect(


async def test_unsupported_trigger_excepts(test_adaravis: adaravis.AravisDetector):
with pytest.raises(
ValueError,
# str(EnumClass.value) handling changed in Python 3.11
match=r"AravisController only supports the following trigger types: .* but",
):
await test_adaravis.prepare(
TriggerInfo(
number_of_triggers=0,
trigger=DetectorTrigger.VARIABLE_GATE,
deadtime=1,
livetime=1,
frame_timeout=3,
with patch(
"ophyd_async.epics.adcore._hdf_writer.ADHDFWriter.open", new_callable=AsyncMock
) as mock_open:
with pytest.raises(
ValueError,
# str(EnumClass.value) handling changed in Python 3.11
match=(
"AravisController only supports the following trigger types: .* but"
),
):
await test_adaravis.prepare(
TriggerInfo(
number_of_triggers=0,
trigger=DetectorTrigger.VARIABLE_GATE,
deadtime=1,
livetime=1,
frame_timeout=3,
)
)
)

mock_open.assert_called_once()
31 changes: 18 additions & 13 deletions tests/epics/adpilatus/test_pilatus.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from collections.abc import Awaitable, Callable
from typing import cast
from unittest.mock import patch
from unittest.mock import AsyncMock, patch

import pytest

Expand Down Expand Up @@ -103,19 +103,24 @@ async def test_hints_from_hdf_writer(test_adpilatus: adpilatus.PilatusDetector):


async def test_unsupported_trigger_excepts(test_adpilatus: adpilatus.PilatusDetector):
with pytest.raises(
ValueError,
# str(EnumClass.value) handling changed in Python 3.11
match=r"PilatusController only supports the following trigger types: .* but",
):
await test_adpilatus.prepare(
TriggerInfo(
number_of_triggers=1,
trigger=DetectorTrigger.EDGE_TRIGGER,
deadtime=1.0,
livetime=1.0,
open = "ophyd_async.epics.adcore._hdf_writer.ADHDFWriter.open"
with patch(open, new_callable=AsyncMock) as mock_open:
with pytest.raises(
ValueError,
# str(EnumClass.value) handling changed in Python 3.11
match=(
"PilatusController only supports the following trigger types: .* but"
),
):
await test_adpilatus.prepare(
TriggerInfo(
number_of_triggers=1,
trigger=DetectorTrigger.EDGE_TRIGGER,
deadtime=1.0,
livetime=1.0,
)
)
)
mock_open.assert_called_once()


async def test_exposure_time_and_acquire_period_set(
Expand Down
2 changes: 1 addition & 1 deletion tests/epics/signal/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ async def test_signals_created_for_not_prec_0_float_cannot_use_int(
sig = epics_signal_rw(int, ioc_devices.get_pv(protocol, "float_prec_1"))
with pytest.raises(
TypeError,
match="float_prec_1 with inferred datatype float" ".* cannot be coerced to int",
match="float_prec_1 with inferred datatype float.* cannot be coerced to int",
):
await sig.connect()

Expand Down
6 changes: 3 additions & 3 deletions tests/sim/test_sim_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
async def test_sim_pattern_detector_initialization(
sim_pattern_detector: PatternDetector,
):
assert (
sim_pattern_detector.pattern_generator
), "PatternGenerator was not initialized correctly."
assert sim_pattern_detector.pattern_generator, (
"PatternGenerator was not initialized correctly."
)


async def test_detector_creates_controller_and_writer(
Expand Down

0 comments on commit 492d13b

Please sign in to comment.