diff --git a/buildconfig/stubs/pygame/_event.pyi b/buildconfig/stubs/pygame/_event.pyi index bf8fdf87a0..b656b0f3a0 100644 --- a/buildconfig/stubs/pygame/_event.pyi +++ b/buildconfig/stubs/pygame/_event.pyi @@ -1,8 +1,8 @@ from __future__ import annotations -from ._common import Sequence, EventLike +from .typing import SequenceLike, EventLike -_EventTypes = int | Sequence[int] +_EventTypes = int | SequenceLike[int] def pump(dopump: bool, /) -> None: ... def get( diff --git a/buildconfig/stubs/pygame/event.pyi b/buildconfig/stubs/pygame/event.pyi index e8e3f800b5..f20c4bf6e5 100644 --- a/buildconfig/stubs/pygame/event.pyi +++ b/buildconfig/stubs/pygame/event.pyi @@ -1,30 +1,12 @@ from __future__ import annotations -from typing import Any, Protocol +from typing import Any -from pygame.typing import SequenceLike +from pygame.typing import SequenceLike, EventLike -# TODO: Should this be moved to "pygame.typing"? -class _EventLike(Protocol): - def __init__( - self, type: int, dict: dict[str, Any] | None = None, **kwargs: Any - ) -> None: ... - def __getattribute__(self, name: str) -> Any: ... - def __setattr__(self, name: str, value: Any) -> None: ... - def __delattr__(self, name: str) -> None: ... - def __int__(self) -> int: ... - def __bool__(self) -> bool: ... - def __eq__(self, other: Any) -> bool: ... - - @property - def type(self) -> int: ... - @property - def dict(self) -> dict[str, Any]: ... - - -class Event(_EventLike): - ... +class Event(EventLike): + def __new__(cls, *args: Any, **kwargs: Any) -> "Event": ... _EventTypes = int | SequenceLike[int] diff --git a/buildconfig/stubs/pygame/typing.pyi b/buildconfig/stubs/pygame/typing.pyi index 883fe2def1..22e9028f16 100644 --- a/buildconfig/stubs/pygame/typing.pyi +++ b/buildconfig/stubs/pygame/typing.pyi @@ -1,10 +1,11 @@ """Set of common pygame type aliases for proper typehint annotations""" +from __future__ import annotations # NOTE: `src_py/typing.py` and `buildconfig/stubs/pygame/typing.pyi` must be duplicates. # Use the command `python buildconfig/stubs/gen_stubs.py` to copy typing.py to typing.pyi import sys -from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, SupportsIndex +from typing import IO, Callable, TypeVar, Protocol, SupportsIndex, Any if sys.version_info >= (3, 9): from os import PathLike as _PathProtocol @@ -16,9 +17,9 @@ else: # For functions that take a file name -PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]] +PathLike = str | bytes | _PathProtocol[str] | _PathProtocol[bytes] # Most pygame functions that take a file argument should be able to handle a FileLike type -FileLike = Union[PathLike, IO[bytes], IO[str]] +FileLike = PathLike | IO[bytes] | IO[str] _T_co = TypeVar("_T_co", covariant=True) @@ -41,19 +42,37 @@ Coordinate = SequenceLike[float] IntCoordinate = SequenceLike[int] # Used for functions that return an RGBA tuple -RGBATuple = Tuple[int, int, int, int] -ColorLike = Union[int, str, SequenceLike[int]] +RGBATuple = tuple[int, int, int, int] +ColorLike = int | str | SequenceLike[int] -_CanBeRect = SequenceLike[Union[float, Coordinate]] +_CanBeRect = SequenceLike[float | Coordinate] class _HasRectAttribute(Protocol): # An object that has a rect attribute that is either a rect, or a function # that returns a rect conforms to the rect protocol - rect: Union["RectLike", Callable[[], "RectLike"]] + rect: "RectLike" | Callable[[], "RectLike"] -RectLike = Union[_CanBeRect, _HasRectAttribute] +RectLike = _CanBeRect | _HasRectAttribute + +class EventLike(Protocol): + def __init__( + self, type: int, dict: dict[str, Any] | None = None, **kwargs: Any + ) -> None: ... + def __new__(cls, *args: Any, **kwargs: Any) -> "EventLike": ... + def __getattribute__(self, name: str) -> Any: ... + def __setattr__(self, name: str, value: Any) -> None: ... + def __delattr__(self, name: str) -> None: ... + def __int__(self) -> int: ... + def __bool__(self) -> bool: ... + def __eq__(self, other: Any) -> bool: ... + + @property + def type(self) -> int: ... + @property + def dict(self) -> dict[str, Any]: ... + # cleanup namespace -del sys, IO, Callable, Tuple, Union, TypeVar, Protocol, SupportsIndex +del sys, IO, Callable, TypeVar, Protocol, SupportsIndex diff --git a/src_c/_event.c b/src_c/_event.c index e2e6902d03..56cc83a4e7 100644 --- a/src_c/_event.c +++ b/src_c/_event.c @@ -678,13 +678,13 @@ pgEvent_AutoInit(PyObject *self, PyObject *_null) static int pg_post_event(int type, PyObject *obj) { + if (type == -1) + return -1; + SDL_Event event = {0}; event.type = _pg_pgevent_proxify(type); - if (event.type == -1) - return -1; - if (obj) Py_INCREF(obj); diff --git a/src_c/time.c b/src_c/time.c index 1c71cced75..e6d81990f0 100644 --- a/src_c/time.c +++ b/src_c/time.c @@ -264,7 +264,7 @@ timer_callback(Uint32 interval, void *param) } else { if (SDL_WasInit(SDL_INIT_VIDEO)) { - PyGILState_STATE gstate; + PyGILState_STATE gstate = 0; if (evtimer->obj) gstate = PyGILState_Ensure(); diff --git a/src_py/event.py b/src_py/event.py index 5ece5ec6ae..98a5533841 100644 --- a/src_py/event.py +++ b/src_py/event.py @@ -105,7 +105,11 @@ def event_name(type: int) -> str: - "event_name(type, /) -> string\nget the string name from an event id" + """ + event_name(type, /) -> string + + get the string name from an event id + """ if type in _NAMES_MAPPING: return _NAMES_MAPPING[type] @@ -118,6 +122,7 @@ class Event: """ Event(type, dict) -> Event Event(type, **attributes) -> Event + pygame object for representing events """ @@ -208,7 +213,11 @@ def quit(): def custom_type(): - """custom_type() -> int\nmake custom user event type""" + """ + custom_type() -> int + + make custom user event type + """ global _custom_event if _custom_event >= pg.NUMEVENTS: @@ -219,7 +228,11 @@ def custom_type(): def pump(): - "pump() -> None\ninternally process pygame event handlers" + """ + pump() -> None + + internally process pygame event handlers + """ return _pump(True) @@ -252,19 +265,36 @@ def _setter(val: bool, type: int | Iterable[int] | None, *args: int): def set_blocked(type: int | Iterable[int] | None, *args: int): - "set_blocked(type, /) -> None\nset_blocked(typelist, /) -> None\nset_blocked(None) -> None\ncontrol which events are blocked on the queue" + """ + set_blocked(type, /) -> None + set_blocked(typelist, /) -> None + set_blocked(None) -> None + + control which events are blocked on the queue + """ _setter(False, type, *args) def set_allowed(type: int | Iterable[int] | None, *args: int): - "set_allowed(type, /) -> None\nset_allowed(typelist, /) -> None\nset_allowed(None) -> None\ncontrol which events are allowed on the queue" + """ + set_allowed(type, /) -> None + set_allowed(typelist, /) -> None + set_allowed(None) -> None + + control which events are allowed on the queue + """ _setter(True, type, *args) def get_blocked(type: int | Iterable[int], *args: int): - "get_blocked(type, /) -> bool\nget_blocked(typelist, /) -> bool\ntest if a type of event is blocked from the queue" + """ + get_blocked(type, /) -> bool + get_blocked(typelist, /) -> bool + + test if a type of event is blocked from the queue + """ for t in _parse(type, args): if not _allowed_get(t): diff --git a/src_py/typing.py b/src_py/typing.py index 883fe2def1..23c6885c94 100644 --- a/src_py/typing.py +++ b/src_py/typing.py @@ -1,24 +1,26 @@ """Set of common pygame type aliases for proper typehint annotations""" +from __future__ import annotations + # NOTE: `src_py/typing.py` and `buildconfig/stubs/pygame/typing.pyi` must be duplicates. # Use the command `python buildconfig/stubs/gen_stubs.py` to copy typing.py to typing.pyi import sys -from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, SupportsIndex +from typing import IO, Callable, TypeVar, Protocol, SupportsIndex, Any if sys.version_info >= (3, 9): from os import PathLike as _PathProtocol else: - _T = TypeVar("_T", bound=Union[str, bytes]) + _T = TypeVar("_T", bound=str | bytes) class _PathProtocol(Protocol[_T]): def __fspath__(self) -> _T: ... # For functions that take a file name -PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]] +PathLike = str | bytes | _PathProtocol[str] | _PathProtocol[bytes] # Most pygame functions that take a file argument should be able to handle a FileLike type -FileLike = Union[PathLike, IO[bytes], IO[str]] +FileLike = PathLike | IO[bytes] | IO[str] _T_co = TypeVar("_T_co", covariant=True) @@ -41,19 +43,38 @@ def __len__(self) -> int: ... IntCoordinate = SequenceLike[int] # Used for functions that return an RGBA tuple -RGBATuple = Tuple[int, int, int, int] -ColorLike = Union[int, str, SequenceLike[int]] +RGBATuple = tuple[int, int, int, int] +ColorLike = int | str | SequenceLike[int] -_CanBeRect = SequenceLike[Union[float, Coordinate]] +_CanBeRect = SequenceLike[float | Coordinate] class _HasRectAttribute(Protocol): # An object that has a rect attribute that is either a rect, or a function # that returns a rect conforms to the rect protocol - rect: Union["RectLike", Callable[[], "RectLike"]] + rect: "RectLike" | Callable[[], "RectLike"] + + +RectLike = _CanBeRect | _HasRectAttribute + + +class EventLike(Protocol): + def __init__( + self, type: int, dict: dict[str, Any] | None = None, **kwargs: Any + ) -> None: ... + def __new__(cls, *args: Any, **kwargs: Any) -> "EventLike": ... + def __getattribute__(self, name: str) -> Any: ... + def __setattr__(self, name: str, value: Any) -> None: ... + def __delattr__(self, name: str) -> None: ... + def __int__(self) -> int: ... + def __bool__(self) -> bool: ... + def __eq__(self, other: Any) -> bool: ... + @property + def type(self) -> int: ... + @property + def dict(self) -> dict[str, Any]: ... -RectLike = Union[_CanBeRect, _HasRectAttribute] # cleanup namespace -del sys, IO, Callable, Tuple, Union, TypeVar, Protocol, SupportsIndex +del sys, IO, Callable, TypeVar, Protocol, SupportsIndex