Skip to content

Commit acca63e

Browse files
committed
fix typing in pyramid.py
1 parent 6b4a75a commit acca63e

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

.github/workflows/mypy-type-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ jobs:
5050
tiatoolbox/models/__init__.py \
5151
tiatoolbox/models/models_abc.py \
5252
tiatoolbox/models/architecture/__init__.py \
53-
tiatoolbox/models/architecture/utils.py \
53+
tiatoolbox/models/architecture/utils.py

requirements/requirements_dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-r ../docs/requirements.txt
33
coverage>=7.0.0
44
docutils>=0.18.1
5-
mypy>=1.6.1
5+
mypy>=1.16
66
pip>=22.3
77
poetry-bumpversion>=0.3.1
88
pre-commit>=2.20.0

tiatoolbox/tools/pyramid.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import zipfile
1818
from io import BytesIO
1919
from pathlib import Path
20-
from typing import TYPE_CHECKING
20+
from typing import TYPE_CHECKING, Literal
2121

2222
import defusedxml
2323
import numpy as np
@@ -352,17 +352,13 @@ def save_tile(tile_path: Path, tile: Image.Image) -> None:
352352
)
353353

354354
else: # container == "tar":
355-
compression2mode = {
356-
None: "w",
357-
"gzip": "w:gz",
358-
"bz2": "w:bz2",
359-
"lzma": "w:xz",
360-
}
361-
if compression not in compression2mode:
355+
if compression not in [None, "gzip", "bz2", "lzma"]:
362356
msg = "Unsupported compression for tar."
363357
raise ValueError(msg)
364358

365-
tar_archive = tarfile.TarFile.open(path, mode=compression2mode[compression])
359+
compression_mode = self._compression_mode_to_literal(compression)
360+
361+
tar_archive = tarfile.TarFile.open(str(path), mode=compression_mode)
366362

367363
def save_tile(tile_path: Path, tile: Image.Image) -> None:
368364
"""Write the tile to the output zip."""
@@ -399,6 +395,19 @@ def __iter__(self: TilePyramidGenerator) -> Iterator:
399395
for x, y in np.ndindex(self.tile_grid_size(level)):
400396
yield self.get_tile(level=level, x=x, y=y)
401397

398+
def _compression_mode_to_literal(
399+
self: TilePyramidGenerator, mode: str | None
400+
) -> Literal["w", "w:gz", "w:bz2", "w:xz"]:
401+
"""Convert compression mode to a literal string."""
402+
if mode is None:
403+
return "w"
404+
if mode == "gzip":
405+
return "w:gz"
406+
if mode == "bz2":
407+
return "w:bz2"
408+
# mode is "lzma"
409+
return "w:xz"
410+
402411

403412
class ZoomifyGenerator(TilePyramidGenerator):
404413
r"""Pyramid tile generator with extra Zoomify specific methods.

0 commit comments

Comments
 (0)