diff --git a/src/ophyd_async/epics/pvi/__init__.py b/src/ophyd_async/epics/pvi/__init__.py index 522b96ae0a..ad638b740c 100644 --- a/src/ophyd_async/epics/pvi/__init__.py +++ b/src/ophyd_async/epics/pvi/__init__.py @@ -1,3 +1,3 @@ -from .pvi import PVIEntry, fill_pvi_entries, pre_initialize_blocks +from .pvi import PVIEntry, create_children_from_annotations, fill_pvi_entries -__all__ = ["PVIEntry", "fill_pvi_entries", "pre_initialize_blocks"] +__all__ = ["PVIEntry", "fill_pvi_entries", "create_children_from_annotations"] diff --git a/src/ophyd_async/epics/pvi/pvi.py b/src/ophyd_async/epics/pvi/pvi.py index 897b4b0389..960e63d25a 100644 --- a/src/ophyd_async/epics/pvi/pvi.py +++ b/src/ophyd_async/epics/pvi/pvi.py @@ -95,14 +95,10 @@ def _verify_common_blocks(entry: PVIEntry, common_device: Type[Device]): continue assert entry.sub_entries device_t, is_optional = _strip_union(sub_device) - if sub_name not in entry.sub_entries: - if is_optional: - continue - else: - raise RuntimeError( - f"sub device `{sub_name}:{type(sub_device)}` " - "was not provided by pvi" - ) + if sub_name not in entry.sub_entries and not is_optional: + raise RuntimeError( + f"sub device `{sub_name}:{type(sub_device)}` " "was not provided by pvi" + ) if isinstance(entry.sub_entries[sub_name], dict): for sub_sub_entry in entry.sub_entries[sub_name].values(): # type: ignore _verify_common_blocks(sub_sub_entry, sub_device) # type: ignore @@ -304,19 +300,15 @@ async def fill_pvi_entries( device.set_name(device.name) -def pre_initialize_blocks( - device: Device, included_optional_fields: Optional[Tuple[str, ...]] = None +def create_children_from_annotations( + device: Device, included_optional_fields: Tuple[str, ...] = () ): """For intializing blocks at __init__ of ``device``.""" for name, device_type in get_type_hints(type(device)).items(): if name in ("_name", "parent"): continue device_type, is_optional = _strip_union(device_type) - if ( - is_optional - and included_optional_fields - and name not in included_optional_fields - ): + if is_optional and name not in included_optional_fields: continue is_device_vector, device_type = _strip_device_vector(device_type) if ( @@ -328,4 +320,4 @@ def pre_initialize_blocks( sub_device = device_type() setattr(device, name, sub_device) - pre_initialize_blocks(sub_device) + create_children_from_annotations(sub_device) diff --git a/src/ophyd_async/panda/_hdf_panda.py b/src/ophyd_async/panda/_hdf_panda.py index 6a64924b75..cc59898a0e 100644 --- a/src/ophyd_async/panda/_hdf_panda.py +++ b/src/ophyd_async/panda/_hdf_panda.py @@ -8,8 +8,7 @@ SignalR, StandardDetector, ) -from ophyd_async.epics.pvi import fill_pvi_entries, pre_initialize_blocks - +from ophyd_async.epics.pvi import create_children_from_annotations, fill_pvi_entries from ._common_panda import CommonPandaBlocks from ._panda_controller import PandaPcapController @@ -26,7 +25,7 @@ def __init__( ): self._prefix = prefix - pre_initialize_blocks(self) + create_children_from_annotations(self) controller = PandaPcapController(pcap=self.pcap) writer = PandaHDFWriter( prefix=prefix, diff --git a/tests/epics/test_pvi.py b/tests/epics/test_pvi.py index 0993716f8d..65574f14dc 100644 --- a/tests/epics/test_pvi.py +++ b/tests/epics/test_pvi.py @@ -10,7 +10,7 @@ SignalRW, SignalX, ) -from ophyd_async.epics.pvi import fill_pvi_entries, pre_initialize_blocks +from ophyd_async.epics.pvi import create_children_from_annotations, fill_pvi_entries class Block1(Device): @@ -97,14 +97,14 @@ async def test_fill_pvi_entries_sim_mode(pvi_test_device_t): @pytest.fixture -def pvi_test_device_pre_initialize_blocks_t(): +def pvi_test_device_create_children_from_annotations_t(): """A fixture since pytest discourages init in test case classes""" class TestDevice(Block3, Device): def __init__(self, prefix: str, name: str = ""): self._prefix = prefix super().__init__(name) - pre_initialize_blocks(self) + create_children_from_annotations(self) async def connect( self, sim: bool = False, timeout: float = DEFAULT_TIMEOUT @@ -116,14 +116,16 @@ async def connect( yield TestDevice -async def test_device_pre_initialize_blocks(pvi_test_device_pre_initialize_blocks_t): - device = pvi_test_device_pre_initialize_blocks_t("PREFIX:") +async def test_device_create_children_from_annotations( + pvi_test_device_create_children_from_annotations_t, +): + device = pvi_test_device_create_children_from_annotations_t("PREFIX:") block_2_device = device.device block_1_device = device.device.device top_block_1_device = device.signal_device - # The pre_initialize_blocks has only made blocks, + # The create_children_from_annotations has only made blocks, # not signals or device vectors assert isinstance(block_2_device, Block2) assert isinstance(block_1_device, Block1) diff --git a/tests/panda/test_panda_connect.py b/tests/panda/test_panda_connect.py index 67706ec8cb..7dcd9c5b51 100644 --- a/tests/panda/test_panda_connect.py +++ b/tests/panda/test_panda_connect.py @@ -9,7 +9,7 @@ from ophyd_async.core import DEFAULT_TIMEOUT, Device, DeviceCollector, DeviceVector from ophyd_async.core.utils import NotConnected from ophyd_async.epics.pvi import PVIEntry, fill_pvi_entries -from ophyd_async.epics.pvi.pvi import pre_initialize_blocks +from ophyd_async.epics.pvi.pvi import create_children_from_annotations from ophyd_async.panda import PcapBlock, PulseBlock, SeqBlock, SeqTable, SeqTrigger @@ -47,7 +47,7 @@ class CommonPandaBlocksNoData(Device): class Panda(CommonPandaBlocksNoData): def __init__(self, prefix: str, name: str = ""): self._prefix = prefix - pre_initialize_blocks(self) + create_children_from_annotations(self) super().__init__(name) async def connect(self, sim: bool = False, timeout: float = DEFAULT_TIMEOUT): diff --git a/tests/panda/test_writer.py b/tests/panda/test_writer.py index 0fc0e9777f..0e56b87b68 100644 --- a/tests/panda/test_writer.py +++ b/tests/panda/test_writer.py @@ -12,12 +12,12 @@ StaticDirectoryProvider, set_sim_value, ) -from ophyd_async.epics.pvi import fill_pvi_entries, pre_initialize_blocks -from ophyd_async.panda._common_panda import CommonPandaBlocks -from ophyd_async.panda.writers import PandaHDFWriter +from ophyd_async.epics.pvi import create_children_from_annotations, fill_pvi_entries +from ophyd_async.panda import CommonPandaBlocks from ophyd_async.panda.writers._hdf_writer import ( Capture, CaptureSignalWrapper, + PandaHDFWriter, get_capture_signals, get_signals_marked_for_capture, ) @@ -35,7 +35,7 @@ class Panda(CommonPandaBlocks): def __init__(self, prefix: str, name: str = ""): self._prefix = prefix - pre_initialize_blocks(self) + create_children_from_annotations(self) super().__init__(name) async def connect(self, sim: bool = False, timeout: float = DEFAULT_TIMEOUT):