Skip to content

Commit 8106316

Browse files
committed
Bugfix.
1 parent 8a990c2 commit 8106316

File tree

4 files changed

+106
-88
lines changed

4 files changed

+106
-88
lines changed

docs/reST/ref/event.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ On Android, the following events can be generated
473473

474474
| :sl:`returns related event class to event type`
475475
| :sg:`event_class(type: int, /) -> type[Event]`
476+
476477
Returns an event class that is correlated with the given event type. If the class to a given event type is not found,
477478
but the type is within the range of valid values for the event type, instead of a ``pygame.event.Event`` subclass,
478479
``pygame.event.Event`` itself will be returned, so don't rely on the retuned class having ``type`` attribute equal to a number.
@@ -615,4 +616,8 @@ On Android, the following events can be generated
615616
``WindowDisplayChanged`` ``WINDOWDISPLAYCHANGED``
616617
============================= ============================= ==========================
617618

619+
.. note::
620+
621+
While instantiating these subclasses, don't pass the ``type`` argument to ``{Class name}.__init__()``
622+
618623
.. ## pygame.event ##

src_c/doc/event_doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define DOC_EVENT_GETGRAB "get_grab() -> bool\ntest if the program is sharing input devices"
1515
#define DOC_EVENT_POST "post(event, /) -> bool\nplace a new event on the queue"
1616
#define DOC_EVENT_CUSTOMTYPE "custom_type() -> int\nmake custom user event type"
17+
#define DOC_EVENT_EVENTCLASS "event_class(type: int, /) -> type[Event]\nreturns related event class to event type"
1718
#define DOC_EVENT_EVENT "Event(type, dict) -> Event\nEvent(type, **attributes) -> Event\npygame object for representing events"
1819
#define DOC_EVENT_EVENT_TYPE "type -> int\nevent type identifier."
1920
#define DOC_EVENT_EVENT_DICT "__dict__ -> dict\nevent attribute dictionary"

src_py/event.py

Lines changed: 97 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,22 @@ class UnknownEvent(Event):
7474
return UnknownEvent
7575

7676

77-
def event_class(ev_type: int) -> type[EventLike]:
78-
_check_ev_type(ev_type)
77+
def event_class(type: int) -> type[EventLike]:
78+
_check_ev_type(type)
7979

80-
if ev_type not in _events_map:
81-
_events_map[ev_type] = _unknown_event_factory(ev_type)
82-
return _events_map[ev_type]
80+
if type not in _events_map:
81+
_events_map[type] = _unknown_event_factory(type)
82+
return _events_map[type]
8383

8484

8585
class _EventMeta(type):
86-
def _create_of_type(self, type: int, *args, **kwds):
86+
def _create_of_type(cls, type: int, *args, **kwds):
8787
return event_class(type)(*args, **kwds)
8888

89-
def __call__(self, *args: Any, **kwds: Any) -> Any:
90-
if self is Event:
91-
return self._create_of_type(*args, **kwds)
92-
return super(_EventMeta, self).__call__(*args, **kwds)
89+
def __call__(cls, *args: Any, **kwds: Any) -> Any:
90+
if cls is Event:
91+
return cls._create_of_type(*args, **kwds)
92+
return super(_EventMeta, cls).__call__(*args, **kwds)
9393

9494

9595
class Event(metaclass=_EventMeta):
@@ -141,7 +141,11 @@ def __eq__(self, other: Any):
141141

142142
def __repr__(self):
143143
if getattr(self, "_unknown", False):
144-
return f"<{type(self).__module__}.<dynamic>.{type(self).__name__}({self.type} {self.dict})>"
144+
return (
145+
f"<{type(self).__module__}.<dynamic>."
146+
f"{type(self).__name__}({self.type} {self.dict})>"
147+
)
148+
145149
return f"<{type(self).__module__}.{type(self).__qualname__}({self.dict})>"
146150

147151
@property
@@ -163,9 +167,10 @@ def __getattribute__(self, name: str):
163167
def __setattr__(self, name: str, value: Any):
164168
if name == "type":
165169
raise AttributeError(
166-
f"attribute 'type' of 'Event' or its subclass object is protected"
170+
"attribute 'type' of 'Event' or its subclass object is protected"
167171
)
168-
elif name in ("_dict", "dict"):
172+
173+
if name in ("_dict", "dict"):
169174
super().__setattr__(name, value)
170175
else:
171176
self._dict[name] = value
@@ -446,136 +451,141 @@ def _create_class(type: int, name: str, note: str | None):
446451

447452
# for cls in _events_map.values():
448453
# if cls.__name__ == "UserEvent": continue
449-
# print(f"{cls.__name__} = _create_class(pg.{const_find(cls.type, cls.__name__)}, {cls.__qualname__!r}, {cls.__doc__!r})")
454+
# print(
455+
# f"{cls.__name__} = _create_class(pg.{const_find(cls.type, cls.__name__)}, " \
456+
# f"{cls.__qualname__!r}, {cls.__doc__!r})"
457+
# )
450458

451459

452-
ActiveEvent = _create_class(pg.ACTIVEEVENT, 'ActiveEvent', None)
453-
AppTerminating = _create_class(pg.APP_TERMINATING, 'AppTerminating', None)
454-
AppLowMemory = _create_class(pg.APP_LOWMEMORY, 'AppLowMemory', None)
460+
ActiveEvent = _create_class(pg.ACTIVEEVENT, "ActiveEvent", None)
461+
AppTerminating = _create_class(pg.APP_TERMINATING, "AppTerminating", None)
462+
AppLowMemory = _create_class(pg.APP_LOWMEMORY, "AppLowMemory", None)
455463
AppWillEnterBackground = _create_class(
456-
pg.APP_WILLENTERBACKGROUND, 'AppWillEnterBackground', None
464+
pg.APP_WILLENTERBACKGROUND, "AppWillEnterBackground", None
457465
)
458466
AppDidEnterBackground = _create_class(
459-
pg.APP_DIDENTERBACKGROUND, 'AppDidEnterBackground', None
467+
pg.APP_DIDENTERBACKGROUND, "AppDidEnterBackground", None
460468
)
461469
AppWillEnterForeground = _create_class(
462-
pg.APP_WILLENTERFOREGROUND, 'AppWillEnterForeground', None
470+
pg.APP_WILLENTERFOREGROUND, "AppWillEnterForeground", None
463471
)
464472
AppDidEnterForeground = _create_class(
465-
pg.APP_DIDENTERFOREGROUND, 'AppDidEnterForeground', None
473+
pg.APP_DIDENTERFOREGROUND, "AppDidEnterForeground", None
466474
)
467-
ClipboardUpdate = _create_class(pg.CLIPBOARDUPDATE, 'ClipboardUpdate', None)
468-
KeyDown = _create_class(pg.KEYDOWN, 'KeyDown', None)
469-
KeyUp = _create_class(pg.KEYUP, 'KeyUp', None)
470-
KeyMapChanged = _create_class(pg.KEYMAPCHANGED, 'KeyMapChanged', None)
471-
LocaleChanged = _create_class(pg.LOCALECHANGED, 'LocaleChanged', 'Only for SDL 2.0.14+')
472-
MouseMotion = _create_class(pg.MOUSEMOTION, 'MouseMotion', None)
473-
MouseButtonDown = _create_class(pg.MOUSEBUTTONDOWN, 'MouseButtonDown', None)
474-
MouseButtonUp = _create_class(pg.MOUSEBUTTONUP, 'MouseButtonUp', None)
475+
ClipboardUpdate = _create_class(pg.CLIPBOARDUPDATE, "ClipboardUpdate", None)
476+
KeyDown = _create_class(pg.KEYDOWN, "KeyDown", None)
477+
KeyUp = _create_class(pg.KEYUP, "KeyUp", None)
478+
KeyMapChanged = _create_class(pg.KEYMAPCHANGED, "KeyMapChanged", None)
479+
LocaleChanged = _create_class(pg.LOCALECHANGED, "LocaleChanged", "Only for SDL 2.0.14+")
480+
MouseMotion = _create_class(pg.MOUSEMOTION, "MouseMotion", None)
481+
MouseButtonDown = _create_class(pg.MOUSEBUTTONDOWN, "MouseButtonDown", None)
482+
MouseButtonUp = _create_class(pg.MOUSEBUTTONUP, "MouseButtonUp", None)
475483
JoyAxisMotion = _create_class(
476484
pg.JOYAXISMOTION,
477-
'JoyAxisMotion',
485+
"JoyAxisMotion",
478486
'Attribute "joy" is depracated, use "instance_id".',
479487
)
480488
JoyBallMotion = _create_class(
481489
pg.JOYBALLMOTION,
482-
'JoyBallMotion',
490+
"JoyBallMotion",
483491
'Attribute "joy" is depracated, use "instance_id".',
484492
)
485493
JoyHatMotion = _create_class(
486-
pg.JOYHATMOTION, 'JoyHatMotion', 'Attribute "joy" is depracated, use "instance_id".'
494+
pg.JOYHATMOTION, "JoyHatMotion", 'Attribute "joy" is depracated, use "instance_id".'
487495
)
488496
JoyButtonUp = _create_class(
489-
pg.JOYBUTTONUP, 'JoyButtonUp', 'Attribute "joy" is depracated, use "instance_id".'
497+
pg.JOYBUTTONUP, "JoyButtonUp", 'Attribute "joy" is depracated, use "instance_id".'
490498
)
491499
JoyButtonDown = _create_class(
492500
pg.JOYBUTTONDOWN,
493-
'JoyButtonDown',
501+
"JoyButtonDown",
494502
'Attribute "joy" is depracated, use "instance_id".',
495503
)
496-
Quit = _create_class(pg.QUIT, 'Quit', None)
504+
Quit = _create_class(pg.QUIT, "Quit", None)
497505
SysWMEvent = _create_class(
498506
pg.SYSWMEVENT,
499-
'SysWMEvent',
500-
"\n Attributes are OS-depended:\n hwnd, msg, wparam, lparam - Windows.\n event - Unix / OpenBSD\n For other OSes and in some cases for Unix / OpenBSD\n this event won't have any attributes.\n ",
507+
"SysWMEvent",
508+
"\n Attributes are OS-depended:\n hwnd, msg, wparam, lparam - Windows.\n"
509+
" event - Unix / OpenBSD\n For other OSes and in some cases for Unix / OpenBSD\n"
510+
" this event won't have any attributes.\n ",
501511
)
502-
VideoResize = _create_class(pg.VIDEORESIZE, 'VideoResize', None)
503-
VideoExpose = _create_class(pg.VIDEOEXPOSE, 'VideoExpose', None)
504-
MidiIn = _create_class(pg.MIDIIN, 'MidiIn', None)
505-
MidiOut = _create_class(pg.MIDIOUT, 'MidiOut', None)
506-
NoEvent = _create_class(pg.NOEVENT, 'NoEvent', None)
512+
VideoResize = _create_class(pg.VIDEORESIZE, "VideoResize", None)
513+
VideoExpose = _create_class(pg.VIDEOEXPOSE, "VideoExpose", None)
514+
MidiIn = _create_class(pg.MIDIIN, "MidiIn", None)
515+
MidiOut = _create_class(pg.MIDIOUT, "MidiOut", None)
516+
NoEvent = _create_class(pg.NOEVENT, "NoEvent", None)
507517
FingerMotion = _create_class(
508-
pg.FINGERMOTION, 'FingerMotion', 'Attribute "window" avalible only for SDL 2.0.14+'
518+
pg.FINGERMOTION, "FingerMotion", 'Attribute "window" avalible only for SDL 2.0.14+'
509519
)
510520
FingerDown = _create_class(
511-
pg.FINGERDOWN, 'FingerDown', 'Attribute "window" avalible only for SDL 2.0.14+'
521+
pg.FINGERDOWN, "FingerDown", 'Attribute "window" avalible only for SDL 2.0.14+'
512522
)
513523
FingerUp = _create_class(
514-
pg.FINGERUP, 'FingerUp', 'Attribute "window" avalible only for SDL 2.0.14+'
524+
pg.FINGERUP, "FingerUp", 'Attribute "window" avalible only for SDL 2.0.14+'
515525
)
516-
MultiGesture = _create_class(pg.MULTIGESTURE, 'MultiGesture', None)
517-
MouseWheel = _create_class(pg.MOUSEWHEEL, 'MouseWheel', None)
518-
TextInput = _create_class(pg.TEXTINPUT, 'TextInput', None)
519-
TextEditing = _create_class(pg.TEXTEDITING, 'TextEditing', None)
520-
DropFile = _create_class(pg.DROPFILE, 'DropFile', None)
521-
DropText = _create_class(pg.DROPTEXT, 'DropText', None)
522-
DropBegin = _create_class(pg.DROPBEGIN, 'DropBegin', None)
523-
DropComplete = _create_class(pg.DROPCOMPLETE, 'DropComplete', None)
526+
MultiGesture = _create_class(pg.MULTIGESTURE, "MultiGesture", None)
527+
MouseWheel = _create_class(pg.MOUSEWHEEL, "MouseWheel", None)
528+
TextInput = _create_class(pg.TEXTINPUT, "TextInput", None)
529+
TextEditing = _create_class(pg.TEXTEDITING, "TextEditing", None)
530+
DropFile = _create_class(pg.DROPFILE, "DropFile", None)
531+
DropText = _create_class(pg.DROPTEXT, "DropText", None)
532+
DropBegin = _create_class(pg.DROPBEGIN, "DropBegin", None)
533+
DropComplete = _create_class(pg.DROPCOMPLETE, "DropComplete", None)
524534
ControllerAxisMotion = _create_class(
525-
pg.CONTROLLERAXISMOTION, 'ControllerAxisMotion', None
535+
pg.CONTROLLERAXISMOTION, "ControllerAxisMotion", None
526536
)
527537
ControllerButtonDown = _create_class(
528-
pg.CONTROLLERBUTTONDOWN, 'ControllerButtonDown', None
538+
pg.CONTROLLERBUTTONDOWN, "ControllerButtonDown", None
529539
)
530-
ControllerButtonUp = _create_class(pg.CONTROLLERBUTTONUP, 'ControllerButtonUp', None)
540+
ControllerButtonUp = _create_class(pg.CONTROLLERBUTTONUP, "ControllerButtonUp", None)
531541
ControllerDeviceAdded = _create_class(
532-
pg.CONTROLLERDEVICEADDED, 'ControllerDeviceAdded', None
542+
pg.CONTROLLERDEVICEADDED, "ControllerDeviceAdded", None
533543
)
534544
ControllerDeviceRemoved = _create_class(
535-
pg.CONTROLLERDEVICEREMOVED, 'ControllerDeviceRemoved', None
545+
pg.CONTROLLERDEVICEREMOVED, "ControllerDeviceRemoved", None
536546
)
537547
ControllerDeviceMapped = _create_class(
538-
pg.CONTROLLERDEVICEREMAPPED, 'ControllerDeviceMapped', None
548+
pg.CONTROLLERDEVICEREMAPPED, "ControllerDeviceMapped", None
539549
)
540-
JoyDeviceAdded = _create_class(pg.JOYDEVICEADDED, 'JoyDeviceAdded', None)
541-
JoyDeviceRemoved = _create_class(pg.JOYDEVICEREMOVED, 'JoyDeviceRemoved', None)
550+
JoyDeviceAdded = _create_class(pg.JOYDEVICEADDED, "JoyDeviceAdded", None)
551+
JoyDeviceRemoved = _create_class(pg.JOYDEVICEREMOVED, "JoyDeviceRemoved", None)
542552
ControllerTouchpadDown = _create_class(
543-
pg.CONTROLLERTOUCHPADDOWN, 'ControllerTouchpadDown', 'Only for SDL 2.0.14+'
553+
pg.CONTROLLERTOUCHPADDOWN, "ControllerTouchpadDown", "Only for SDL 2.0.14+"
544554
)
545555
ControllerTouchpadMotion = _create_class(
546-
pg.CONTROLLERTOUCHPADMOTION, 'ControllerTouchpadMotion', 'Only for SDL 2.0.14+'
556+
pg.CONTROLLERTOUCHPADMOTION, "ControllerTouchpadMotion", "Only for SDL 2.0.14+"
547557
)
548558
ControllerTouchpadUp = _create_class(
549-
pg.CONTROLLERTOUCHPADUP, 'ControllerTouchpadUp', 'Only for SDL 2.0.14+'
559+
pg.CONTROLLERTOUCHPADUP, "ControllerTouchpadUp", "Only for SDL 2.0.14+"
550560
)
551561
ControllerSensorUpdate = _create_class(
552-
pg.CONTROLLERSENSORUPDATE, 'ControllerSensorUpdate', 'Only for SDL 2.0.14+'
562+
pg.CONTROLLERSENSORUPDATE, "ControllerSensorUpdate", "Only for SDL 2.0.14+"
553563
)
554-
AudioDeviceAdded = _create_class(pg.AUDIODEVICEADDED, 'AudioDeviceAdded', None)
555-
AudioDeviceRemoved = _create_class(pg.AUDIODEVICEREMOVED, 'AudioDeviceRemoved', None)
556-
RenderTargetsReset = _create_class(pg.RENDER_TARGETS_RESET, 'RenderTargetsReset', None)
557-
RenderDeviceReset = _create_class(pg.RENDER_DEVICE_RESET, 'RenderDeviceReset', None)
558-
WindowShown = _create_class(pg.WINDOWSHOWN, 'WindowShown', None)
559-
WindowHidden = _create_class(pg.WINDOWHIDDEN, 'WindowHidden', None)
560-
WindowExposed = _create_class(pg.WINDOWEXPOSED, 'WindowExposed', None)
561-
WindowMoved = _create_class(pg.WINDOWMOVED, 'WindowMoved', None)
562-
WindowResized = _create_class(pg.WINDOWRESIZED, 'WindowResized', None)
563-
WindowSizeChanged = _create_class(pg.WINDOWSIZECHANGED, 'WindowSizeChanged', None)
564-
WindowMinimized = _create_class(pg.WINDOWMINIMIZED, 'WindowMinimized', None)
565-
WindowMaximized = _create_class(pg.WINDOWMAXIMIZED, 'WindowMaximized', None)
566-
WindowRestored = _create_class(pg.WINDOWRESTORED, 'WindowRestored', None)
567-
WindowEnter = _create_class(pg.WINDOWENTER, 'WindowEnter', None)
568-
WindowLeave = _create_class(pg.WINDOWLEAVE, 'WindowLeave', None)
569-
WindowFocusGained = _create_class(pg.WINDOWFOCUSGAINED, 'WindowFocusGained', None)
570-
WindowFocusLost = _create_class(pg.WINDOWFOCUSLOST, 'WindowFocusLost', None)
571-
WindowClose = _create_class(pg.WINDOWCLOSE, 'WindowClose', None)
572-
WindowTakeFocus = _create_class(pg.WINDOWTAKEFOCUS, 'WindowTakeFocus', None)
573-
WindowHitTest = _create_class(pg.WINDOWHITTEST, 'WindowHitTest', None)
564+
AudioDeviceAdded = _create_class(pg.AUDIODEVICEADDED, "AudioDeviceAdded", None)
565+
AudioDeviceRemoved = _create_class(pg.AUDIODEVICEREMOVED, "AudioDeviceRemoved", None)
566+
RenderTargetsReset = _create_class(pg.RENDER_TARGETS_RESET, "RenderTargetsReset", None)
567+
RenderDeviceReset = _create_class(pg.RENDER_DEVICE_RESET, "RenderDeviceReset", None)
568+
WindowShown = _create_class(pg.WINDOWSHOWN, "WindowShown", None)
569+
WindowHidden = _create_class(pg.WINDOWHIDDEN, "WindowHidden", None)
570+
WindowExposed = _create_class(pg.WINDOWEXPOSED, "WindowExposed", None)
571+
WindowMoved = _create_class(pg.WINDOWMOVED, "WindowMoved", None)
572+
WindowResized = _create_class(pg.WINDOWRESIZED, "WindowResized", None)
573+
WindowSizeChanged = _create_class(pg.WINDOWSIZECHANGED, "WindowSizeChanged", None)
574+
WindowMinimized = _create_class(pg.WINDOWMINIMIZED, "WindowMinimized", None)
575+
WindowMaximized = _create_class(pg.WINDOWMAXIMIZED, "WindowMaximized", None)
576+
WindowRestored = _create_class(pg.WINDOWRESTORED, "WindowRestored", None)
577+
WindowEnter = _create_class(pg.WINDOWENTER, "WindowEnter", None)
578+
WindowLeave = _create_class(pg.WINDOWLEAVE, "WindowLeave", None)
579+
WindowFocusGained = _create_class(pg.WINDOWFOCUSGAINED, "WindowFocusGained", None)
580+
WindowFocusLost = _create_class(pg.WINDOWFOCUSLOST, "WindowFocusLost", None)
581+
WindowClose = _create_class(pg.WINDOWCLOSE, "WindowClose", None)
582+
WindowTakeFocus = _create_class(pg.WINDOWTAKEFOCUS, "WindowTakeFocus", None)
583+
WindowHitTest = _create_class(pg.WINDOWHITTEST, "WindowHitTest", None)
574584
WindowICCProfChanged = _create_class(
575-
pg.WINDOWICCPROFCHANGED, 'WindowICCProfChanged', None
585+
pg.WINDOWICCPROFCHANGED, "WindowICCProfChanged", None
576586
)
577587
WindowDisplayChanged = _create_class(
578-
pg.WINDOWDISPLAYCHANGED, 'WindowDisplayChanged', None
588+
pg.WINDOWDISPLAYCHANGED, "WindowDisplayChanged", None
579589
)
580590

581591

src_py/typing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def rect(self) -> Union["RectLike", Callable[[], "RectLike"]]: ...
8181
class EventLike(Protocol):
8282
type: int
8383

84-
def __init__(self, dict: dict[str, Any] | None, **kwargs: Any) -> None: ...
84+
def __init__(
85+
self, dict: Optional[Dict[str, Any]] = None, **kwargs: Any
86+
) -> None: ...
8587

8688
@property
8789
def dict(self) -> Dict[str, Any]: ...

0 commit comments

Comments
 (0)