Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
(#1217) Make pyright happy
Browse files Browse the repository at this point in the history
  • Loading branch information
rtuck99 committed Apr 2, 2024
1 parent 4fb13cf commit 831d8e8
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
for self.ispyb_ids."""
ISPYB_LOGGER.debug("Initialising ISPyB callback")
super().__init__(log=ISPYB_LOGGER, emit=emit)
self._oav_snapshot_event_idx: Optional[int] = None
self._oav_snapshot_event_idx: int = 0
self._hwscan_data_collection_info: Optional[DataCollectionInfo] = None
self._sample_barcode: Optional[str] = None
self.params: GridscanInternalParameters | RotationInternalParameters | None = (
Expand Down Expand Up @@ -92,6 +92,9 @@ def activity_gated_event(self, doc: Event) -> Event:
ISPYB_LOGGER.debug("ISPyB handler received event document.")
assert self.ispyb is not None, "ISPyB deposition wasn't initialised!"
assert self.params is not None, "ISPyB handler didn't recieve parameters!"
assert (
self._hwscan_data_collection_info
), "Processed unexpected event prior to callback activation"

event_descriptor = self.descriptors.get(doc["descriptor"])
if event_descriptor is None:
Expand Down Expand Up @@ -139,6 +142,7 @@ def _handle_ispyb_hardware_read(self, doc):

def _handle_oav_snapshot_triggered(self, doc):
assert self.ispyb_ids.data_collection_ids, "No current data collection"
assert self.params, "ISPyB handler didn't recieve parameters!"
data = doc["data"]
data_collection_id = None
data_collection_info = DataCollectionInfo()
Expand Down Expand Up @@ -201,6 +205,7 @@ def _handle_oav_snapshot_triggered(self, doc):

def _handle_ispyb_transmission_flux_read(self, doc):
assert self._hwscan_data_collection_info
assert self.params
if doc["data"]["attenuator_actual_transmission"]:
# Ispyb wants the transmission in a percentage, we use fractions
self._hwscan_data_collection_info.transmission = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def test_ispyb_deposition_in_rotation_plan(
fake_create_rotation_devices.dcm.energy_in_kev.user_readback.sim_put( # pyright: ignore
energy_ev / 1000
)
fake_create_rotation_devices.undulator.current_gap.sim_put(1.12)
fake_create_rotation_devices.undulator.current_gap.sim_put(1.12) # pyright: ignore
fake_create_rotation_devices.synchrotron.machine_status.synchrotron_mode.sim_put( # pyright: ignore
test_synchrotron_mode.value
)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/experiment_plans/test_grid_detection_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ def decorated():
RE(ispyb_activation_wrapper(test_fgs_params, decorated()))

assert_event(
cb.activity_gated_start.mock_calls[0],
cb.activity_gated_start.mock_calls[0], # pyright:ignore
{"activate_callbacks": ["GridscanISPyBCallback"]},
)
assert_event(
cb.activity_gated_event.mock_calls[0],
cb.activity_gated_event.mock_calls[0], # pyright: ignore
{
"oav_snapshot_top_left_x": 8,
"oav_snapshot_top_left_y": -6,
Expand All @@ -255,7 +255,7 @@ def decorated():
},
)
assert_event(
cb.activity_gated_event.mock_calls[1],
cb.activity_gated_event.mock_calls[1], # pyright:ignore
{
"oav_snapshot_top_left_x": 8,
"oav_snapshot_top_left_y": 2,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random
import types
from typing import Tuple
from typing import Any, Tuple
from unittest.mock import DEFAULT, MagicMock, call, patch

import bluesky.preprocessors as bpp
Expand Down Expand Up @@ -351,7 +351,7 @@ def test_individual_plans_triggered_once_and_only_once_in_composite_run(
move_xyz: MagicMock,
run_gridscan: MagicMock,
move_aperture: MagicMock,
RE_with_subs: RunEngine,
RE_with_subs,
mock_subscriptions: Tuple[GridscanNexusFileCallback, GridscanISPyBCallback],
fake_fgs_composite: FlyScanXRayCentreComposite,
test_panda_fgs_params: PandAGridscanInternalParameters,
Expand Down Expand Up @@ -395,7 +395,7 @@ def test_when_gridscan_finished_then_smargon_stub_offsets_are_set(
move_xyz: MagicMock,
run_gridscan: MagicMock,
aperture_set: MagicMock,
RE_with_subs: RunEngine,
RE_with_subs: tuple[RunEngine, Any],
mock_subscriptions: Tuple[GridscanNexusFileCallback, GridscanISPyBCallback],
test_panda_fgs_params: PandAGridscanInternalParameters,
fake_fgs_composite: FlyScanXRayCentreComposite,
Expand Down Expand Up @@ -441,7 +441,7 @@ def test_when_gridscan_succeeds_ispyb_comment_appended_to(
move_xyz: MagicMock,
run_gridscan: MagicMock,
aperture_set: MagicMock,
RE_with_subs: RunEngine,
RE_with_subs: tuple[RunEngine, Any],
mock_subscriptions: Tuple[GridscanNexusFileCallback, GridscanISPyBCallback],
test_panda_fgs_params: PandAGridscanInternalParameters,
fake_fgs_composite: FlyScanXRayCentreComposite,
Expand Down Expand Up @@ -486,7 +486,7 @@ def test_when_gridscan_fails_ispyb_comment_appended_to(
setup_panda_for_flyscan: MagicMock,
move_xyz: MagicMock,
run_gridscan: MagicMock,
RE_with_subs: RunEngine,
RE_with_subs: tuple[RunEngine, Any],
mock_subscriptions: Tuple[GridscanNexusFileCallback, GridscanISPyBCallback],
test_panda_fgs_params: PandAGridscanInternalParameters,
fake_fgs_composite: FlyScanXRayCentreComposite,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ def test_when_pin_centre_xray_centre_called_then_plan_runs_correctly(
@patch(
"hyperion.experiment_plans.grid_detect_then_xray_centre_plan.GridDetectionCallback",
)
@patch(
"hyperion.experiment_plans.grid_detect_then_xray_centre_plan.OavSnapshotCallback",
)
@patch(
"hyperion.experiment_plans.pin_centre_then_xray_centre_plan.pin_tip_centre_plan",
autospec=True,
Expand All @@ -89,20 +86,13 @@ def test_when_pin_centre_xray_centre_called_then_plan_runs_correctly(
def test_when_pin_centre_xray_centre_called_then_detector_positioned(
mock_grid_detect: MagicMock,
mock_pin_tip_centre: MagicMock,
mock_oav_callback: MagicMock,
mock_grid_callback: MagicMock,
test_pin_centre_then_xray_centre_params: PinCentreThenXrayCentreInternalParameters,
simple_beamline,
test_config_files,
sim_run_engine,
):

mock_oav_callback.return_value.out_upper_left = [[1, 3], [3, 4]]
mock_oav_callback.return_value.snapshot_filenames = [
["1.png", "2.png", "3.png"],
["1.png", "2.png", "3.png"],
["1.png", "2.png", "3.png"],
]
mock_grid_callback.return_value.get_grid_parameters.return_value = GridScanParams(
dwell_time_ms=0,
x_start=0,
Expand Down
8 changes: 8 additions & 0 deletions tests/unit_tests/external_interaction/callbacks/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ class TestData:
} # type: ignore
test_event_document_oav_snapshot_xy: Event = {
"descriptor": "b5ba4aec-de49-4970-81a4-b4a847391d34",
"time": 1666604299.828203,
"timestamps": {},
"seq_num": 1,
"uid": "29033ecf-e052-43dd-98af-c7cdd62e8174",
"data": {
"oav_snapshot_top_left_x": 50,
"oav_snapshot_top_left_y": 100,
Expand All @@ -162,6 +166,10 @@ class TestData:
}
test_event_document_oav_snapshot_xz: Event = {
"descriptor": "b5ba4aec-de49-4970-81a4-b4a847391d34",
"time": 1666604299.828203,
"timestamps": {},
"seq_num": 1,
"uid": "29033ecf-e052-43dd-98af-c7cdd62e8174",
"data": {
"oav_snapshot_top_left_x": 50,
"oav_snapshot_top_left_y": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ def outer_plan():
inner_callback, activity_gated_start=DEFAULT, activity_gated_stop=DEFAULT
):
root_mock = MagicMock()
root_mock.attach_mock(outer_callback.activity_gated_start, "outer_start")
root_mock.attach_mock(outer_callback.activity_gated_stop, "outer_stop")
root_mock.attach_mock(inner_callback.activity_gated_start, "inner_start")
root_mock.attach_mock(inner_callback.activity_gated_stop, "inner_stop")
# fmt: off
root_mock.attach_mock(outer_callback.activity_gated_start, "outer_start") # pyright: ignore
root_mock.attach_mock(outer_callback.activity_gated_stop, "outer_stop") # pyright: ignore
root_mock.attach_mock(inner_callback.activity_gated_start, "inner_start") # pyright: ignore
root_mock.attach_mock(inner_callback.activity_gated_stop, "inner_stop") # pyright: ignore
# fmt: on
RE(outer_plan())

assert [call[0] for call in root_mock.mock_calls] == [
Expand Down

0 comments on commit 831d8e8

Please sign in to comment.