Skip to content

Commit

Permalink
Update typing to current standard
Browse files Browse the repository at this point in the history
  • Loading branch information
dhirving committed Feb 14, 2024
1 parent ff32292 commit f57fa36
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
18 changes: 9 additions & 9 deletions python/lsst/image_cutout_backend/_image_cutout_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
__all__ = ("ImageCutoutBackend", "Extraction")

import dataclasses
from typing import Optional, Sequence, Union
from collections.abc import Sequence
from uuid import UUID, uuid4

from lsst.afw.image import Exposure, Image, Mask, MaskedImage
Expand All @@ -42,7 +42,7 @@ class Extraction:
image cutout backend.
"""

cutout: Union[Image, Mask, MaskedImage, Exposure]
cutout: Image | Mask | MaskedImage | Exposure
"""The image cutout itself.
"""

Expand Down Expand Up @@ -137,7 +137,7 @@ def __init__(
butler: Butler,
projection_finder: ProjectionFinder,
output_root: ResourcePathExpression,
temporary_root: Optional[ResourcePathExpression] = None,
temporary_root: ResourcePathExpression | None = None,
):
self.butler = butler
self.projection_finder = projection_finder
Expand All @@ -159,13 +159,13 @@ def __init__(
"""Root path that extracted cutouts are written to (`ResourcePath`).
"""

temporary_root: Optional[ResourcePath]
temporary_root: ResourcePath | None
"""Local filesystem root to write files to before they are transferred to
``output_root``
"""

def process_ref(
self, stencil: SkyStencil, ref: DatasetRef, *, mask_plane: Optional[str] = "STENCIL"
self, stencil: SkyStencil, ref: DatasetRef, *, mask_plane: str | None = "STENCIL"
) -> ResourcePath:
"""Extract and write a cutout from a fully-resolved `DatasetRef`.
Expand Down Expand Up @@ -198,8 +198,8 @@ def process_uuid(
stencil: SkyStencil,
uuid: UUID,
*,
component: Optional[str] = None,
mask_plane: Optional[str] = "STENCIL",
component: str | None = None,
mask_plane: str | None = "STENCIL",
) -> ResourcePath:
"""Extract and write a cutout from a dataset identified by its UUID.
Expand Down Expand Up @@ -234,7 +234,7 @@ def process_search(
data_id: DataId,
collections: Sequence[str],
*,
mask_plane: Optional[str] = "STENCIL",
mask_plane: str | None = "STENCIL",
) -> ResourcePath:
"""Extract and write a cutout from a dataset identified by a
(dataset type, data ID, collection path) tuple.
Expand Down Expand Up @@ -318,7 +318,7 @@ def extract_ref(self, stencil: SkyStencil, ref: DatasetRef) -> Extraction:
origin_ref=ref,
)

def extract_uuid(self, stencil: SkyStencil, uuid: UUID, *, component: Optional[str] = None) -> Extraction:
def extract_uuid(self, stencil: SkyStencil, uuid: UUID, *, component: str | None = None) -> Extraction:
"""Extract a subimage from a dataset identified by its UUID.
Parameters
Expand Down
23 changes: 12 additions & 11 deletions python/lsst/image_cutout_backend/projection_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@

import re
from abc import ABC, abstractmethod
from typing import Dict, Iterable, Optional, Tuple, cast
from collections.abc import Iterable
from typing import cast

from lsst.afw.geom import SkyWcs
from lsst.daf.butler import Butler, DatasetRef
Expand All @@ -55,7 +56,7 @@ class ProjectionFinder(ABC):
"""

@abstractmethod
def find_projection(self, ref: DatasetRef, butler: Butler) -> Optional[Tuple[SkyWcs, Box2I]]:
def find_projection(self, ref: DatasetRef, butler: Butler) -> tuple[SkyWcs, Box2I] | None:
"""Run the finder on the given dataset with the given butler.
Parameters
Expand All @@ -76,7 +77,7 @@ def find_projection(self, ref: DatasetRef, butler: Butler) -> Optional[Tuple[Sky
"""
raise NotImplementedError()

def __call__(self, ref: DatasetRef, butler: Butler) -> Tuple[SkyWcs, Box2I]:
def __call__(self, ref: DatasetRef, butler: Butler) -> tuple[SkyWcs, Box2I]:
"""A thin wrapper around `find_projection` that raises `LookupError`
when no projection information was found.
Expand Down Expand Up @@ -132,7 +133,7 @@ class ReadComponents(ProjectionFinder):
or yield the most accurate WCS.
"""

def find_projection(self, ref: DatasetRef, butler: Butler) -> Optional[Tuple[SkyWcs, Box2I]]:
def find_projection(self, ref: DatasetRef, butler: Butler) -> tuple[SkyWcs, Box2I] | None:
# Docstring inherited.
if {"wcs", "bbox"}.issubset(ref.datasetType.storageClass.allComponents().keys()):
wcs = butler.get(ref.makeComponentRef("wcs"))
Expand Down Expand Up @@ -160,7 +161,7 @@ class TryComponentParents(ProjectionFinder):
def __init__(self, nested: ProjectionFinder):
self._nested = nested

def find_projection(self, ref: DatasetRef, butler: Butler) -> Optional[Tuple[SkyWcs, Box2I]]:
def find_projection(self, ref: DatasetRef, butler: Butler) -> tuple[SkyWcs, Box2I] | None:
# Docstring inherited.
while True:
if (result := self._nested.find_projection(ref, butler)) is not None:
Expand Down Expand Up @@ -197,9 +198,9 @@ class UseSkyMap(ProjectionFinder):
def __init__(self, dataset_type_name: str = "skyMap", collections: Iterable[str] = ("skymaps",)):
self._dataset_type_name = dataset_type_name
self._collections = tuple(collections)
self._cache: Dict[str, BaseSkyMap] = {}
self._cache: dict[str, BaseSkyMap] = {}

def find_projection(self, ref: DatasetRef, butler: Butler) -> Optional[Tuple[SkyWcs, Box2I]]:
def find_projection(self, ref: DatasetRef, butler: Butler) -> tuple[SkyWcs, Box2I] | None:
# Docstring inherited.
if "tract" in ref.dataId.graph.names:
assert "skymap" in ref.dataId.graph.names, "Guaranteed by expected dimension schema."
Expand Down Expand Up @@ -229,7 +230,7 @@ class Chain(ProjectionFinder):
def __init__(self, *nested: ProjectionFinder):
self._nested = tuple(nested)

def find_projection(self, ref: DatasetRef, butler: Butler) -> Optional[Tuple[SkyWcs, Box2I]]:
def find_projection(self, ref: DatasetRef, butler: Butler) -> tuple[SkyWcs, Box2I] | None:
# Docstring inherited.
for f in self._nested:
if (result := f.find_projection(ref, butler)) is not None:
Expand All @@ -256,14 +257,14 @@ class MatchDatasetTypeName(ProjectionFinder):
def __init__(
self,
regex: str,
on_match: Optional[ProjectionFinder] = None,
otherwise: Optional[ProjectionFinder] = None,
on_match: ProjectionFinder | None = None,
otherwise: ProjectionFinder | None = None,
):
self._regex = re.compile(regex)
self._on_match = on_match
self._otherwise = otherwise

def find_projection(self, ref: DatasetRef, butler: Butler) -> Optional[Tuple[SkyWcs, Box2I]]:
def find_projection(self, ref: DatasetRef, butler: Butler) -> tuple[SkyWcs, Box2I] | None:
# Docstring inherited.
if self._regex.match(ref.datasetType.name):
if self._on_match is not None:
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/image_cutout_backend/stencils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@

import struct
from abc import ABC, abstractmethod
from collections.abc import Iterable
from hashlib import blake2b
from typing import Iterable, Optional

import astropy.coordinates
import lsst.afw.detection
Expand Down Expand Up @@ -104,7 +104,7 @@ class PixelPolygon(PixelStencil):
a pixel. This may change in the future.
"""

def __init__(self, polygon: lsst.afw.geom.polygon.Polygon, bbox: Optional[Box2I] = None):
def __init__(self, polygon: lsst.afw.geom.polygon.Polygon, bbox: Box2I | None = None):
self._polygon = polygon
self._bbox = Box2I(polygon.getBBox())
if bbox is not None:
Expand Down

0 comments on commit f57fa36

Please sign in to comment.