Skip to content

Commit ec4ed5d

Browse files
authored
Merge pull request #523 from JaskRendix/various
Fix CodeQL findings: empty except blocks, method signature mismatch, unused globals
2 parents afa2c6b + b9ef5b4 commit ec4ed5d

13 files changed

Lines changed: 53 additions & 45 deletions

File tree

docs/add_widgets.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def func(name):
118118
elif EXAMPLE == "CLOCK":
119119
menu = make_menu(pygame_menu.themes.THEME_DARK, "Clock")
120120

121-
clock = menu.add.clock(font_size=25, font_name=pygame_menu.font.FONT_DIGITAL)
121+
menu.add.clock(font_size=25, font_name=pygame_menu.font.FONT_DIGITAL)
122122

123123
elif EXAMPLE == "COLORINPUT":
124124
menu = make_menu(pygame_menu.themes.THEME_DARK, "Color Entry")
@@ -145,13 +145,13 @@ def func(name):
145145
elif EXAMPLE == "DROPSELECT":
146146
menu = make_menu(pygame_menu.themes.THEME_DEFAULT, "Drop Select")
147147

148-
selector_epic = menu.add.dropselect(
148+
menu.add.dropselect(
149149
title="Is pygame-menu epic?",
150150
items=[("Yes", 0), ("Absolutely Yes", 1)],
151151
font_size=16,
152152
selection_option_font_size=20,
153153
)
154-
selector_sum = menu.add.dropselect(
154+
menu.add.dropselect(
155155
title="What is the value of π?",
156156
items=[
157157
("3 (Engineer)", 0),
@@ -164,7 +164,7 @@ def func(name):
164164
selection_option_padding=(0, 5),
165165
selection_option_font_size=20,
166166
)
167-
selector_country = menu.add.dropselect(
167+
menu.add.dropselect(
168168
title="Pick a country",
169169
items=[
170170
("Argentina", "ar"),
@@ -196,7 +196,7 @@ def func(name):
196196
)
197197
menu.add.vertical_margin(75)
198198

199-
selector = menu.add.dropselect_multiple(
199+
menu.add.dropselect_multiple(
200200
title="Pick 3 colors",
201201
items=[
202202
("Black", (0, 0, 0)),
@@ -382,7 +382,7 @@ def open_link(*args) -> None:
382382
onchange=change_background_color, # User changes value with left/right keys
383383
)
384384
selector.add_self_to_kwargs() # Callbacks will receive widget as parameter
385-
selector2 = menu.add.selector(
385+
menu.add.selector(
386386
title="New color:", items=items, style=pygame_menu.widgets.SELECTOR_STYLE_FANCY
387387
)
388388

pygame_menu/_decorator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ def _update_pos_list(
11521152
try:
11531153
decoid_exists = self._coord_cache[decoid] is not None
11541154
except KeyError:
1155+
# No cached coordinates for this decoid; proceed as if unseen
11551156
pass
11561157
if (
11571158
decoid_exists

pygame_menu/widgets/core/widget.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ class Widget(Base):
280280
:param kwargs: Optional keyword arguments
281281
"""
282282

283-
_accept_events: bool
284283
_alignment: str
285284
_angle: NumberType
286285
_args: list[Any]
@@ -361,12 +360,9 @@ class Widget(Base):
361360
_update_callbacks: dict[
362361
str, Callable[[EventListType, Widget, pygame_menu.Menu], Any]
363362
]
364-
_visible: bool
365-
active: bool
366363
configured: bool
367364
force_menu_draw_focus: bool
368365
is_scrollable: bool
369-
is_selectable: bool
370366
last_surface: pygame.Surface | None
371367
lock_position: bool
372368
readonly: bool
@@ -383,6 +379,9 @@ def __init__(
383379
onselect: Callable[[bool, Widget, pygame_menu.Menu], Any] | None = None,
384380
args=None,
385381
kwargs=None,
382+
*,
383+
selectable: bool = True,
384+
visible: bool = True,
386385
) -> None:
387386
super().__init__(object_id=widget_id)
388387

@@ -413,7 +412,7 @@ def __init__(
413412
self._sound = Sound()
414413
self._tab_size = 0 # Tab spaces
415414
self._title = str(title)
416-
self._visible = True # Use show() or hide() to modify this status
415+
self._visible = visible # Use show() or hide() to modify this status
417416

418417
# If True, the widget don't contribute width/height to the Menu widget
419418
# positioning computation. Use .set_float() to modify this status
@@ -525,7 +524,7 @@ def __init__(
525524
self.configured = False # Widget has been configured
526525
self.force_menu_draw_focus = False # If True Menu draw focus if widget is selected, don't consider the previous requisites
527526
self.is_scrollable = False # Some widgets can be scrolled, such as the Frame
528-
self.is_selectable = True # Some widgets cannot be selected like labels
527+
self.is_selectable = selectable # Some widgets cannot be selected like labels
529528
self.last_surface = None # Stores the last surface the widget has been drawn
530529
self.lock_position = False # If True, the widget don't update the position if .set_position() is executed
531530
self.readonly = False # If True, widget ignores all input

pygame_menu/widgets/widget/colorinput.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def value_changed(self) -> bool:
360360
default = "#" + default
361361
return self.get_value(as_string=True) != default
362362

363-
def get_value(self, as_string: bool = False) -> str | Tuple3IntType:
363+
def get_value(self, as_string: bool = False) -> str | float | int | Tuple3IntType:
364364
"""
365365
Return the color value as a tuple or red blue and green channels.
366366
@@ -371,7 +371,6 @@ def get_value(self, as_string: bool = False) -> str | Tuple3IntType:
371371
:param as_string: If ``True`` returns the widget value as plain text
372372
:return: Color tuple as (R, G, B) or color string
373373
"""
374-
assert isinstance(as_string, bool)
375374
if as_string:
376375
return self._input_string
377376

pygame_menu/widgets/widget/dropselect.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def __init__(
223223
selection_option_font: FontType | None = None,
224224
selection_option_font_color: ColorInputType = (0, 0, 0),
225225
selection_option_font_size: int | None = None,
226+
selection_option_left_space: bool = False,
226227
selection_option_left_space_height_factor: float = 1.0,
227228
selection_option_padding: PaddingType = 5,
228229
selection_option_selected_bgcolor: ColorInputType = (188, 227, 244),
@@ -328,7 +329,7 @@ def __init__(
328329

329330
# If True adds a space equals to the height of the option at left, used for
330331
# drawing some options (for example, ticks, boxes, etc.)
331-
self._selection_option_left_space = False
332+
self._selection_option_left_space = selection_option_left_space
332333
self._selection_option_left_space_height_factor = (
333334
selection_option_left_space_height_factor
334335
)
@@ -1089,7 +1090,7 @@ def _up(self) -> None:
10891090
self._sound.play_key_add()
10901091
return None
10911092

1092-
def set_value(self, item: str | int) -> None:
1093+
def set_value(self, item: str | int, process_index: bool = False) -> None:
10931094
"""
10941095
Set the current value of the widget, selecting the item that matches the
10951096
text if ``item`` is a string, or the index if ``item`` is an integer.
@@ -1104,10 +1105,9 @@ def set_value(self, item: str | int) -> None:
11041105
11051106
This method does not trigger any event (change).
11061107
1107-
:param item: Item to select, can be a string or an integer
1108+
:param item: The item to select, either a string or an integer index.
1109+
:param process_index: Ignored in this class; used by subclasses.
11081110
"""
1109-
assert isinstance(item, (str, int)), "item must be a string or an integer"
1110-
11111111
if isinstance(item, str):
11121112
found = False
11131113
for i in self._items:
@@ -1117,13 +1117,17 @@ def set_value(self, item: str | int) -> None:
11171117
break
11181118
if not found:
11191119
raise ValueError(f'no value "{item}" found in drop select')
1120+
11201121
elif isinstance(item, int):
11211122
assert -1 <= item < len(self._items), (
11221123
"item index must be greater than zero and lower than the number "
11231124
"of items on the drop select"
11241125
)
11251126
self._index = item
11261127

1128+
# Base class ignores process_index
1129+
# (DropSelectMultiple overrides and uses it)
1130+
11271131
# Update options background selection
11281132
for b_ind_x in range(len(self._option_buttons)):
11291133
btn = self._option_buttons[b_ind_x]

pygame_menu/widgets/widget/dropselect_multiple.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ def __init__(
236236
selection_option_font=selection_option_font,
237237
selection_option_font_color=selection_option_font_color,
238238
selection_option_font_size=selection_option_font_size,
239+
selection_option_left_space=True,
239240
selection_option_left_space_height_factor=selection_option_selected_box_height,
240241
selection_option_padding=selection_option_padding,
241242
selection_option_selected_bgcolor=selection_option_selected_bgcolor,
@@ -262,7 +263,6 @@ def __init__(
262263
self._args = args or []
263264
self._close_on_apply = False
264265
self._max_selected = max_selected
265-
self._selection_option_left_space = True
266266
self._selection_option_left_space_margin = selection_option_selected_box_margin
267267

268268
# Set style
@@ -490,8 +490,6 @@ def set_value(self, item: str | int, process_index: bool = False) -> None:
490490
:param item: Item to select, can be a string or an integer
491491
:param process_index: Adds/Removes the index from the selected indices list
492492
"""
493-
assert isinstance(item, (str, int)), "item must be a string or an integer"
494-
495493
if isinstance(item, str):
496494
found = False
497495
for i in self._items:

pygame_menu/widgets/widget/frame.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def __init__(
161161
orientation: str,
162162
frame_id: str = "",
163163
) -> None:
164-
super().__init__(widget_id=frame_id)
164+
super().__init__(widget_id=frame_id, selectable=False)
165165
assert isinstance(width, NumberInstance)
166166
assert isinstance(height, NumberInstance)
167167
assert width > 0, f"width must be greater than zero ({width} received)"
@@ -201,7 +201,6 @@ def __init__(
201201
self.first_index = -1
202202
self.horizontal = orientation == ORIENTATION_HORIZONTAL
203203
self.is_scrollable = False
204-
self.is_selectable = False
205204
self.last_index = -1
206205

207206
def set_title(

pygame_menu/widgets/widget/menubar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def __init__(
135135
onreturn=onreturn,
136136
title=title,
137137
widget_id=menubar_id,
138+
selectable=False,
138139
)
139140

140141
self._backbox = back_box
@@ -157,7 +158,6 @@ def __init__(
157158
self.set_title(title, offsetx, offsety)
158159

159160
# Public's
160-
self.is_selectable = False
161161
self.fixed = True
162162

163163
def _apply_font(self) -> None:

pygame_menu/widgets/widget/menulink.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,12 @@ class MenuLink(NoneWidget):
4141
def __init__(
4242
self, menu: pygame_menu.Menu, menu_opener_handler: Callable, link_id: str = ""
4343
) -> None:
44-
assert isinstance(menu, pygame_menu.Menu)
4544
assert callable(menu_opener_handler), (
4645
"menu opener handler must be callable (a function)"
4746
)
48-
super().__init__(widget_id=link_id)
47+
super().__init__(widget_id=link_id, visible=False)
4948
self.menu = menu
5049
self._onreturn = menu_opener_handler
51-
self._visible = False
52-
self.is_selectable = False
5350

5451
def hide(self) -> MenuLink:
5552
pass

pygame_menu/widgets/widget/none.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ class NoneWidget(Widget):
4141
:param widget_id: ID of the widget
4242
"""
4343

44-
def __init__(self, widget_id: str = "") -> None:
45-
super().__init__(widget_id=widget_id)
46-
self.is_selectable = False
44+
def __init__(self, widget_id: str = "", *, visible: bool = True) -> None:
45+
super().__init__(widget_id=widget_id, selectable=False, visible=visible)
4746
self._surface = make_surface(0, 0, alpha=True)
4847

4948
def _apply_font(self) -> None:

0 commit comments

Comments
 (0)