🔧 minor refactoring

- use `type` keyword instead of `typing.TypeAlias`
- use `str` instead of `AnyStr`
This commit is contained in:
Jörn-Michael Miehe 2026-02-18 03:21:03 +01:00
parent c7679072aa
commit 67eebecd39
3 changed files with 13 additions and 16 deletions

View file

@ -1,7 +1,7 @@
import colorsys import colorsys
import logging import logging
from dataclasses import dataclass from dataclasses import dataclass
from typing import AnyStr, Self, TypeAlias from typing import Self, cast
import numpy as np import numpy as np
from PIL import Image as PILImage from PIL import Image as PILImage
@ -11,9 +11,9 @@ from PIL.ImageFont import FreeTypeFont
from .config import Config from .config import Config
_RGB: TypeAlias = tuple[int, int, int] type _RGB = tuple[int, int, int]
_XY: TypeAlias = tuple[float, float] type _XY = tuple[float, float]
_Box: TypeAlias = tuple[int, int, int, int] type _Box = tuple[int, int, int, int]
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -56,7 +56,7 @@ class AdventImage:
async def get_text_box( async def get_text_box(
self, self,
xy: _XY, xy: _XY,
text: AnyStr, text: str,
font: FreeTypeFont, font: FreeTypeFont,
anchor: str | None = "mm", anchor: str | None = "mm",
**text_kwargs, **text_kwargs,
@ -95,12 +95,12 @@ class AdventImage:
pixel_data = np.asarray(self.img.crop(box)) pixel_data = np.asarray(self.img.crop(box))
mean_color: np.ndarray = np.mean(pixel_data, axis=(0, 1)) mean_color: np.ndarray = np.mean(pixel_data, axis=(0, 1))
return _RGB(mean_color.astype(int)) return cast(_RGB, mean_color.astype(int))
async def hide_text( async def hide_text(
self, self,
xy: _XY, xy: _XY,
text: AnyStr, text: str,
font: FreeTypeFont, font: FreeTypeFont,
anchor: str | None = "mm", anchor: str | None = "mm",
**text_kwargs, **text_kwargs,
@ -134,8 +134,10 @@ class AdventImage:
else: else:
tc_v -= 3 tc_v -= 3
text_color = colorsys.hsv_to_rgb(tc_h, tc_s, tc_v) text_color: tuple[int | float, int | float, int | float] = colorsys.hsv_to_rgb(
text_color = _RGB(int(val) for val in text_color) tc_h, tc_s, tc_v
)
text_color = cast(_RGB, tuple(int(val) for val in text_color))
# Buchstaben verstecken # Buchstaben verstecken
ImageDraw.Draw(self.img).text( ImageDraw.Draw(self.img).text(

View file

@ -1,5 +1,4 @@
import tomllib import tomllib
from typing import TypeAlias
import tomli_w import tomli_w
from fastapi import Depends from fastapi import Depends
@ -20,7 +19,7 @@ class DoorSaved(BaseModel):
y2: int y2: int
DoorsSaved: TypeAlias = list[DoorSaved] type DoorsSaved = list[DoorSaved]
class CalendarConfig(BaseModel): class CalendarConfig(BaseModel):

View file

@ -1,10 +1,6 @@
from typing import TypeVar
from pydantic import BaseModel from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import BaseSettings, SettingsConfigDict
T = TypeVar("T")
class Credentials(BaseModel): class Credentials(BaseModel):
username: str = "" username: str = ""
@ -71,7 +67,7 @@ class Settings(BaseSettings):
# openapi settings # openapi settings
##### #####
def __dev_value(self, value: T) -> T | None: def __dev_value[T](self, value: T) -> T | None:
if self.production_mode: if self.production_mode:
return None return None