Start adding ANN ruff rules

This commit is contained in:
Julian Stirling 2025-08-27 13:21:34 +01:00
parent 154f063ab3
commit 24233412af
19 changed files with 123 additions and 46 deletions

View file

@ -127,7 +127,7 @@ class StackParams:
"""
return self.min_images_to_test + 15
def slice_to_save(self, sharpest_index):
def slice_to_save(self, sharpest_index: int):
"""Return the slice of images to save given the index of the sharpest image."""
images_each_side = (self.images_to_save - 1) // 2
return slice(
@ -401,7 +401,7 @@ class AutofocusThing(lt.Thing):
self,
stage: Stage,
sharpness_monitor: SharpnessMonitorDep,
dz=2000,
dz: int = 2000,
start: Literal["centre", "base"] = "centre",
):
"""Repeatedly autofocus the stage until it looks focused.

View file

@ -8,6 +8,7 @@ See repository root for licensing information.
from __future__ import annotations
from typing import Literal, Optional, Tuple, Any
from types import TracebackType
import json
import io
import time
@ -185,7 +186,12 @@ class BaseCamera(lt.Thing):
"""Open hardware connection when the Thing context manager is opened."""
raise NotImplementedError("CameraThings must define their own __enter__ method")
def __exit__(self, _exc_type, _exc_value, _traceback) -> None:
def __exit__(
self,
_exc_type: type[BaseException],
_exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
) -> None:
"""Close hardware connection when the Thing context manager is closed."""
raise NotImplementedError("CameraThings must define their own __exit__ method")
@ -575,7 +581,7 @@ class BaseCamera(lt.Thing):
return self.active_detector.status
@lt.thing_action
def update_detector_settings(self, data) -> None:
def update_detector_settings(self, data: dict[str, Any]) -> None:
"""Update the settings of the current detector.
This is an action not a setting/property as the data model depends on the

View file

@ -10,6 +10,7 @@ from __future__ import annotations
import logging
from typing import Literal, Optional
from types import TracebackType
from threading import Thread
import cv2
@ -42,7 +43,12 @@ class OpenCVCamera(BaseCamera):
self._capture_thread.start()
return self
def __exit__(self, _exc_type, _exc_value, _traceback):
def __exit__(
self,
_exc_type: type[BaseException],
_exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
):
"""Release the camera when the Thing context manager is closed.
Before releasing the camera the capture thread is closed.

View file

@ -15,7 +15,8 @@ https://datasheets.raspberrypi.com/camera/raspberry-pi-camera-guide.pdf
"""
from __future__ import annotations
from typing import Annotated, Iterator, Literal, Mapping, Optional, overload
from typing import Annotated, Iterator, Literal, Mapping, Optional, overload, Any
from types import TracebackType
import json
import logging
import os
@ -65,7 +66,12 @@ class PicameraStreamOutput(Output):
self.portal = portal
def outputframe(
self, frame, _keyframe=True, _timestamp=None, _packet=None, _audio=False
self,
frame: bytes,
_keyframe: Optional[bool] = True,
_timestamp: Optional[int] = None,
_packet: Any = None,
_audio: bool = False,
):
"""Add a frame to the stream's ringbuffer."""
self.stream.add_frame(frame, self.portal)
@ -396,7 +402,7 @@ class StreamingPiCamera2(BaseCamera):
return self._picamera is not None and self._picamera.started
@contextmanager
def _streaming_picamera(self, pause_stream=False) -> Iterator[Picamera2]:
def _streaming_picamera(self, pause_stream: bool = False) -> Iterator[Picamera2]:
"""Lock access to picamera and return the underlying ``Picamera2`` instance.
Optionally the stream can be paused to allow updating the camera settings.
@ -419,7 +425,12 @@ class StreamingPiCamera2(BaseCamera):
if pause_stream and already_streaming:
self.start_streaming()
def __exit__(self, exc_type, exc_value, traceback):
def __exit__(
self,
_exc_type: type[BaseException],
_exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
):
"""Close the picamera connection when the Thing context manager is closed."""
self.stop_streaming()
with self._streaming_picamera() as cam:
@ -725,7 +736,10 @@ class StreamingPiCamera2(BaseCamera):
return tuple(recalibrate_utils.get_static_ccm(self.tuning)[0]["ccm"])
@colour_correction_matrix.setter # type: ignore
def colour_correction_matrix(self, value) -> None:
def colour_correction_matrix(
self,
value: tuple[float, float, float, float, float, float, float, float, float],
) -> None:
recalibrate_utils.set_static_ccm(self.tuning, value)
if self._picamera is not None:

View file

@ -353,7 +353,9 @@ def upsample_channels(grids: np.ndarray, shape: tuple[int]) -> np.ndarray:
return zoom(grids, zoom_factors, order=1)[:, : shape[0], : shape[1]]
def downsampled_channels(channels: np.ndarray, blacklevel=64) -> list[np.ndarray]:
def downsampled_channels(
channels: np.ndarray, blacklevel: int = 64
) -> list[np.ndarray]:
"""Generate a downsampled, un-normalised image from which to calculate the LST.
TODO: blacklevel probably ought to be determined from the camera...

View file

@ -9,6 +9,7 @@ See repository root for licensing information.
from __future__ import annotations
import logging
from typing import Literal, Optional
from types import TracebackType
from threading import Thread
import time
@ -212,7 +213,12 @@ class SimulatedCamera(BaseCamera):
self.start_streaming()
return self
def __exit__(self, _exc_type, _exc_value, _traceback):
def __exit__(
self,
_exc_type: type[BaseException],
_exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
):
"""Close the capture thread when the Thing context manager is closed."""
if self.stream_active:
self._capture_enabled = False

View file

@ -7,7 +7,7 @@ It also controls external processes for live stitching composite images, and
the creation of the final stitched images.
"""
from typing import Optional
from typing import Optional, Self, TypeVar, ParamSpec, Callable, Concatenate
import threading
import os
import time
@ -30,6 +30,9 @@ from .camera_stage_mapping import CameraStageMapper
from .camera import CameraDependency as CameraClient
from .stage import StageDependency as StageDep
T = TypeVar("T")
P = ParamSpec("P")
CSMDep = lt.deps.direct_thing_client_dependency(
CameraStageMapper, "/camera_stage_mapping/"
)
@ -43,7 +46,9 @@ class ScanNotRunningError(RuntimeError):
"""Exception called when scan not running that requires a scan to be running."""
def _scan_running(method):
def _scan_running(
method: Callable[Concatenate[Self, P], T],
) -> Callable[Concatenate[Self, P], T]:
"""Decorate a method so that it will error if a scan is not running.
This decorator is used by all methods in SmartScanThing that are using
@ -52,7 +57,7 @@ def _scan_running(method):
the same time and released with the lock
"""
def scan_running_wrapper(self, *args, **kwargs):
def scan_running_wrapper(self: Self, *args: P.args, **kwargs: P.kwargs) -> T:
# Only start the method is the scan logger is set
if self._scan_logger is not None:
return method(self, *args, **kwargs)
@ -623,7 +628,7 @@ class SmartScanThing(lt.Thing):
if scan_info.number_of_images == 0:
self._delete_scan(scan_info.name, logger)
def _delete_scan(self, scan_name, logger: lt.deps.InvocationLogger) -> bool:
def _delete_scan(self, scan_name: str, logger: lt.deps.InvocationLogger) -> bool:
"""Delete a scan.
This is a wrapper around scan manager's delete_scan that logs to the

View file

@ -142,7 +142,7 @@ class BaseStage(lt.Thing):
self,
cancel: lt.deps.CancelHook,
block_cancellation: bool = False,
**kwargs: Mapping[str, int],
**kwargs: int,
):
"""Make a relative move. Keyword arguments should be axis names."""
self._hardware_move_relative(
@ -155,7 +155,7 @@ class BaseStage(lt.Thing):
self,
cancel: lt.deps.CancelHook,
block_cancellation: bool = False,
**kwargs: Mapping[str, int],
**kwargs: int,
):
"""Make a relative move in the coordinate system used by the physical hardware.
@ -170,7 +170,7 @@ class BaseStage(lt.Thing):
self,
cancel: lt.deps.CancelHook,
block_cancellation: bool = False,
**kwargs: Mapping[str, int],
**kwargs: int,
):
"""Make an absolute move. Keyword arguments should be axis names."""
self._hardware_move_absolute(
@ -183,7 +183,7 @@ class BaseStage(lt.Thing):
self,
cancel: lt.deps.CancelHook,
block_cancellation: bool = False,
**kwargs: Mapping[str, int],
**kwargs: int,
):
"""Make a absolute move in the coordinate system used by the physical hardware.

View file

@ -2,6 +2,8 @@
from __future__ import annotations
from typing import Optional
from types import TracebackType
from collections.abc import Mapping
import time
@ -33,7 +35,12 @@ class DummyStage(BaseStage):
"""Register the stage position when the Thing context manager is opened."""
self.instantaneous_position = self._hardware_position
def __exit__(self, _exc_type, _exc_value, _traceback):
def __exit__(
self,
_exc_type: type[BaseException],
_exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
):
"""Nothing to do when the Thing context manager is closed."""
axis_inverted = lt.ThingSetting(
@ -47,7 +54,7 @@ class DummyStage(BaseStage):
self,
cancel: lt.deps.CancelHook,
block_cancellation: bool = False,
**kwargs: Mapping[str, int],
**kwargs: int,
):
"""Make a relative move. Keyword arguments should be axis names."""
displacement = [kwargs.get(k, 0) for k in self.axis_names]
@ -85,7 +92,7 @@ class DummyStage(BaseStage):
self,
cancel: lt.deps.CancelHook,
block_cancellation: bool = False,
**kwargs: Mapping[str, int],
**kwargs: int,
):
"""Make an absolute move. Keyword arguments should be axis names."""
displacement = {

View file

@ -5,7 +5,8 @@ import logging
import threading
import time
from copy import copy
from typing import Iterator, Literal
from typing import Iterator, Literal, Optional
from types import TracebackType
from contextlib import contextmanager
from collections.abc import Mapping
@ -53,7 +54,12 @@ class SangaboardThing(BaseStage):
sb.query("blocking_moves false")
self.update_position()
def __exit__(self, _exc_type, _exc_value, _traceback):
def __exit__(
self,
_exc_type: type[BaseException],
_exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
):
"""Close the sangaboard connection when the Thing context manager is closed."""
with self.sangaboard() as sb:
sb.close()
@ -85,7 +91,7 @@ class SangaboardThing(BaseStage):
self,
cancel: lt.deps.CancelHook,
block_cancellation: bool = False,
**kwargs: Mapping[str, int],
**kwargs: int,
) -> None:
"""Make a relative move in the coordinate system used by the sangaboard."""
displacement = [kwargs.get(axis, 0) for axis in self.axis_names]
@ -112,7 +118,7 @@ class SangaboardThing(BaseStage):
self,
cancel: lt.deps.CancelHook,
block_cancellation: bool = False,
**kwargs: Mapping[str, int],
**kwargs: int,
) -> None:
"""Make a absolute move in the coordinate system used by the sangaboard."""
with self.sangaboard():