|
23 | 23 |
|
24 | 24 | import enum |
25 | 25 | import warnings |
26 | | -from typing import Any, Callable, Dict, Generic, Iterator, Mapping, NamedTuple, Optional, Tuple, TypeVar |
| 26 | +from typing import Any, Callable, Dict, Generic, Iterator, Mapping, NamedTuple, Optional, Tuple, TypeVar, Union |
27 | 27 |
|
28 | 28 | import numpy as np |
29 | 29 | from numpy.typing import NDArray |
| 30 | +from typing_extensions import Final, Literal |
30 | 31 |
|
31 | 32 | import tcod.event_constants |
32 | 33 | from tcod.event_constants import * # noqa: F4 |
@@ -225,7 +226,7 @@ class Event: |
225 | 226 | def __init__(self, type: Optional[str] = None): |
226 | 227 | if type is None: |
227 | 228 | type = self.__class__.__name__.upper() |
228 | | - self.type = type |
| 229 | + self.type: Final = type |
229 | 230 | self.sdl_event = None |
230 | 231 |
|
231 | 232 | @classmethod |
@@ -606,12 +607,33 @@ class WindowEvent(Event): |
606 | 607 | type (str): A window event could mean various event types. |
607 | 608 | """ |
608 | 609 |
|
| 610 | + type: Final[ # type: ignore[misc] # Narrowing contant type. |
| 611 | + Literal[ |
| 612 | + "WindowShown", |
| 613 | + "WindowHidden", |
| 614 | + "WindowExposed", |
| 615 | + "WindowMoved", |
| 616 | + "WindowResized", |
| 617 | + "WindowSizeChanged", |
| 618 | + "WindowMinimized", |
| 619 | + "WindowMaximized", |
| 620 | + "WindowRestored", |
| 621 | + "WindowEnter", |
| 622 | + "WindowLeave", |
| 623 | + "WindowFocusGained", |
| 624 | + "WindowFocusLost", |
| 625 | + "WindowClose", |
| 626 | + "WindowTakeFocus", |
| 627 | + "WindowHitTest", |
| 628 | + ] |
| 629 | + ] |
| 630 | + |
609 | 631 | @classmethod |
610 | | - def from_sdl_event(cls, sdl_event: Any) -> Any: |
| 632 | + def from_sdl_event(cls, sdl_event: Any) -> Union[WindowEvent, Undefined]: |
611 | 633 | if sdl_event.window.event not in cls.__WINDOW_TYPES: |
612 | 634 | return Undefined.from_sdl_event(sdl_event) |
613 | | - event_type = cls.__WINDOW_TYPES[sdl_event.window.event].upper() |
614 | | - self = None # type: Any |
| 635 | + event_type: Final = cls.__WINDOW_TYPES[sdl_event.window.event].upper() |
| 636 | + self: WindowEvent |
615 | 637 | if sdl_event.window.event == lib.SDL_WINDOWEVENT_MOVED: |
616 | 638 | self = WindowMoved(sdl_event.window.data1, sdl_event.window.data2) |
617 | 639 | elif sdl_event.window.event in ( |
@@ -655,6 +677,8 @@ class WindowMoved(WindowEvent): |
655 | 677 | y (int): Movement on the y-axis. |
656 | 678 | """ |
657 | 679 |
|
| 680 | + type: Literal["WINDOWMOVED"] # type: ignore[assignment,misc] |
| 681 | + |
658 | 682 | def __init__(self, x: int, y: int) -> None: |
659 | 683 | super().__init__(None) |
660 | 684 | self.x = x |
@@ -684,6 +708,8 @@ class WindowResized(WindowEvent): |
684 | 708 | height (int): The current height of the window. |
685 | 709 | """ |
686 | 710 |
|
| 711 | + type: Literal["WINDOWRESIZED", "WINDOWSIZECHANGED"] # type: ignore[assignment,misc] |
| 712 | + |
687 | 713 | def __init__(self, type: str, width: int, height: int) -> None: |
688 | 714 | super().__init__(type) |
689 | 715 | self.width = width |
|
0 commit comments