Skip to content

Commit

Permalink
Fix mypy type
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Oct 2, 2024
1 parent 822f6ce commit f11686c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion tilecloud/filter/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, filter_pattern: Callable[[Tile], Tile], **kwargs: Any):
def __call__(self, tile: Tile) -> Tile:
assert tile.data is not None
image = PIL.Image.open(BytesIO(tile.data))
image = image.filter(self.filter) # type: ignore[no-untyped-call]
image = image.filter(self.filter)
bytes_io = BytesIO()
assert tile.content_type is not None
image.save(bytes_io, FORMAT_BY_CONTENT_TYPE.get(tile.content_type, "PNG"), **self.kwargs)
Expand Down
2 changes: 1 addition & 1 deletion tilecloud/store/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_one(self, tile: Tile) -> Optional[Tile]:
draw.line([(0, 255), (0, 0), (255, 0)], fill=self.color)
text = str(tile.tilecoord)
font = PIL.ImageFont.load_default()
bbox = font.getbbox(text) # type: ignore[no-untyped-call]
bbox = font.getbbox(text)
width = bbox[2] - bbox[0]
height = bbox[3] - bbox[1]
draw.text((127 - width / 2, 127 - height / 2), text, fill=self.color, font=font)
Expand Down
15 changes: 9 additions & 6 deletions tilecloud/store/mask.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from collections.abc import Iterator
from typing import Any, Optional
from typing import Any, Optional, Union

import PIL.Image
import PIL.ImageFile

from tilecloud import BoundingPyramid, Bounds, NotSupportedOperation, Tile, TileCoord, TileStore

Expand All @@ -11,6 +12,8 @@ class MaskTileStore(TileStore):
A black and white image representing present and absent tiles.
"""

image: Union[PIL.Image.Image, PIL.ImageFile.ImageFile]

def __init__(self, z: int, bounds: tuple[Bounds, Bounds], file: Optional[str] = None, **kwargs: Any):
TileStore.__init__(self, **kwargs)
self.zoom = z
Expand All @@ -29,7 +32,7 @@ def __init__(self, z: int, bounds: tuple[Bounds, Bounds], file: Optional[str] =
assert self.image.size == (self.width, self.height)
else:
self.image = PIL.Image.new("1", (self.width, self.height))
self.pixels = self.image.load() # type: ignore[no-untyped-call]
self.pixels = self.image.load()

def delete_one(self, tile: Tile) -> Tile:
assert self.xbounds.start is not None
Expand All @@ -38,15 +41,15 @@ def delete_one(self, tile: Tile) -> Tile:
x = tile.tilecoord.x - self.xbounds.start # pylint: disable=invalid-name
y = self.ybounds.stop - tile.tilecoord.y - 1 # pylint: disable=invalid-name
if 0 <= x < self.width and 0 <= y < self.height:
self.pixels[x, y] = 0
self.pixels[x, y] = 0 # type: ignore[index]
return tile

def list(self) -> Iterator[Tile]:
assert self.xbounds.start is not None
assert self.ybounds.stop is not None
for x in range(0, self.width): # pylint: disable=invalid-name
for y in range(0, self.height): # pylint: disable=invalid-name
if self.pixels[x, y]:
if self.pixels[x, y]: # type: ignore[index]
yield Tile(TileCoord(self.zoom, self.xbounds.start + x, self.ybounds.stop - y - 1))

def put_one(self, tile: Tile) -> Tile:
Expand All @@ -55,10 +58,10 @@ def put_one(self, tile: Tile) -> Tile:
x = tile.tilecoord.x - self.xbounds.start # pylint: disable=invalid-name
y = self.ybounds.stop - tile.tilecoord.y - 1 # pylint: disable=invalid-name
if 0 <= x < self.width and 0 <= y < self.height:
self.pixels[x, y] = 1
self.pixels[x, y] = 1 # type: ignore[index]
return tile

def save(self, file: str, format_pattern: type, **kwargs: Any) -> None:
def save(self, file: str, format_pattern: Optional[str], **kwargs: Any) -> None:
self.image.save(file, format_pattern, **kwargs)

def get_one(self, tile: Tile) -> Optional[Tile]:
Expand Down

0 comments on commit f11686c

Please sign in to comment.