Skip to content

Commit 005f27e

Browse files
committed
Even More mypy fixes
1 parent 76a754c commit 005f27e

35 files changed

+626
-220
lines changed

pygame_gui/core/drawable_shapes/drawable_shape.py

+31-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import math
22

33
from collections import deque
4-
from typing import Dict, List, Union, Tuple, Optional
4+
from typing import Dict, List, Union, Tuple, Optional, Deque
55

66
import pygame
77

@@ -247,7 +247,7 @@ def __init__(
247247
self.ui_manager = manager
248248
self.shape_cache = self.ui_manager.get_theme().shape_cache
249249

250-
self.states_to_redraw_queue = deque([])
250+
self.states_to_redraw_queue: Deque[str] = deque([])
251251
self.need_to_clean_up = True
252252

253253
self.should_trigger_full_rebuild = True
@@ -772,29 +772,27 @@ def finalise_text(
772772
flags=pygame.SRCALPHA,
773773
depth=32,
774774
)
775-
self.states[state_str].text_surface.fill("#00000000")
775+
text_surface = self.states[state_str].text_surface
776+
if text_surface is not None:
777+
text_surface.fill("#00000000")
778+
779+
if only_text_changed:
780+
self.text_box_layout.blit_finalised_text_to_surf(text_surface)
781+
else:
782+
self.text_box_layout.set_default_text_colour(
783+
self.theming[text_colour_state_str]
784+
)
785+
self.text_box_layout.set_default_text_shadow_colour(
786+
self.theming[text_shadow_colour_state_str]
787+
)
788+
self.text_box_layout.finalise_to_surf(text_surface)
776789

777-
if only_text_changed:
778-
self.text_box_layout.blit_finalised_text_to_surf(
779-
self.states[state_str].text_surface
780-
)
781-
else:
782-
self.text_box_layout.set_default_text_colour(
783-
self.theming[text_colour_state_str]
784-
)
785-
self.text_box_layout.set_default_text_shadow_colour(
786-
self.theming[text_shadow_colour_state_str]
787-
)
788-
self.text_box_layout.finalise_to_surf(
789-
self.states[state_str].text_surface
790+
basic_blit(
791+
self.states[state_str].surface,
792+
text_surface,
793+
(0, 0),
790794
)
791795

792-
basic_blit(
793-
self.states[state_str].surface,
794-
self.states[state_str].text_surface,
795-
(0, 0),
796-
)
797-
798796
def apply_active_text_changes(self):
799797
"""
800798
Updates the shape surface with any changes to the text surface. Useful when we've made
@@ -832,14 +830,15 @@ def set_text_alpha(self, alpha: int):
832830
833831
:param alpha: the alpha to set.
834832
"""
835-
self.text_box_layout.set_alpha(alpha)
836-
self.redraw_state(self.active_state.state_id, add_text=False)
837-
self.finalise_text(
838-
self.active_state.state_id,
839-
f"{self.active_state.state_id}_text",
840-
f"{self.active_state.state_id}_text_shadow",
841-
only_text_changed=False,
842-
)
833+
if self.text_box_layout is not None:
834+
self.text_box_layout.set_alpha(alpha)
835+
self.redraw_state(self.active_state.state_id, add_text=False)
836+
self.finalise_text(
837+
self.active_state.state_id,
838+
f"{self.active_state.state_id}_text",
839+
f"{self.active_state.state_id}_text_shadow",
840+
only_text_changed=False,
841+
)
843842

844843
def redraw_active_state_no_text(self):
845844
"""
@@ -877,7 +876,8 @@ def insert_text(
877876
+ text
878877
+ self.theming["text"][layout_index:]
879878
)
880-
self.text_box_layout.insert_text(text, layout_index, parser)
879+
if self.text_box_layout is not None:
880+
self.text_box_layout.insert_text(text, layout_index, parser)
881881

882882
def toggle_text_cursor(self):
883883
"""

pygame_gui/core/drawable_shapes/ellipse_drawable_shape.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ def redraw_state(self, state_str: str, add_text: bool = True):
217217
if found_shape is not None:
218218
self.states[state_str].surface = found_shape.copy()
219219
else:
220-
self.states[state_str].surface = self.base_surface.copy()
220+
if self.base_surface is not None:
221+
self.states[state_str].surface = self.base_surface.copy()
221222

222223
# Try one AA call method
223224
aa_amount = 4

pygame_gui/core/drawable_shapes/rect_drawable_shape.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ def redraw_state(self, state_str: str, add_text: bool = True):
222222
if found_shape is not None:
223223
self.states[state_str].surface = found_shape.copy()
224224
else:
225-
self.states[state_str].surface = self.base_surface.copy()
225+
if self.base_surface is not None:
226+
self.states[state_str].surface = self.base_surface.copy()
226227

227228
if self.border_width > 0:
228229
if isinstance(
@@ -308,10 +309,9 @@ def redraw_state(self, state_str: str, add_text: bool = True):
308309
self.theming["filled_bar"], bar_rect
309310
)
310311

311-
if self.states[state_str].cached_background_id is not None:
312-
self.shape_cache.remove_user_from_cache_item(
313-
self.states[state_str].cached_background_id
314-
)
312+
cached_id = self.states[state_str].cached_background_id
313+
if cached_id is not None:
314+
self.shape_cache.remove_user_from_cache_item(cached_id)
315315
if (
316316
not self.has_been_resized
317317
and (

pygame_gui/core/drawable_shapes/rounded_rect_drawable_shape.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,8 @@ def redraw_state(self, state_str: str, add_text: bool = True):
514514

515515
basic_blit(self.states[state_str].surface, bab_surface, (0, 0))
516516

517-
if self.states[state_str].cached_background_id is not None:
518-
cached_id = self.states[state_str].cached_background_id
517+
cached_id = self.states[state_str].cached_background_id
518+
if cached_id is not None:
519519
self.shape_cache.remove_user_from_cache_item(cached_id)
520520
if (
521521
not self.has_been_resized

pygame_gui/core/gui_type_hints.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
RectLike = Union[Rect, FRect, Tuple[float, float, float, float]]
77

88

9-
class SpriteWithHealth(Protocol):
9+
class WithHealth(Protocol):
10+
current_health: int
11+
health_capacity: int
12+
13+
14+
class SpriteWithHealth(WithHealth):
1015
"""
1116
A protocol for sprites that have health. This protocol defines the required attributes for
1217
sprites that have health, including current health, health capacity, rect, and image.

pygame_gui/core/interfaces/appearance_theme_interface.py

+43
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
)
1616
from pygame_gui.core.interfaces.gui_font_interface import IGUIFontInterface
1717
from pygame_gui.core.package_resource import PackageResource
18+
from pygame_gui.core.surface_cache import SurfaceCache
19+
from pygame_gui.core.ui_shadow import ShadowGenerator
1820

1921

2022
class IUIAppearanceThemeInterface(metaclass=ABCMeta):
@@ -190,3 +192,44 @@ def update_single_element_theming(self, element_name: str, new_theming_data: str
190192
:param new_theming_data:
191193
:return:
192194
"""
195+
196+
@property
197+
@abstractmethod
198+
def shape_cache(self) -> SurfaceCache:
199+
"""
200+
201+
:return:
202+
"""
203+
204+
@shape_cache.setter
205+
@abstractmethod
206+
def shape_cache(self, new_cache: SurfaceCache):
207+
"""
208+
209+
:param new_cache:
210+
:return:
211+
"""
212+
213+
@abstractmethod
214+
def check_need_to_rebuild_data_manually_changed(self) -> bool:
215+
"""
216+
Checks and resets a flag for whether we need to trigger a rebuild of all the UI elements after a manual
217+
change in the data.
218+
219+
:return: A boolean that indicates whether we should rebuild or not.
220+
"""
221+
222+
@abstractmethod
223+
def set_locale(self, locale: str):
224+
"""
225+
Set the locale used in the appearance theme.
226+
227+
:param locale: a two-letter ISO country code.
228+
"""
229+
230+
@abstractmethod
231+
def get_shadow_generator(self) -> ShadowGenerator:
232+
"""
233+
234+
:return:
235+
"""

pygame_gui/core/interfaces/container_interface.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def get_thickness(self) -> int:
120120
"""
121121

122122
@abstractmethod
123-
def get_size(self) -> Tuple[int, int]:
123+
def get_size(self) -> Tuple[int, int] | Tuple[float, float]:
124124
"""
125125
Get the container's pixel size.
126126

pygame_gui/core/interfaces/font_dictionary_interface.py

+9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ def get_default_font(self) -> IGUIFontInterface:
5353
5454
"""
5555

56+
@abstractmethod
57+
def get_default_symbol_font(self) -> IGUIFontInterface:
58+
"""
59+
Grab the default symbol font.
60+
61+
:return: The default symbol font.
62+
63+
"""
64+
5665
@abstractmethod
5766
def create_font_id(
5867
self,

pygame_gui/core/interfaces/manager_interface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def get_double_click_time(self) -> float:
3434
"""
3535

3636
@abstractmethod
37-
def get_root_container(self) -> IContainerAndContainerLike:
37+
def get_root_container(self) -> Optional[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.
@@ -248,7 +248,7 @@ def create_tool_tip(
248248
position: Tuple[int, int],
249249
hover_distance: Tuple[int, int],
250250
parent_element: IUIElementInterface,
251-
object_id: ObjectID,
251+
object_id: Optional[ObjectID],
252252
*,
253253
wrap_width: Optional[int] = None,
254254
text_kwargs: Optional[Dict[str, str]] = None,

0 commit comments

Comments
 (0)