From 69ae6662cbfa95555488d4f2f4858f1fb88dfde3 Mon Sep 17 00:00:00 2001 From: AsfhtgkDavid Date: Sat, 25 Jan 2025 22:07:46 +0200 Subject: [PATCH] Improved typing for ImageDraw --- src/PIL/ImageDraw.py | 30 +++++++++++++++--------------- src/PIL/_typing.py | 9 ++++++++- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index d8e4c0c60..455a93b68 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -39,7 +39,7 @@ from typing import TYPE_CHECKING, Any, AnyStr, Callable, Union, cast from . import Image, ImageColor from ._deprecate import deprecate -from ._typing import Coords +from ._typing import Coords, Anchor, Align, Direction # experimental access to the outline API Outline: Callable[[], Image.core._Outline] | None @@ -241,7 +241,7 @@ class ImageDraw: xy: Coords, fill: _Ink | None = None, width: int = 0, - joint: str | None = None, + joint: Literal["curve"] | None = None, ) -> None: """Draw a line, or a connected sequence of line segments.""" ink = self._getink(fill)[0] @@ -583,10 +583,10 @@ class ImageDraw: | ImageFont.TransposedFont | None ) = None, - anchor: str | None = None, + anchor: Anchor | None = None, spacing: float = 4, - align: str = "left", - direction: str | None = None, + align: Align = "left", + direction: Direction | None = None, features: list[str] | None = None, language: str | None = None, stroke_width: float = 0, @@ -708,10 +708,10 @@ class ImageDraw: | ImageFont.TransposedFont | None ) = None, - anchor: str | None = None, + anchor: Anchor | None = None, spacing: float = 4, - align: str = "left", - direction: str | None = None, + align: Align = "left", + direction: Direction | None = None, features: list[str] | None = None, language: str | None = None, stroke_width: float = 0, @@ -798,7 +798,7 @@ class ImageDraw: | ImageFont.TransposedFont | None ) = None, - direction: str | None = None, + direction: Direction | None = None, features: list[str] | None = None, language: str | None = None, embedded_color: bool = False, @@ -828,10 +828,10 @@ class ImageDraw: | ImageFont.TransposedFont | None ) = None, - anchor: str | None = None, + anchor: Anchor | None = None, spacing: float = 4, - align: str = "left", - direction: str | None = None, + align: Align = "left", + direction: Direction | None = None, features: list[str] | None = None, language: str | None = None, stroke_width: float = 0, @@ -878,10 +878,10 @@ class ImageDraw: | ImageFont.TransposedFont | None ) = None, - anchor: str | None = None, + anchor: Anchor | None = None, spacing: float = 4, - align: str = "left", - direction: str | None = None, + align: Align = "left", + direction: Direction | None = None, features: list[str] | None = None, language: str | None = None, stroke_width: float = 0, diff --git a/src/PIL/_typing.py b/src/PIL/_typing.py index 34a9a81e1..2b0930323 100644 --- a/src/PIL/_typing.py +++ b/src/PIL/_typing.py @@ -3,7 +3,7 @@ from __future__ import annotations import os import sys from collections.abc import Sequence -from typing import TYPE_CHECKING, Any, Protocol, TypeVar, Union +from typing import TYPE_CHECKING, Any, Protocol, TypeVar, Union, Literal if TYPE_CHECKING: from numbers import _IntegralLike as IntegralLike @@ -49,5 +49,12 @@ class SupportsRead(Protocol[_T_co]): StrOrBytesPath = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]] +horizontal_anchors = ("l", "m", "r", "s") +vertical_anchors = ("a", "t", "m", "s", "b", "d") +Anchor = Literal[*(horizontal_anchor + vertical_anchor for horizontal_anchor in horizontal_anchors for vertical_anchor in vertical_anchors)] + +Align = Literal["left", "center", "right"] + +Direction = Literal["rtl", "ltr", "ttb"] __all__ = ["Buffer", "IntegralLike", "StrOrBytesPath", "SupportsRead", "TypeGuard"]