Type Guards#
weitsicht provides a few small helper functions that act as type guards.
They are mainly useful for static type checking (pyright, mypy), where they can
help narrow base classes (like ImageBase) to concrete subclasses (like
ImagePerspective) based on the runtime type discriminator.
Available guards#
is_opencv_camera: narrowCameraBasePerspective→CameraOpenCVPerspectiveis_camera_type: check a camera discriminator (narrowing works when called with a literal, e.g.CameraType.OpenCV)is_perspective_image: narrowImageBase→ImagePerspectiveis_ortho_image: narrowImageBase→ImageOrtho
They are available from weitsicht (top-level) and from weitsicht.type_guards.
Examples#
from weitsicht import ImageBase, is_perspective_image
def process(img: ImageBase) -> None:
if is_perspective_image(img):
# here img is treated as ImagePerspective by type checkers
_ = img.camera
else:
# here img is treated as ImageOrtho | ImageBase (depending on your checker)
pass
from weitsicht import CameraBasePerspective, CameraType, is_camera_type
def process_camera(cam: CameraBasePerspective) -> None:
if is_camera_type(cam, CameraType.OpenCV):
# here cam is treated as CameraOpenCVPerspective by type checkers
_ = cam.camera_matrix