Skip to content

Commit

Permalink
Include the explicit type in ColorLike and RectLike (#3183)
Browse files Browse the repository at this point in the history
* Include the explicit type in type unions

* Sort ColorLike types by how common they are
  • Loading branch information
aatle authored Oct 19, 2024
1 parent 120675a commit 8b09b1f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
24 changes: 21 additions & 3 deletions buildconfig/stubs/pygame/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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,
)
24 changes: 21 additions & 3 deletions src_py/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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,
)

0 comments on commit 8b09b1f

Please sign in to comment.