Skip to content

Commit 76a754c

Browse files
committed
More mypy fixes
1 parent bcd88cb commit 76a754c

36 files changed

+398
-289
lines changed

pygame_gui/core/colour_parser.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
Set,
3939
Dict,
4040
TypedDict,
41+
Any,
4142
)
4243

4344
import pygame
@@ -478,7 +479,16 @@ def parse_cmy_string(strdata: str) -> pygame.Color:
478479
"""
479480

480481
colour = pygame.Color(0, 0, 0)
481-
colour.cmy = parse_colour_model(strdata, "cmy", _colourModelSchemas["cmy"])
482+
cmy_output: List[Any] = parse_colour_model(
483+
strdata, "cmy", _colourModelSchemas["cmy"]
484+
)
485+
if (
486+
len(cmy_output) == 3
487+
and isinstance(cmy_output[0], float)
488+
and isinstance(cmy_output[1], float)
489+
and isinstance(cmy_output[2], float)
490+
):
491+
colour.cmy = tuple(cmy_output)
482492
return colour
483493

484494

@@ -510,7 +520,7 @@ def parse_hsl_string(strdata: str) -> pygame.Color:
510520
"""
511521

512522
colour = pygame.Color(0, 0, 0)
513-
hsl = parse_colour_model(strdata, "hsl", _colourModelSchemas["hsl"])
523+
hsl: List[float] = parse_colour_model(strdata, "hsl", _colourModelSchemas["hsl"])
514524
colour.hsla = (hsl[0], int(hsl[1] * 100), int(hsl[2] * 100), 100)
515525
return colour
516526

@@ -544,7 +554,7 @@ def parse_hsla_string(strdata: str) -> pygame.Color:
544554
"""
545555

546556
colour = pygame.Color(0, 0, 0)
547-
hsla = parse_colour_model(strdata, "hsla", _colourModelSchemas["hsla"])
557+
hsla: List[float] = parse_colour_model(strdata, "hsla", _colourModelSchemas["hsla"])
548558
colour.hsla = (hsla[0], int(hsla[1] * 100), int(hsla[2] * 100), int(hsla[3] * 100))
549559
return colour
550560

@@ -578,7 +588,7 @@ def parse_hsv_string(strdata: str) -> pygame.Color:
578588
"""
579589

580590
colour = pygame.Color(0, 0, 0)
581-
hsv = parse_colour_model(strdata, "hsv", _colourModelSchemas["hsv"])
591+
hsv: List[float] = parse_colour_model(strdata, "hsv", _colourModelSchemas["hsv"])
582592
colour.hsva = (hsv[0], int(hsv[1] * 100), int(hsv[2] * 100), 100)
583593
return colour
584594

@@ -597,7 +607,7 @@ def parse_hsva_string(strdata: str) -> pygame.Color:
597607
"""
598608

599609
colour = pygame.Color(0, 0, 0)
600-
hsva = parse_colour_model(strdata, "hsva", _colourModelSchemas["hsva"])
610+
hsva: List[float] = parse_colour_model(strdata, "hsva", _colourModelSchemas["hsva"])
601611
colour.hsva = (hsva[0], int(hsva[1] * 100), int(hsva[2] * 100), int(hsva[3] * 100))
602612
return colour
603613

@@ -747,7 +757,7 @@ def get_commas_outside_enclosing_glyphs(strdata: str) -> List[int]:
747757

748758
glyphs: Dict[str, str] = {")": "(", "]": "[", "}": "{"}
749759

750-
opening_stack = []
760+
opening_stack: List[str] = []
751761
comma_indices_outside_parentheses: List[int] = []
752762

753763
for i, ch in enumerate(strdata):

pygame_gui/core/gui_font_freetype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def underline_adjustment(self, value: float):
6969
self.__internal_font.underline_adjustment = value
7070

7171
def get_point_size(self) -> int:
72-
return self.point_size
72+
return int(self.point_size)
7373

7474
def get_rect(self, text: str) -> Rect:
7575
supposed_rect = self.__internal_font.get_rect(text)

pygame_gui/core/gui_font_pygame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ def __init__(
3333
force_style: bool = False,
3434
style: Optional[Dict[str, Any]] = None,
3535
):
36+
self.point_size = int(size)
3637
self.__internal_font: Font = Font(
37-
file, int(size)
38+
file, self.point_size
3839
) # no resolution option for pygame font?
3940

40-
self.__internal_font.set_point_size(int(size))
41+
self.__internal_font.set_point_size(self.point_size)
4142
self.pad = True
4243
self.origin = True
4344
self.__underline = False # pylint: disable=unused-private-member
4445
self.__underline_adjustment = 0.0
4546

46-
self.point_size = size
4747
self.antialiased = True
4848
self.direction = pygame.DIRECTION_LTR
4949

pygame_gui/core/interfaces/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pygame_gui.core.interfaces.element_interface import IUIElementInterface
1111
from pygame_gui.core.interfaces.container_interface import IUIContainerInterface
1212
from pygame_gui.core.interfaces.container_interface import IContainerLikeInterface
13+
from pygame_gui.core.interfaces.container_interface import IContainerAndContainerLike
1314
from pygame_gui.core.interfaces.window_interface import IWindowInterface
1415
from pygame_gui.core.interfaces.window_stack_interface import IUIWindowStackInterface
1516
from pygame_gui.core.interfaces.tool_tip_interface import IUITooltipInterface
@@ -26,6 +27,7 @@
2627
"IUIElementInterface",
2728
"IUIContainerInterface",
2829
"IContainerLikeInterface",
30+
"IContainerAndContainerLike",
2931
"IWindowInterface",
3032
"IUIWindowStackInterface",
3133
"IUITooltipInterface",

pygame_gui/core/interfaces/appearance_theme_interface.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import io
22

33
from abc import ABCMeta, abstractmethod
4-
from typing import List, Union, Dict, Any
4+
from typing import Optional, List, Union, Dict, Any
55
from os import PathLike
66

77

@@ -134,7 +134,7 @@ def get_misc_data(
134134

135135
@abstractmethod
136136
def get_colour(
137-
self, colour_id: str, combined_element_ids: List[str] = None
137+
self, colour_id: str, combined_element_ids: Optional[List[str]] = None
138138
) -> pygame.Color:
139139
"""
140140
Uses data about a UI element and a specific ID to find a colour from our theme.
@@ -147,7 +147,7 @@ def get_colour(
147147

148148
@abstractmethod
149149
def get_colour_or_gradient(
150-
self, colour_id: str, combined_ids: List[str] = None
150+
self, colour_id: str, combined_ids: Optional[List[str]] = None
151151
) -> Union[pygame.Color, IColourGradientInterface]:
152152
"""
153153
Uses data about a UI element and a specific ID to find a colour, or a gradient,

pygame_gui/core/interfaces/container_interface.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class IContainerLikeInterface(metaclass=ABCMeta):
206206
"""
207207

208208
@abstractmethod
209-
def get_container(self) -> IUIContainerInterface:
209+
def get_container(self) -> "IContainerAndContainerLike":
210210
"""
211211
Gets an actual container from this container-like UI element.
212212
"""
@@ -241,9 +241,18 @@ def __contains__(self, item: IUIElementInterface) -> bool:
241241
:return bool: Return True if the element is found, False otherwise.
242242
"""
243243

244+
@abstractmethod
244245
def are_contents_hovered(self) -> bool:
245246
"""
246247
Are any of the elements in the container hovered? Used for handling mousewheel events.
247248
248249
:return: True if one of the elements is hovered, False otherwise.
249250
"""
251+
252+
253+
class IContainerAndContainerLike(
254+
IUIContainerInterface, IContainerLikeInterface, metaclass=ABCMeta
255+
):
256+
"""
257+
Combination of IUIContainerInterface and IContainerLikeInterface mostly for type checking
258+
"""

pygame_gui/core/interfaces/element_interface.py

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABCMeta, abstractmethod
2-
from typing import Union, List, Set, Any, Dict
2+
from typing import Union, List, Set, Dict, Optional
33

44
import pygame
55

@@ -15,6 +15,25 @@ class IUIElementInterface(IGUISpriteInterface, metaclass=ABCMeta):
1515
1616
"""
1717

18+
@property
19+
@abstractmethod
20+
def is_focused(self) -> bool:
21+
"""
22+
Returns True if we are focusing this element.
23+
24+
:return: True if focused.
25+
"""
26+
27+
@is_focused.setter
28+
@abstractmethod
29+
def is_focused(self, value: bool):
30+
"""
31+
Setter for if we are focusing this element.
32+
33+
:param value: the focus value to set.
34+
:return:
35+
"""
36+
1837
@property
1938
@abstractmethod
2039
def hovered(self) -> bool:
@@ -35,7 +54,7 @@ def hovered(self, value: bool):
3554
"""
3655

3756
@abstractmethod
38-
def get_relative_rect(self) -> pygame.Rect:
57+
def get_relative_rect(self) -> pygame.Rect | pygame.FRect:
3958
"""
4059
The relative positioning rect.
4160
@@ -44,7 +63,7 @@ def get_relative_rect(self) -> pygame.Rect:
4463
"""
4564

4665
@abstractmethod
47-
def get_abs_rect(self) -> pygame.Rect:
66+
def get_abs_rect(self) -> pygame.Rect | pygame.FRect:
4867
"""
4968
The absolute positioning rect.
5069
@@ -53,7 +72,7 @@ def get_abs_rect(self) -> pygame.Rect:
5372
"""
5473

5574
@abstractmethod
56-
def get_element_base_ids(self) -> List[str]:
75+
def get_element_base_ids(self) -> List[str | None]:
5776
"""
5877
A list of all the element base IDs in this element's theming/event hierarchy.
5978
@@ -69,23 +88,23 @@ def get_element_ids(self) -> List[str]:
6988
"""
7089

7190
@abstractmethod
72-
def get_class_ids(self) -> List[str]:
91+
def get_class_ids(self) -> List[str | None]:
7392
"""
7493
A list of all the class IDs in this element's theming/event hierarchy.
7594
7695
:return: a list of strings, one for each element in the hierarchy.
7796
"""
7897

7998
@abstractmethod
80-
def get_object_ids(self) -> List[str]:
99+
def get_object_ids(self) -> List[str | None]:
81100
"""
82101
A list of all the object IDs in this element's theming/event hierarchy.
83102
84103
:return: a list of strings, one for each element in the hierarchy.
85104
"""
86105

87106
@abstractmethod
88-
def get_anchors(self) -> Dict[str, Union[str, "IUIElementInterface"]]:
107+
def get_anchors(self) -> Optional[Dict[str, Union[str, "IUIElementInterface"]]]:
89108
"""
90109
A dictionary containing all the anchors defining what the relative rect is relative to
91110
@@ -94,7 +113,7 @@ def get_anchors(self) -> Dict[str, Union[str, "IUIElementInterface"]]:
94113

95114
@abstractmethod
96115
def set_anchors(
97-
self, anchors: Union[Dict[str, Union[str, "IUIElementInterface"]], None]
116+
self, anchors: Optional[Dict[str, Union[str, "IUIElementInterface"]]]
98117
) -> None:
99118
"""
100119
Wraps the setting of the anchors with some validation
@@ -342,13 +361,13 @@ def get_starting_height(self) -> int:
342361
"""
343362

344363
@abstractmethod
345-
def get_focus_set(self) -> Set[Any]:
364+
def get_focus_set(self) -> Optional[Set["IUIElementInterface"]]:
346365
"""
347366
Return the set of elements to focus when we focus this element.
348367
"""
349368

350369
@abstractmethod
351-
def set_focus_set(self, focus_set: Set[Any]):
370+
def set_focus_set(self, focus_set: Optional[Set["IUIElementInterface"]]):
352371
"""
353372
Set the focus set to a specific set of elements.
354373

pygame_gui/core/interfaces/font_dictionary_interface.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from abc import ABCMeta, abstractmethod
2+
from typing import Optional
23
from pygame import DIRECTION_LTR
34
from pygame_gui.core.interfaces.gui_font_interface import IGUIFontInterface
45

@@ -108,9 +109,9 @@ def add_font_path(
108109
self,
109110
font_name: str,
110111
font_path: str,
111-
bold_path: str = None,
112-
italic_path: str = None,
113-
bold_italic_path: str = None,
112+
bold_path: Optional[str] = None,
113+
italic_path: Optional[str] = None,
114+
bold_italic_path: Optional[str] = None,
114115
):
115116
"""
116117
Adds paths to different font files for a font name.

pygame_gui/core/interfaces/manager_interface.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
IUIAppearanceThemeInterface,
88
)
99
from pygame_gui.core.interfaces.element_interface import IUIElementInterface
10-
from pygame_gui.core.interfaces.container_interface import IUIContainerInterface
10+
from pygame_gui.core.interfaces.container_interface import IContainerAndContainerLike
1111
from pygame_gui.core.interfaces.window_stack_interface import IUIWindowStackInterface
1212
from pygame_gui.core.interfaces.tool_tip_interface import IUITooltipInterface
1313
from pygame_gui.core.object_id import ObjectID
@@ -34,7 +34,7 @@ def get_double_click_time(self) -> float:
3434
"""
3535

3636
@abstractmethod
37-
def get_root_container(self) -> IUIContainerInterface:
37+
def get_root_container(self) -> IContainerAndContainerLike:
3838
"""
3939
Returns the 'root' container. The one all UI elements are placed in by default if they are
4040
not placed anywhere else, fills the whole OS/pygame window.
@@ -148,9 +148,9 @@ def add_font_paths(
148148
self,
149149
font_name: str,
150150
regular_path: str,
151-
bold_path: str = None,
152-
italic_path: str = None,
153-
bold_italic_path: str = None,
151+
bold_path: Optional[str] = None,
152+
italic_path: Optional[str] = None,
153+
bold_italic_path: Optional[str] = None,
154154
):
155155
"""
156156
Add file paths for custom fonts you want to use in the UI.
@@ -218,12 +218,7 @@ def print_layer_debug(self):
218218
"""
219219

220220
@abstractmethod
221-
def set_active_cursor(
222-
self,
223-
cursor: Tuple[
224-
Tuple[int, int], Tuple[int, int], Tuple[int, ...], Tuple[int, ...]
225-
],
226-
):
221+
def set_active_cursor(self, cursor: pygame.cursors.Cursor):
227222
"""
228223
This is for users of the library to set the currently active cursor, it will be currently
229224
only be overridden by the resizing cursors.

pygame_gui/core/interfaces/window_interface.py

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def set_display_title(self, new_title: str):
211211
:param new_title: The title to set.
212212
"""
213213

214+
@abstractmethod
214215
def get_layer_thickness(self) -> int:
215216
"""
216217
The layer 'thickness' of this window/

pygame_gui/core/interfaces/window_stack_interface.py

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def is_window_at_top(self, window: IWindowInterface) -> bool:
5757
5858
"""
5959

60+
@abstractmethod
6061
def is_window_at_top_of_top(self, window: IWindowInterface) -> bool:
6162
"""
6263
Checks if a window is at the top of the top window stack or not.

pygame_gui/core/object_id.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
from collections import namedtuple
22

3-
ObjectID = namedtuple(
4-
"ObjectID", ("object_id", "class_id"), defaults=(None, None)
5-
)
3+
ObjectID = namedtuple("ObjectID", ("object_id", "class_id"), defaults=(None, None))

pygame_gui/core/package_resource.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def _create_resource_path(relative_path: Union[str, Path]):
1414

1515
try:
1616
# PyInstaller creates a temp folder and stores path in _MEIPASS
17-
if getattr(sys, 'frozen', False):
17+
if getattr(sys, "frozen", False):
1818
base_path = sys._MEIPASS # type: ignore # pylint: disable=no-member,protected-access
1919
else:
2020
base_path = os.path.abspath(".")

0 commit comments

Comments
 (0)