Skip to content

Commit 42a314a

Browse files
committed
Fix formatting for docs and bugfixes.
1 parent e8e9a42 commit 42a314a

File tree

7 files changed

+108
-54
lines changed

7 files changed

+108
-54
lines changed

buildconfig/stubs/pygame/_event.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import annotations
22

3-
from ._common import Sequence, EventLike
3+
from .typing import SequenceLike, EventLike
44

5-
_EventTypes = int | Sequence[int]
5+
_EventTypes = int | SequenceLike[int]
66

77
def pump(dopump: bool, /) -> None: ...
88
def get(

buildconfig/stubs/pygame/event.pyi

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
from __future__ import annotations
22

3-
from typing import Any, Protocol
3+
from typing import Any
44

5-
from pygame.typing import SequenceLike
5+
from pygame.typing import SequenceLike, EventLike
66

7-
# TODO: Should this be moved to "pygame.typing"?
87

9-
class _EventLike(Protocol):
10-
def __init__(
11-
self, type: int, dict: dict[str, Any] | None = None, **kwargs: Any
12-
) -> None: ...
13-
def __getattribute__(self, name: str) -> Any: ...
14-
def __setattr__(self, name: str, value: Any) -> None: ...
15-
def __delattr__(self, name: str) -> None: ...
16-
def __int__(self) -> int: ...
17-
def __bool__(self) -> bool: ...
18-
def __eq__(self, other: Any) -> bool: ...
19-
20-
@property
21-
def type(self) -> int: ...
22-
@property
23-
def dict(self) -> dict[str, Any]: ...
24-
25-
26-
class Event(_EventLike):
27-
...
8+
class Event(EventLike):
9+
def __new__(cls, *args: Any, **kwargs: Any) -> "Event": ...
2810

2911
_EventTypes = int | SequenceLike[int]
3012

buildconfig/stubs/pygame/typing.pyi

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
"""Set of common pygame type aliases for proper typehint annotations"""
22

3+
from __future__ import annotations
4+
35
# NOTE: `src_py/typing.py` and `buildconfig/stubs/pygame/typing.pyi` must be duplicates.
46
# Use the command `python buildconfig/stubs/gen_stubs.py` to copy typing.py to typing.pyi
57

68
import sys
7-
from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, SupportsIndex
9+
from typing import IO, Callable, TypeVar, Protocol, SupportsIndex, Any
810

911
if sys.version_info >= (3, 9):
1012
from os import PathLike as _PathProtocol
1113
else:
12-
_T = TypeVar("_T", bound=Union[str, bytes])
14+
_T = TypeVar("_T", bound="str | bytes", covariant=True)
1315

1416
class _PathProtocol(Protocol[_T]):
1517
def __fspath__(self) -> _T: ...
1618

1719

1820
# For functions that take a file name
19-
PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]]
21+
PathLike = str | bytes | _PathProtocol[str] | _PathProtocol[bytes]
2022
# Most pygame functions that take a file argument should be able to handle a FileLike type
21-
FileLike = Union[PathLike, IO[bytes], IO[str]]
23+
FileLike = PathLike | IO[bytes] | IO[str]
2224

2325
_T_co = TypeVar("_T_co", covariant=True)
2426

@@ -41,19 +43,38 @@ Coordinate = SequenceLike[float]
4143
IntCoordinate = SequenceLike[int]
4244

4345
# Used for functions that return an RGBA tuple
44-
RGBATuple = Tuple[int, int, int, int]
45-
ColorLike = Union[int, str, SequenceLike[int]]
46+
RGBATuple = tuple[int, int, int, int]
47+
ColorLike = int | str | SequenceLike[int]
4648

47-
_CanBeRect = SequenceLike[Union[float, Coordinate]]
49+
_CanBeRect = SequenceLike[float | Coordinate]
4850

4951

5052
class _HasRectAttribute(Protocol):
5153
# An object that has a rect attribute that is either a rect, or a function
5254
# that returns a rect conforms to the rect protocol
53-
rect: Union["RectLike", Callable[[], "RectLike"]]
55+
rect: "RectLike" | Callable[[], "RectLike"]
56+
57+
58+
RectLike = _CanBeRect | _HasRectAttribute
59+
60+
61+
class EventLike(Protocol):
62+
def __init__(
63+
self, type: int, dict: dict[str, Any] | None = None, **kwargs: Any
64+
) -> None: ...
65+
def __new__(cls, *args: Any, **kwargs: Any) -> "EventLike": ...
66+
def __getattribute__(self, name: str) -> Any: ...
67+
def __setattr__(self, name: str, value: Any) -> None: ...
68+
def __delattr__(self, name: str) -> None: ...
69+
def __int__(self) -> int: ...
70+
def __bool__(self) -> bool: ...
71+
def __eq__(self, other: Any) -> bool: ...
5472

73+
@property
74+
def type(self) -> int: ...
75+
@property
76+
def dict(self) -> dict[str, Any]: ...
5577

56-
RectLike = Union[_CanBeRect, _HasRectAttribute]
5778

5879
# cleanup namespace
59-
del sys, IO, Callable, Tuple, Union, TypeVar, Protocol, SupportsIndex
80+
del sys, IO, Callable, TypeVar, Protocol, SupportsIndex

src_c/_event.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,13 +678,13 @@ pgEvent_AutoInit(PyObject *self, PyObject *_null)
678678
static int
679679
pg_post_event(int type, PyObject *obj)
680680
{
681+
if (type == -1)
682+
return -1;
683+
681684
SDL_Event event = {0};
682685

683686
event.type = _pg_pgevent_proxify(type);
684687

685-
if (event.type == -1)
686-
return -1;
687-
688688
if (obj)
689689
Py_INCREF(obj);
690690

src_c/time.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ timer_callback(Uint32 interval, void *param)
264264
}
265265
else {
266266
if (SDL_WasInit(SDL_INIT_VIDEO)) {
267-
PyGILState_STATE gstate;
267+
PyGILState_STATE gstate = 0;
268268

269269
if (evtimer->obj)
270270
gstate = PyGILState_Ensure();

src_py/event.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@
105105

106106

107107
def event_name(type: int) -> str:
108-
"event_name(type, /) -> string\nget the string name from an event id"
108+
"""
109+
event_name(type, /) -> string
110+
111+
get the string name from an event id
112+
"""
109113

110114
if type in _NAMES_MAPPING:
111115
return _NAMES_MAPPING[type]
@@ -118,6 +122,7 @@ class Event:
118122
"""
119123
Event(type, dict) -> Event
120124
Event(type, **attributes) -> Event
125+
121126
pygame object for representing events
122127
"""
123128

@@ -208,7 +213,11 @@ def quit():
208213

209214

210215
def custom_type():
211-
"""custom_type() -> int\nmake custom user event type"""
216+
"""
217+
custom_type() -> int
218+
219+
make custom user event type
220+
"""
212221
global _custom_event
213222

214223
if _custom_event >= pg.NUMEVENTS:
@@ -219,7 +228,11 @@ def custom_type():
219228

220229

221230
def pump():
222-
"pump() -> None\ninternally process pygame event handlers"
231+
"""
232+
pump() -> None
233+
234+
internally process pygame event handlers
235+
"""
223236
return _pump(True)
224237

225238

@@ -252,19 +265,36 @@ def _setter(val: bool, type: int | Iterable[int] | None, *args: int):
252265

253266

254267
def set_blocked(type: int | Iterable[int] | None, *args: int):
255-
"set_blocked(type, /) -> None\nset_blocked(typelist, /) -> None\nset_blocked(None) -> None\ncontrol which events are blocked on the queue"
268+
"""
269+
set_blocked(type, /) -> None
270+
set_blocked(typelist, /) -> None
271+
set_blocked(None) -> None
272+
273+
control which events are blocked on the queue
274+
"""
256275

257276
_setter(False, type, *args)
258277

259278

260279
def set_allowed(type: int | Iterable[int] | None, *args: int):
261-
"set_allowed(type, /) -> None\nset_allowed(typelist, /) -> None\nset_allowed(None) -> None\ncontrol which events are allowed on the queue"
280+
"""
281+
set_allowed(type, /) -> None
282+
set_allowed(typelist, /) -> None
283+
set_allowed(None) -> None
284+
285+
control which events are allowed on the queue
286+
"""
262287

263288
_setter(True, type, *args)
264289

265290

266291
def get_blocked(type: int | Iterable[int], *args: int):
267-
"get_blocked(type, /) -> bool\nget_blocked(typelist, /) -> bool\ntest if a type of event is blocked from the queue"
292+
"""
293+
get_blocked(type, /) -> bool
294+
get_blocked(typelist, /) -> bool
295+
296+
test if a type of event is blocked from the queue
297+
"""
268298

269299
for t in _parse(type, args):
270300
if not _allowed_get(t):

src_py/typing.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
"""Set of common pygame type aliases for proper typehint annotations"""
22

3+
from __future__ import annotations
4+
35
# NOTE: `src_py/typing.py` and `buildconfig/stubs/pygame/typing.pyi` must be duplicates.
46
# Use the command `python buildconfig/stubs/gen_stubs.py` to copy typing.py to typing.pyi
57

68
import sys
7-
from typing import IO, Callable, Tuple, Union, TypeVar, Protocol, SupportsIndex
9+
from typing import IO, Callable, TypeVar, Protocol, SupportsIndex, Any
810

911
if sys.version_info >= (3, 9):
1012
from os import PathLike as _PathProtocol
1113
else:
12-
_T = TypeVar("_T", bound=Union[str, bytes])
14+
_T = TypeVar("_T", bound="str | bytes", covariant=True)
1315

1416
class _PathProtocol(Protocol[_T]):
1517
def __fspath__(self) -> _T: ...
1618

1719

1820
# For functions that take a file name
19-
PathLike = Union[str, bytes, _PathProtocol[str], _PathProtocol[bytes]]
21+
PathLike = str | bytes | _PathProtocol[str] | _PathProtocol[bytes]
2022
# Most pygame functions that take a file argument should be able to handle a FileLike type
21-
FileLike = Union[PathLike, IO[bytes], IO[str]]
23+
FileLike = PathLike | IO[bytes] | IO[str]
2224

2325
_T_co = TypeVar("_T_co", covariant=True)
2426

@@ -41,19 +43,38 @@ def __len__(self) -> int: ...
4143
IntCoordinate = SequenceLike[int]
4244

4345
# Used for functions that return an RGBA tuple
44-
RGBATuple = Tuple[int, int, int, int]
45-
ColorLike = Union[int, str, SequenceLike[int]]
46+
RGBATuple = tuple[int, int, int, int]
47+
ColorLike = int | str | SequenceLike[int]
4648

47-
_CanBeRect = SequenceLike[Union[float, Coordinate]]
49+
_CanBeRect = SequenceLike[float | Coordinate]
4850

4951

5052
class _HasRectAttribute(Protocol):
5153
# An object that has a rect attribute that is either a rect, or a function
5254
# that returns a rect conforms to the rect protocol
53-
rect: Union["RectLike", Callable[[], "RectLike"]]
55+
rect: "RectLike" | Callable[[], "RectLike"]
56+
57+
58+
RectLike = _CanBeRect | _HasRectAttribute
59+
60+
61+
class EventLike(Protocol):
62+
def __init__(
63+
self, type: int, dict: dict[str, Any] | None = None, **kwargs: Any
64+
) -> None: ...
65+
def __new__(cls, *args: Any, **kwargs: Any) -> "EventLike": ...
66+
def __getattribute__(self, name: str) -> Any: ...
67+
def __setattr__(self, name: str, value: Any) -> None: ...
68+
def __delattr__(self, name: str) -> None: ...
69+
def __int__(self) -> int: ...
70+
def __bool__(self) -> bool: ...
71+
def __eq__(self, other: Any) -> bool: ...
5472

73+
@property
74+
def type(self) -> int: ...
75+
@property
76+
def dict(self) -> dict[str, Any]: ...
5577

56-
RectLike = Union[_CanBeRect, _HasRectAttribute]
5778

5879
# cleanup namespace
59-
del sys, IO, Callable, Tuple, Union, TypeVar, Protocol, SupportsIndex
80+
del sys, IO, Callable, TypeVar, Protocol, SupportsIndex

0 commit comments

Comments
 (0)