Add return typehints to magic methods

This commit is contained in:
Julian Stirling 2025-08-27 14:04:25 +01:00
parent 4c46330959
commit 2ce49088e7
17 changed files with 41 additions and 34 deletions

View file

@ -69,7 +69,7 @@ class CameraMemoryBuffer:
_storage: dict[int, tuple[Any, Optional[dict]]]
def __init__(self):
def __init__(self) -> None:
"""Create the buffer instance."""
# This dictionary is the main store for data. Dictionaries are ordered since
# Python 3.6, so the order in the dictionary is the capture order
@ -170,7 +170,7 @@ class BaseCamera(lt.Thing):
lores_mjpeg_stream = lt.outputs.MJPEGStreamDescriptor()
_memory_buffer = CameraMemoryBuffer()
def __init__(self):
def __init__(self) -> None:
"""Initialise the base camera, this creates the background detectors.
This must be run by all child camera classes.

View file

@ -25,7 +25,7 @@ from . import BaseCamera
class OpenCVCamera(BaseCamera):
"""A Thing that provides and interface to an OpenCV Camera."""
def __init__(self, camera_index: int = 0):
def __init__(self, camera_index: int = 0) -> None:
"""Iniatilise the thing storing the index of the camera to use.
:param camera_index: The index of the camera to use for the microscope.
@ -35,7 +35,7 @@ class OpenCVCamera(BaseCamera):
self._capture_thread: Optional[Thread] = None
self._capture_enabled = False
def __enter__(self):
def __enter__(self) -> None:
"""Start the capture thread when the Thing context manager is opened."""
self.cap = cv2.VideoCapture(self.camera_index)
self._capture_enabled = True
@ -48,7 +48,7 @@ class OpenCVCamera(BaseCamera):
_exc_type: type[BaseException],
_exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
):
) -> None:
"""Release the camera when the Thing context manager is closed.
Before releasing the camera the capture thread is closed.

View file

@ -53,7 +53,9 @@ class MissingCalibrationError(RuntimeError):
class PicameraStreamOutput(Output):
"""An Output class that sends frames to a stream."""
def __init__(self, stream: lt.outputs.MJPEGStream, portal: lt.deps.BlockingPortal):
def __init__(
self, stream: lt.outputs.MJPEGStream, portal: lt.deps.BlockingPortal
) -> None:
"""Create an output that puts frames in an MJPEGStream.
We need to pass the stream object, and also the blocking portal, because
@ -126,7 +128,7 @@ class StreamingPiCamera2(BaseCamera):
generalisation.
"""
def __init__(self, camera_num: int = 0):
def __init__(self, camera_num: int = 0) -> None:
"""Initialise the camera with the given camera number.
This makes no connection to the camera (except to get the default tuning file).
@ -384,7 +386,7 @@ class StreamingPiCamera2(BaseCamera):
)
self._picamera_lock = RLock()
def __enter__(self):
def __enter__(self) -> None:
"""Start streaming when the Thing context manager is opened.
This opens the picamera connection, initialises the camera, sets the
@ -430,7 +432,7 @@ class StreamingPiCamera2(BaseCamera):
_exc_type: type[BaseException],
_exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
):
) -> None:
"""Close the picamera connection when the Thing context manager is closed."""
self.stop_streaming()
with self._streaming_picamera() as cam:

View file

@ -54,7 +54,7 @@ class SimulatedCamera(BaseCamera):
glyph_shape: tuple[int, int, int] = (91, 91, 3),
canvas_shape: tuple[int, int, int] = (3000, 4000, 3),
frame_interval: float = 0.1,
):
) -> None:
"""Initialise the simulated with settings for how images are generated.
:param shape: The shape (size) of the generated image.
@ -208,7 +208,7 @@ class SimulatedCamera(BaseCamera):
pos = {"x": 0, "y": 0, "z": 0}
return self.generate_image((pos["y"], pos["x"], pos["z"]))
def __enter__(self):
def __enter__(self) -> None:
"""Start the capture thread when the Thing context manager is opened."""
self.start_streaming()
return self
@ -218,7 +218,7 @@ class SimulatedCamera(BaseCamera):
_exc_type: type[BaseException],
_exc_value: Optional[BaseException],
_traceback: Optional[TracebackType],
):
) -> None:
"""Close the capture thread when the Thing context manager is closed."""
if self.stream_active:
self._capture_enabled = False