From e2f8317e7584e4de788c2b39e5b5edaa98c1bc9e Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Wed, 17 Apr 2024 12:20:06 +0100 Subject: [PATCH] Ruff and Pyrite linting changes --- src/ophyd_async/core/sim_signal_backend.py | 8 +++--- src/ophyd_async/core/utils.py | 2 +- src/ophyd_async/epics/_backend/_aioca.py | 16 ++++++------ src/ophyd_async/epics/_backend/_p4p.py | 26 +++++++++---------- src/ophyd_async/epics/_backend/common.py | 4 +-- .../epics/areadetector/writers/_hdffile.py | 8 +++--- src/ophyd_async/epics/pvi/pvi.py | 7 +++-- src/ophyd_async/panda/trigger.py | 1 - src/ophyd_async/panda/writers/hdf_writer.py | 4 +-- .../panda/writers/panda_hdf_file.py | 8 +++--- .../planstubs/prepare_trigger_and_dets.py | 1 - tests/conftest.py | 6 ++--- tests/core/test_device.py | 4 +-- tests/core/test_flyer.py | 8 +++--- tests/core/test_sim.py | 10 +++---- tests/core/test_utils.py | 9 ++++--- tests/epics/test_signals.py | 12 ++++----- tests/panda/test_panda.py | 2 -- tests/panda/test_panda_utils.py | 1 - tests/panda/test_writer.py | 8 +++--- tests/test_flyer_with_panda.py | 8 +++--- 21 files changed, 70 insertions(+), 83 deletions(-) diff --git a/src/ophyd_async/core/sim_signal_backend.py b/src/ophyd_async/core/sim_signal_backend.py index a2575dc9b8..b0190344bb 100644 --- a/src/ophyd_async/core/sim_signal_backend.py +++ b/src/ophyd_async/core/sim_signal_backend.py @@ -41,7 +41,7 @@ def descriptor(self, source: str, value) -> Descriptor: type(value) in primitive_dtypes ), f"invalid converter for value of type {type(value)}" dtype = primitive_dtypes[type(value)] - return dict(source=source, dtype=dtype, shape=[]) + return {"source": source, "dtype": dtype, "shape": []} def make_initial_value(self, datatype: Optional[Type[T]]) -> T: if datatype is None: @@ -52,7 +52,7 @@ def make_initial_value(self, datatype: Optional[Type[T]]) -> T: class SimArrayConverter(SimConverter): def descriptor(self, source: str, value) -> Descriptor: - return dict(source=source, dtype="array", shape=[len(value)]) + return {"source": source, "dtype": "array", "shape": [len(value)]} def make_initial_value(self, datatype: Optional[Type[T]]) -> T: if datatype is None: @@ -76,9 +76,7 @@ def write_value(self, value: Union[Enum, str]) -> Enum: def descriptor(self, source: str, value) -> Descriptor: choices = [e.value for e in self.enum_class] - return dict( - source=source, dtype="string", shape=[], choices=choices - ) # type: ignore + return {"source": source, "dtype": "string", "shape": [], "choices": choices} # type: ignore def make_initial_value(self, datatype: Optional[Type[T]]) -> T: if datatype is None: diff --git a/src/ophyd_async/core/utils.py b/src/ophyd_async/core/utils.py index 6863c1d1f2..ad70bcb62e 100644 --- a/src/ophyd_async/core/utils.py +++ b/src/ophyd_async/core/utils.py @@ -132,7 +132,7 @@ def get_unique(values: Dict[str, T], types: str) -> T: async def merge_gathered_dicts( - coros: Iterable[Awaitable[Dict[str, T]]] + coros: Iterable[Awaitable[Dict[str, T]]], ) -> Dict[str, T]: """Merge dictionaries produced by a sequence of coroutines. diff --git a/src/ophyd_async/epics/_backend/_aioca.py b/src/ophyd_async/epics/_backend/_aioca.py index 00cb0cbfc3..db8180641b 100644 --- a/src/ophyd_async/epics/_backend/_aioca.py +++ b/src/ophyd_async/epics/_backend/_aioca.py @@ -52,14 +52,14 @@ def value(self, value: AugmentedValue): return value def reading(self, value: AugmentedValue): - return dict( - value=self.value(value), - timestamp=value.timestamp, - alarm_severity=-1 if value.severity > 2 else value.severity, - ) + return { + "value": self.value(value), + "timestamp": value.timestamp, + "alarm_severity": -1 if value.severity > 2 else value.severity, + } def descriptor(self, source: str, value: AugmentedValue) -> Descriptor: - return dict(source=source, dtype=dbr_to_dtype[value.datatype], shape=[]) + return {"source": source, "dtype": dbr_to_dtype[value.datatype], "shape": []} class CaLongStrConverter(CaConverter): @@ -74,7 +74,7 @@ def write_value(self, value: str): class CaArrayConverter(CaConverter): def descriptor(self, source: str, value: AugmentedValue) -> Descriptor: - return dict(source=source, dtype="array", shape=[len(value)]) + return {"source": source, "dtype": "array", "shape": [len(value)]} @dataclass @@ -92,7 +92,7 @@ def value(self, value: AugmentedValue): def descriptor(self, source: str, value: AugmentedValue) -> Descriptor: choices = [e.value for e in self.enum_class] - return dict(source=source, dtype="string", shape=[], choices=choices) + return {"source": source, "dtype": "string", "shape": [], "choices": choices} class DisconnectedCaConverter(CaConverter): diff --git a/src/ophyd_async/epics/_backend/_p4p.py b/src/ophyd_async/epics/_backend/_p4p.py index 0507ff4d32..759d86b7bb 100644 --- a/src/ophyd_async/epics/_backend/_p4p.py +++ b/src/ophyd_async/epics/_backend/_p4p.py @@ -49,15 +49,15 @@ def value(self, value): def reading(self, value): ts = value["timeStamp"] sv = value["alarm"]["severity"] - return dict( - value=self.value(value), - timestamp=ts["secondsPastEpoch"] + ts["nanoseconds"] * 1e-9, - alarm_severity=-1 if sv > 2 else sv, - ) + return { + "value": self.value(value), + "timestamp": ts["secondsPastEpoch"] + ts["nanoseconds"] * 1e-9, + "alarm_severity": -1 if sv > 2 else sv, + } def descriptor(self, source: str, value) -> Descriptor: dtype = specifier_to_dtype[value.type().aspy("value")] - return dict(source=source, dtype=dtype, shape=[]) + return {"source": source, "dtype": dtype, "shape": []} def metadata_fields(self) -> List[str]: """ @@ -74,7 +74,7 @@ def value_fields(self) -> List[str]: class PvaArrayConverter(PvaConverter): def descriptor(self, source: str, value) -> Descriptor: - return dict(source=source, dtype="array", shape=[len(value["value"])]) + return {"source": source, "dtype": "array", "shape": [len(value["value"])]} class PvaNDArrayConverter(PvaConverter): @@ -98,7 +98,7 @@ def value(self, value): def descriptor(self, source: str, value) -> Descriptor: dims = self._get_dimensions(value) - return dict(source=source, dtype="array", shape=dims) + return {"source": source, "dtype": "array", "shape": dims} def write_value(self, value): # No clear use-case for writing directly to an NDArray, and some @@ -122,7 +122,7 @@ def value(self, value): def descriptor(self, source: str, value) -> Descriptor: choices = [e.value for e in self.enum_class] - return dict(source=source, dtype="string", shape=[], choices=choices) + return {"source": source, "dtype": "string", "shape": [], "choices": choices} class PvaEnumBoolConverter(PvaConverter): @@ -130,7 +130,7 @@ def value(self, value): return value["value"]["index"] def descriptor(self, source: str, value) -> Descriptor: - return dict(source=source, dtype="integer", shape=[]) + return {"source": source, "dtype": "integer", "shape": []} class PvaTableConverter(PvaConverter): @@ -139,7 +139,7 @@ def value(self, value): def descriptor(self, source: str, value) -> Descriptor: # This is wrong, but defer until we know how to actually describe a table - return dict(source=source, dtype="object", shape=[]) # type: ignore + return {"source": source, "dtype": "object", "shape": []} # type: ignore class PvaDictConverter(PvaConverter): @@ -147,7 +147,7 @@ def reading(self, value): ts = time.time() value = value.todict() # Alarm severity is vacuously 0 for a table - return dict(value=value, timestamp=ts, alarm_severity=0) + return {"value": value, "timestamp": ts, "alarm_severity": 0} def value(self, value: Value): return value.todict() @@ -279,7 +279,7 @@ async def put(self, value: Optional[T], wait=True, timeout=None): write_value = self.initial_values[self.write_pv] else: write_value = self.converter.write_value(value) - coro = self.ctxt.put(self.write_pv, dict(value=write_value), wait=wait) + coro = self.ctxt.put(self.write_pv, {"value": write_value}, wait=wait) try: await asyncio.wait_for(coro, timeout) except asyncio.TimeoutError as exc: diff --git a/src/ophyd_async/epics/_backend/common.py b/src/ophyd_async/epics/_backend/common.py index 964d385ca7..eaa6691926 100644 --- a/src/ophyd_async/epics/_backend/common.py +++ b/src/ophyd_async/epics/_backend/common.py @@ -15,6 +15,4 @@ def get_supported_enum_class( choices = tuple(v.value for v in datatype) if set(choices).difference(pv_choices): raise TypeError(f"{pv} has choices {pv_choices}: not all in {choices}") - return Enum( - "GeneratedChoices", {x or "_": x for x in pv_choices}, type=str - ) # type: ignore + return Enum("GeneratedChoices", {x or "_": x for x in pv_choices}, type=str) # type: ignore diff --git a/src/ophyd_async/epics/areadetector/writers/_hdffile.py b/src/ophyd_async/epics/areadetector/writers/_hdffile.py index 474d15e097..19f5a0c4ad 100644 --- a/src/ophyd_async/epics/areadetector/writers/_hdffile.py +++ b/src/ophyd_async/epics/areadetector/writers/_hdffile.py @@ -44,10 +44,10 @@ def stream_resources(self) -> Iterator[StreamResource]: def stream_data(self, indices_written: int) -> Iterator[StreamDatum]: # Indices are relative to resource if indices_written > self._last_emitted: - indices = dict( - start=self._last_emitted, - stop=indices_written, - ) + indices = { + "start": self._last_emitted, + "stop": indices_written, + } self._last_emitted = indices_written for bundle in self._bundles: yield bundle.compose_stream_datum(indices) diff --git a/src/ophyd_async/epics/pvi/pvi.py b/src/ophyd_async/epics/pvi/pvi.py index dc211250db..d174c0a27c 100644 --- a/src/ophyd_async/epics/pvi/pvi.py +++ b/src/ophyd_async/epics/pvi/pvi.py @@ -89,7 +89,8 @@ def _verify_common_blocks(entry: PVIEntry, common_device: Type[Device]): _verify_common_blocks(sub_sub_entry, sub_device) # type: ignore else: _verify_common_blocks( - entry.sub_entries[sub_name], sub_device # type: ignore + entry.sub_entries[sub_name], + sub_device, # type: ignore ) @@ -238,9 +239,7 @@ async def _get_pvi_entries(entry: PVIEntry, timeout=DEFAULT_TIMEOUT): sub_number_split = 1 if sub_number_split is None else sub_number_split if sub_name_split not in entry.sub_entries: entry.sub_entries[sub_name_split] = {} - entry.sub_entries[sub_name_split][ - sub_number_split - ] = sub_entry # type: ignore + entry.sub_entries[sub_name_split][sub_number_split] = sub_entry # type: ignore else: entry.sub_entries[sub_name] = sub_entry diff --git a/src/ophyd_async/panda/trigger.py b/src/ophyd_async/panda/trigger.py index ef9251b7f5..6e4d056f61 100644 --- a/src/ophyd_async/panda/trigger.py +++ b/src/ophyd_async/panda/trigger.py @@ -13,7 +13,6 @@ class SeqTableInfo: class StaticSeqTableTriggerLogic(TriggerLogic[SeqTableInfo]): - def __init__(self, seq: SeqBlock) -> None: self.seq = seq diff --git a/src/ophyd_async/panda/writers/hdf_writer.py b/src/ophyd_async/panda/writers/hdf_writer.py index 513d9c21e0..dc58fce101 100644 --- a/src/ophyd_async/panda/writers/hdf_writer.py +++ b/src/ophyd_async/panda/writers/hdf_writer.py @@ -63,9 +63,8 @@ class CaptureSignalWrapper: # This should return a dictionary which contains a dict, containing the Capture # signal object, and the value of that signal async def get_signals_marked_for_capture( - capture_signals: Dict[str, SignalR] + capture_signals: Dict[str, SignalR], ) -> Dict[str, CaptureSignalWrapper]: - # Read signals to see if they should be captured do_read = [signal.get_value() for signal in capture_signals.values()] @@ -79,7 +78,6 @@ async def get_signals_marked_for_capture( for signal_path, signal_object, signal_value in zip( capture_signals.keys(), capture_signals.values(), signal_values ): - signal_path = signal_path.replace("_capture", "") if (signal_value.value in iter(Capture)) and (signal_value.value != Capture.No): signals_to_capture[signal_path] = CaptureSignalWrapper( diff --git a/src/ophyd_async/panda/writers/panda_hdf_file.py b/src/ophyd_async/panda/writers/panda_hdf_file.py index 615ae85a94..3b5b77449d 100644 --- a/src/ophyd_async/panda/writers/panda_hdf_file.py +++ b/src/ophyd_async/panda/writers/panda_hdf_file.py @@ -49,10 +49,10 @@ def stream_resources(self) -> Iterator[StreamResource]: def stream_data(self, indices_written: int) -> Iterator[StreamDatum]: # Indices are relative to resource if indices_written > self._last_emitted: - indices = dict( - start=self._last_emitted, - stop=indices_written, - ) + indices = { + "start": self._last_emitted, + "stop": indices_written, + } self._last_emitted = indices_written for bundle in self._bundles: yield bundle.compose_stream_datum(indices) diff --git a/src/ophyd_async/planstubs/prepare_trigger_and_dets.py b/src/ophyd_async/planstubs/prepare_trigger_and_dets.py index ad86ef0a92..08e481b74e 100644 --- a/src/ophyd_async/planstubs/prepare_trigger_and_dets.py +++ b/src/ophyd_async/planstubs/prepare_trigger_and_dets.py @@ -19,7 +19,6 @@ def prepare_static_seq_table_flyer_and_detectors_with_same_trigger( repeats: int = 1, period: float = 0.0, ): - trigger_info = TriggerInfo( num=num * repeats, trigger=DetectorTrigger.constant_gate, diff --git a/tests/conftest.py b/tests/conftest.py index 186801ccba..9a82fe2fd1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ -import os import asyncio +import os import subprocess import sys import time @@ -29,8 +29,8 @@ def pytest_exception_interact(call): @pytest.hookimpl(tryfirst=True) def pytest_internalerror(excinfo): raise excinfo.value - - + + @pytest.fixture(scope="function") def RE(request): loop = asyncio.new_event_loop() diff --git a/tests/core/test_device.py b/tests/core/test_device.py index a2d29dd5ad..482666c18c 100644 --- a/tests/core/test_device.py +++ b/tests/core/test_device.py @@ -51,9 +51,7 @@ def test_device_children(parent: DummyDeviceGroup): def test_device_vector_children(): parent = DummyDeviceGroup("root") - device_vector_children = [ - (name, child) for name, child in parent.dict_with_children.children() - ] + device_vector_children = list(parent.dict_with_children.children()) assert device_vector_children == [("123", parent.dict_with_children[123])] diff --git a/tests/core/test_flyer.py b/tests/core/test_flyer.py index 747850f846..53ff7313a7 100644 --- a/tests/core/test_flyer.py +++ b/tests/core/test_flyer.py @@ -97,10 +97,10 @@ async def collect_stream_docs( yield "stream_resource", self._file.stream_resource_doc if indices_written >= self._last_emitted: - indices = dict( - start=self._last_emitted, - stop=indices_written, - ) + indices = { + "start": self._last_emitted, + "stop": indices_written, + } self._last_emitted = indices_written self._last_flush = time.monotonic() yield "stream_datum", self._file.compose_stream_datum(indices) diff --git a/tests/core/test_sim.py b/tests/core/test_sim.py index a7459b9429..baefc850ee 100644 --- a/tests/core/test_sim.py +++ b/tests/core/test_sim.py @@ -18,23 +18,23 @@ class MyEnum(str, Enum): def integer_d(value): - return dict(dtype="integer", shape=[]) + return {"dtype": "integer", "shape": []} def number_d(value): - return dict(dtype="number", shape=[]) + return {"dtype": "number", "shape": []} def string_d(value): - return dict(dtype="string", shape=[]) + return {"dtype": "string", "shape": []} def enum_d(value): - return dict(dtype="string", shape=[], choices=["Aaa", "Bbb", "Ccc"]) + return {"dtype": "string", "shape": [], "choices": ["Aaa", "Bbb", "Ccc"]} def waveform_d(value): - return dict(dtype="array", shape=[len(value)]) + return {"dtype": "array", "shape": [len(value)]} class MonitorQueue: diff --git a/tests/core/test_utils.py b/tests/core/test_utils.py index 59dd8cd3f0..6a02bff77f 100644 --- a/tests/core/test_utils.py +++ b/tests/core/test_utils.py @@ -139,9 +139,11 @@ async def test_error_handling_value_errors(caplog): # This should fail since the error is a ValueError with pytest.raises(NotConnected) as e: - await dummy_device_two_working_one_timeout_two_value_error.connect( - timeout=0.01 - ), + ( + await dummy_device_two_working_one_timeout_two_value_error.connect( + timeout=0.01 + ), + ) assert str(e.value) == str(TWO_WORKING_TWO_TIMEOUT_TWO_VALUE_ERROR_OUTPUT) logs = caplog.get_records("call") @@ -213,7 +215,6 @@ def test_not_connected_error_output(): async def test_combining_top_level_signal_and_child_device(): - dummy_device1 = DummyDeviceCombiningTopLevelSignalAndSubDevice() with pytest.raises(NotConnected) as e: await dummy_device1.connect(timeout=0.01) diff --git a/tests/epics/test_signals.py b/tests/epics/test_signals.py index 2c00bde508..1ab05142b5 100644 --- a/tests/epics/test_signals.py +++ b/tests/epics/test_signals.py @@ -157,23 +157,23 @@ class MyEnum(str, Enum): def integer_d(value): - return dict(dtype="integer", shape=[]) + return {"dtype": "integer", "shape": []} def number_d(value): - return dict(dtype="number", shape=[]) + return {"dtype": "number", "shape": []} def string_d(value): - return dict(dtype="string", shape=[]) + return {"dtype": "string", "shape": []} def enum_d(value): - return dict(dtype="string", shape=[], choices=["Aaa", "Bbb", "Ccc"]) + return {"dtype": "string", "shape": [], "choices": ["Aaa", "Bbb", "Ccc"]} def waveform_d(value): - return dict(dtype="array", shape=[len(value)]) + return {"dtype": "array", "shape": [len(value)]} ls1 = "a string that is just longer than forty characters" @@ -389,7 +389,7 @@ async def test_pva_table(ioc: IOC) -> None: enum=[MyEnum.c, MyEnum.b], ) # TODO: what should this be for a variable length table? - descriptor = dict(dtype="object", shape=[]) + descriptor = {"dtype": "object", "shape": []} # Make and connect the backend for t, i, p in [(MyTable, initial, put), (None, put, initial)]: backend = await ioc.make_backend(t, "table") diff --git a/tests/panda/test_panda.py b/tests/panda/test_panda.py index ba9e753e63..1259f199ab 100644 --- a/tests/panda/test_panda.py +++ b/tests/panda/test_panda.py @@ -62,7 +62,6 @@ def __init__(self, prefix: str, name: str = "") -> None: async def connect( self, sim: bool = False, timeout: float = DEFAULT_TIMEOUT ) -> None: - await fill_pvi_entries(self, self._prefix + "PVI", timeout=timeout, sim=sim) await super().connect(sim) @@ -125,7 +124,6 @@ async def test_panda_with_missing_blocks(panda_pva): async def test_panda_with_extra_blocks_and_signals(panda_pva): - panda = PandANoDataBlock("PANDAQSRV:") await panda.connect() assert panda.extra # type: ignore diff --git a/tests/panda/test_panda_utils.py b/tests/panda/test_panda_utils.py index c636b62e01..c0b67a40f7 100644 --- a/tests/panda/test_panda_utils.py +++ b/tests/panda/test_panda_utils.py @@ -37,7 +37,6 @@ async def test_save_panda(mock_save_to_yaml, sim_panda, RE: RunEngine): "data.hdf_file_name": "", "data.num_capture": 0, "pcap.arm": False, - "pcap.arm": False, "pulse.1.delay": 0.0, "pulse.1.width": 0.0, "pulse.2.delay": 0.0, diff --git a/tests/panda/test_writer.py b/tests/panda/test_writer.py index 87238738da..c80893823c 100644 --- a/tests/panda/test_writer.py +++ b/tests/panda/test_writer.py @@ -43,11 +43,13 @@ async def sim_panda() -> PandA: ) set_sim_value( - sim_panda.block1.test_capture, Capture.MinMaxMean # type: ignore[attr-defined] + sim_panda.block1.test_capture, + Capture.MinMaxMean, # type: ignore[attr-defined] ) set_sim_value( - sim_panda.block2.test_capture, Capture.No # type: ignore[attr-defined] + sim_panda.block2.test_capture, + Capture.No, # type: ignore[attr-defined] ) return sim_panda @@ -90,7 +92,6 @@ async def test_get_capture_signals_gets_all_signals(sim_panda): async def test_get_signals_marked_for_capture(sim_panda): - capture_signals = { "block1.test_capture": sim_panda.block1.test_capture, "block2.test_capture": sim_panda.block2.test_capture, @@ -177,7 +178,6 @@ async def test_collect_stream_docs(sim_writer: PandaHDFWriter): async def test_numeric_blocks_correctly_formated(sim_writer: PandaHDFWriter): - async def get_numeric_signal(_): return { "device.block.1": CaptureSignalWrapper( diff --git a/tests/test_flyer_with_panda.py b/tests/test_flyer_with_panda.py index 573177e701..f680ad0395 100644 --- a/tests/test_flyer_with_panda.py +++ b/tests/test_flyer_with_panda.py @@ -74,10 +74,10 @@ async def collect_stream_docs( yield "stream_resource", self._file.stream_resource_doc if indices_written >= self._last_emitted: - indices = dict( - start=self._last_emitted, - stop=indices_written, - ) + indices = { + "start": self._last_emitted, + "stop": indices_written, + } self._last_emitted = indices_written self._last_flush = time.monotonic() yield "stream_datum", self._file.compose_stream_datum(indices)