From d1158b1e106bbe0c8ac7f5baf77e924e35469d61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Fri, 8 Sep 2023 11:13:51 +0000 Subject: [PATCH] fix typing issues --- api/advent22_api/routers/_image.py | 17 ++++++++++------- api/advent22_api/routers/_misc.py | 6 +++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/api/advent22_api/routers/_image.py b/api/advent22_api/routers/_image.py index 94783c3..b89e6aa 100644 --- a/api/advent22_api/routers/_image.py +++ b/api/advent22_api/routers/_image.py @@ -1,10 +1,13 @@ import colorsys from dataclasses import dataclass -from typing import Self +from typing import Self, TypeAlias, cast import numpy as np from PIL import Image, ImageDraw, ImageFont +_RGB: TypeAlias = tuple[int, int, int] +_XY: TypeAlias = tuple[float, float] + @dataclass class AdventImage: @@ -44,7 +47,7 @@ class AdventImage: async def get_text_box( self, - xy: tuple[float, float], + xy: _XY, text: str | bytes, font: "ImageFont._Font", anchor: str | None = "mm", @@ -75,20 +78,20 @@ class AdventImage: async def get_average_color( self, box: tuple[int, int, int, int], - ) -> tuple[int, int, int]: + ) -> _RGB: """ Durchschnittsfarbe eines rechteckigen Ausschnitts in einem Bild berechnen """ pixel_data = self.img.crop(box).getdata() - mean_color: np.ndarray = np.mean(a=pixel_data, axis=0) + mean_color: np.ndarray = np.mean(pixel_data, axis=0) - return tuple(mean_color.astype(int).tolist()) + return cast(_RGB, tuple(mean_color.astype(int))) async def hide_text( self, - xy: tuple[float, float], + xy: _XY, text: str | bytes, font: "ImageFont._Font", anchor: str | None = "mm", @@ -128,7 +131,7 @@ class AdventImage: xy=xy, text=text, font=font, - fill=text_color, + fill=cast(_RGB, text_color), anchor=anchor, **text_kwargs, ) diff --git a/api/advent22_api/routers/_misc.py b/api/advent22_api/routers/_misc.py index ec44dd2..6188868 100644 --- a/api/advent22_api/routers/_misc.py +++ b/api/advent22_api/routers/_misc.py @@ -2,7 +2,7 @@ import itertools import random import re from io import BytesIO -from typing import Any, Self, Sequence +from typing import Any, Self, Sequence, cast from fastapi import Depends from fastapi.responses import StreamingResponse @@ -10,7 +10,7 @@ from PIL import Image, ImageFont from ..config import Config, get_config from ..dav_common import dav_file_exists, dav_get_file, dav_list_files -from ._image import AdventImage +from ._image import _XY, AdventImage ########## # RANDOM # @@ -101,7 +101,7 @@ async def get_auto_image( # Buchstabe verstecken await image.hide_text( - xy=tuple(rnd.choices(range(30, 470), k=2)), + xy=cast(_XY, tuple(rnd.choices(range(30, 470), k=2))), text=letter, font=ImageFont.truetype(font, 50), )