From 8b09b1f31eafc42b92b30808bba979399f1d283d Mon Sep 17 00:00:00 2001 From: aatle <168398276+aatle@users.noreply.github.com> Date: Sat, 19 Oct 2024 06:03:20 -0700 Subject: [PATCH] Include the explicit type in `ColorLike` and `RectLike` (#3183) * Include the explicit type in type unions * Sort ColorLike types by how common they are --- buildconfig/stubs/pygame/typing.pyi | 24 +++++++++++++++++++++--- src_py/typing.py | 24 +++++++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/buildconfig/stubs/pygame/typing.pyi b/buildconfig/stubs/pygame/typing.pyi index 45d793a4ac..ca88863a59 100644 --- a/buildconfig/stubs/pygame/typing.pyi +++ b/buildconfig/stubs/pygame/typing.pyi @@ -16,6 +16,10 @@ import sys from abc import abstractmethod from typing import IO, Callable, Tuple, Union, TypeVar, Protocol +from pygame.color import Color +from pygame.rect import Rect, FRect + + if sys.version_info >= (3, 9): from os import PathLike as _PathProtocol else: @@ -53,7 +57,7 @@ Point = SequenceLike[float] # This is used where ints are strictly required IntPoint = SequenceLike[int] -ColorLike = Union[int, str, SequenceLike[int]] +ColorLike = Union[Color, SequenceLike[int], str, int] class _HasRectAttribute(Protocol): @@ -63,8 +67,22 @@ class _HasRectAttribute(Protocol): def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... -RectLike = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute] +RectLike = Union[ + Rect, FRect, SequenceLike[float], SequenceLike[Point], _HasRectAttribute +] # cleanup namespace -del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol +del ( + sys, + abstractmethod, + Color, + Rect, + FRect, + IO, + Callable, + Tuple, + Union, + TypeVar, + Protocol, +) diff --git a/src_py/typing.py b/src_py/typing.py index 45d793a4ac..ca88863a59 100644 --- a/src_py/typing.py +++ b/src_py/typing.py @@ -16,6 +16,10 @@ from abc import abstractmethod from typing import IO, Callable, Tuple, Union, TypeVar, Protocol +from pygame.color import Color +from pygame.rect import Rect, FRect + + if sys.version_info >= (3, 9): from os import PathLike as _PathProtocol else: @@ -53,7 +57,7 @@ def __len__(self) -> int: ... # This is used where ints are strictly required IntPoint = SequenceLike[int] -ColorLike = Union[int, str, SequenceLike[int]] +ColorLike = Union[Color, SequenceLike[int], str, int] class _HasRectAttribute(Protocol): @@ -63,8 +67,22 @@ class _HasRectAttribute(Protocol): def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ... -RectLike = Union[SequenceLike[float], SequenceLike[Point], _HasRectAttribute] +RectLike = Union[ + Rect, FRect, SequenceLike[float], SequenceLike[Point], _HasRectAttribute +] # cleanup namespace -del sys, abstractmethod, IO, Callable, Tuple, Union, TypeVar, Protocol +del ( + sys, + abstractmethod, + Color, + Rect, + FRect, + IO, + Callable, + Tuple, + Union, + TypeVar, + Protocol, +)