Skip to content

Commit

Permalink
Fix formatting for docs and bugfixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
gresm committed Aug 26, 2024
1 parent e8e9a42 commit a987ecb
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 53 deletions.
4 changes: 2 additions & 2 deletions buildconfig/stubs/pygame/_event.pyi
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
26 changes: 4 additions & 22 deletions buildconfig/stubs/pygame/event.pyi
Original file line number Diff line number Diff line change
@@ -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]

Expand Down
37 changes: 28 additions & 9 deletions buildconfig/stubs/pygame/typing.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -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
6 changes: 3 additions & 3 deletions src_c/_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src_c/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
42 changes: 36 additions & 6 deletions src_py/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -118,6 +122,7 @@ class Event:
"""
Event(type, dict) -> Event
Event(type, **attributes) -> Event
pygame object for representing events
"""

Expand Down Expand Up @@ -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:
Expand All @@ -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)


Expand Down Expand Up @@ -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):
Expand Down
41 changes: 31 additions & 10 deletions src_py/typing.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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

0 comments on commit a987ecb

Please sign in to comment.