Skip to content

Commit ec3390f

Browse files
authored
Merge pull request #526 from JaskRendix/fix-code-ql
Fix CodeQL findings and some typehints
2 parents a98874c + d863a59 commit ec3390f

8 files changed

Lines changed: 53 additions & 47 deletions

File tree

pygame_menu/examples/other/calculator.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def __init__(self) -> None:
168168
w_deco.disable(on_layer)
169169
widget.set_attribute("on_layer", on_layer)
170170

171-
def widget_select(sel: bool, wid: pygame_menu.widgets.Widget, _):
171+
def widget_select(sel: bool, wid: pygame_menu.widgets.Widget, _) -> None:
172172
"""
173173
Function triggered if widget is selected
174174
"""
@@ -237,9 +237,9 @@ def _operate(self) -> int | float:
237237
238238
:return: Operation result
239239
"""
240-
a = 0 if self.curr == "" else float(self.curr)
241-
b = 0 if self.prev == "" else float(self.prev)
242-
c = 0
240+
a = 0.0 if self.curr == "" else float(self.curr)
241+
b = 0.0 if self.prev == "" else float(self.prev)
242+
c = 0.0
243243
if self.op == "+":
244244
c = a + b
245245
elif self.op == "-":
@@ -259,19 +259,24 @@ def _press(self, digit: int | str) -> None:
259259
260260
:param digit: Number or symbol
261261
"""
262-
if digit in ("+", "-", "x", "/"):
262+
if isinstance(digit, int):
263+
digit_str = str(digit)
264+
else:
265+
digit_str = digit
266+
267+
if digit_str in ("+", "-", "x", "/"):
263268
if self.curr != "":
264269
if self.op != "":
265270
self.prev = str(self._operate())
266271
else:
267272
self.prev = self.curr
268273
self.curr = ""
269-
self.op = digit
274+
self.op = digit_str
270275
if len(self.prev) <= 8:
271276
self.screen.set_title(self.prev + self.op)
272277
else:
273278
self.screen.set_title("Ans" + self.op)
274-
elif digit == "=":
279+
elif digit_str == "=":
275280
if self.prev == "":
276281
self.curr = ""
277282
self.screen.set_title("0")
@@ -287,12 +292,12 @@ def _press(self, digit: int | str) -> None:
287292
else:
288293
if self.op == "":
289294
if len(self.prev) <= 7:
290-
self.prev += str(digit)
295+
self.prev += digit_str
291296
self.prev = self._format(self.prev)
292297
self.screen.set_title(self.prev)
293298
else:
294299
if len(self.curr) <= 7:
295-
self.curr += str(digit)
300+
self.curr += digit_str
296301
self.curr = self._format(self.curr)
297302
self.screen.set_title(self.curr)
298303

pygame_menu/examples/other/maze.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ class AStarQueue:
4747
A* Queue.
4848
"""
4949

50-
def __init__(self):
50+
def __init__(self) -> None:
5151
self.myheap = []
5252

5353
def show(self):
5454
return self.myheap
5555

56-
def push(self, priority, distance, node):
56+
def push(self, priority, distance, node) -> None:
5757
heapq.heappush(self.myheap, (priority, distance, node))
5858

5959
def pop(self):
@@ -67,13 +67,13 @@ class PriorityQueue:
6767
Priority Queue.
6868
"""
6969

70-
def __init__(self):
70+
def __init__(self) -> None:
7171
self.myheap = []
7272

7373
def show(self):
7474
return self.myheap
7575

76-
def push(self, priority, node):
76+
def push(self, priority, node) -> None:
7777
heapq.heappush(self.myheap, (priority, node))
7878

7979
def pop(self):
@@ -87,14 +87,14 @@ class PrioritySet:
8787
Create a priority queue that doesn't add duplicate nodes.
8888
"""
8989

90-
def __init__(self):
90+
def __init__(self) -> None:
9191
self.myheap = []
9292
self.myset = set()
9393

9494
def show(self):
9595
return self.myheap
9696

97-
def push(self, priority, node):
97+
def push(self, priority, node) -> None:
9898
if node not in self.myset:
9999
heapq.heappush(self.myheap, (priority, node))
100100
self.myset.add(node)
@@ -1074,7 +1074,7 @@ def _clear_visited(self) -> None:
10741074
self._grid[row][column].update(is_visited=False, is_path=False)
10751075
self._update_gui()
10761076

1077-
def _update_path(self) -> bool | _MazeType:
1077+
def _update_path(self) -> bool:
10781078
"""
10791079
Updates the path.
10801080
"""
@@ -1403,11 +1403,14 @@ def _xfs(
14031403
# Trace back to start using path_dict
14041404
path_node = goal_node
14051405
while True:
1406-
path_node = path_dict[path_node]
1407-
mazearray[path_node[0]][path_node[1]].update(is_path=True) # type: ignore
1408-
self._draw_square(mazearray, path_node[0], path_node[1]) # type: ignore
1406+
parent = path_dict[path_node]
1407+
if parent is None:
1408+
break
1409+
path_node = parent
1410+
mazearray[path_node[0]][path_node[1]].update(is_path=True)
1411+
self._draw_square(mazearray, path_node[0], path_node[1])
14091412
if self._visualize:
1410-
self._update_square(path_node[0], path_node[1]) # type: ignore
1413+
self._update_square(path_node[0], path_node[1])
14111414
if path_node == start_point:
14121415
return True
14131416

pygame_menu/widgets/widget/colorinput.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,11 @@ def __init__(
172172
), 'invalid hex format mode, it must be "none", "lower" or "upper"'
173173

174174
maxchar: int = 0
175+
valid_chars: list[str] | None = None
175176
self._color_type = color_type.lower()
176177
if self._color_type == COLORINPUT_TYPE_RGB:
177178
maxchar = 11 # RRR,GGG,BBB
178-
self._valid_chars = [
179+
valid_chars = [
179180
"0",
180181
"1",
181182
"2",
@@ -190,7 +191,7 @@ def __init__(
190191
]
191192
elif self._color_type == COLORINPUT_TYPE_HEX:
192193
maxchar = 7 # #XXYYZZ
193-
self._valid_chars = [
194+
valid_chars = [
194195
"a",
195196
"A",
196197
"b",
@@ -218,6 +219,8 @@ def __init__(
218219

219220
# noinspection PyArgumentEqualDefault
220221
super().__init__(
222+
alt_x_enabled=False,
223+
apply_widget_update_callback=False,
221224
copy_paste_enable=False,
222225
cursor_color=cursor_color,
223226
cursor_switch_ms=cursor_switch_ms,
@@ -238,7 +241,7 @@ def __init__(
238241
text_ellipsis="",
239242
textinput_id=colorinput_id,
240243
title=title,
241-
valid_chars=self._valid_chars,
244+
valid_chars=valid_chars,
242245
*args,
243246
**kwargs,
244247
)
@@ -257,12 +260,6 @@ def __init__(
257260
self._prev_width_factor = prev_width_factor
258261
self._previsualization_surface = None
259262

260-
# Disable parent callbacks
261-
self._apply_widget_update_callback = False
262-
263-
# Disable alt+x
264-
self._alt_x_enabled = False
265-
266263
def _apply_font(self) -> None:
267264
super()._apply_font()
268265

pygame_menu/widgets/widget/dropselect_multiple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def __init__(
243243
selection_option_selected_bgcolor=selection_option_selected_bgcolor,
244244
selection_option_selected_font_color=selection_option_selected_font_color,
245245
title=title,
246+
args=args,
246247
**kwargs,
247248
)
248249

@@ -261,7 +262,6 @@ def __init__(
261262
assert isinstance(max_selected, int) and max_selected >= 0
262263

263264
# Configure parent
264-
self._args = args or []
265265
self._max_selected = max_selected
266266
self._selection_option_left_space_margin = selection_option_selected_box_margin
267267

pygame_menu/widgets/widget/textinput.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ class TextInput(Widget):
164164
"""
165165

166166
_absolute_origin: Tuple2IntType
167-
_alt_x_enabled: bool
168-
_apply_widget_update_callback: bool # Used in ColorInput
169167
_block_copy_paste: bool
170168
_clock: pygame.time.Clock
171169
_copy_paste_enabled: bool
@@ -228,6 +226,8 @@ def __init__(
228226
self,
229227
title: Any,
230228
textinput_id: str = "",
229+
apply_widget_update_callback: bool = True,
230+
alt_x_enabled: bool = True,
231231
copy_paste_enable: bool = True,
232232
cursor_color: ColorInputType = (0, 0, 0),
233233
cursor_selection_color: ColorInputType = (30, 30, 30, 100),
@@ -406,10 +406,10 @@ def __init__(
406406
self._valid_chars = valid_chars
407407

408408
# Callbacks
409-
self._apply_widget_update_callback = True
409+
self._apply_widget_update_callback = apply_widget_update_callback
410410

411411
# Other
412-
self._alt_x_enabled = True
412+
self._alt_x_enabled = alt_x_enabled
413413
self._copy_paste_enabled = copy_paste_enable
414414
self._current_underline_string = ""
415415
self._input_type = input_type

test/test_decorator.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
import pygame
1313
import pytest
1414

15-
import pygame_menu
15+
from pygame_menu.baseimage import IMAGE_EXAMPLE_PYGAME_MENU, BaseImage
16+
from pygame_menu.font import FONT_8BIT
17+
from pygame_menu.widgets.widget.button import Button
18+
from pygame_menu.widgets.widget.none import NoneWidget
1619
from test._utils import TEST_THEME, MenuUtils, surface
1720

1821
TEST_TIME_DRAW = False
1922

2023

2124
@pytest.mark.skipif(not TEST_TIME_DRAW, reason="Timing test disabled")
2225
def test_time_draw():
23-
widg = pygame_menu.widgets.NoneWidget()
26+
widg = NoneWidget()
2427
deco = widg.get_decorator()
2528
deco.cache = True
2629

@@ -37,7 +40,7 @@ def test_time_draw():
3740

3841

3942
def test_cache():
40-
widg = pygame_menu.widgets.NoneWidget()
43+
widg = NoneWidget()
4144
deco = widg.get_decorator()
4245
deco.cache = True
4346

@@ -87,7 +90,7 @@ def test_cache():
8790

8891

8992
def test_copy():
90-
widg = pygame_menu.widgets.NoneWidget()
93+
widg = NoneWidget()
9194
deco = widg.get_decorator()
9295

9396
with pytest.raises(Exception):
@@ -97,7 +100,7 @@ def test_copy():
97100

98101

99102
def test_add_remove():
100-
widg = pygame_menu.widgets.NoneWidget()
103+
widg = NoneWidget()
101104
deco = widg.get_decorator()
102105

103106
d = deco._add_none()
@@ -133,7 +136,7 @@ def test_enable_disable():
133136
def fun(surf, obj):
134137
test[0] = True
135138
assert isinstance(surf, pygame.Surface)
136-
assert isinstance(obj, pygame_menu.widgets.Button)
139+
assert isinstance(obj, Button)
137140

138141
call_id = deco.add_callable(fun)
139142
assert not test[0]
@@ -197,7 +200,7 @@ def test_general():
197200
deco.add_circle(1, 1, 100, color, False, 5)
198201
deco.add_circle(50, 50, 100, color, True)
199202

200-
img = pygame_menu.BaseImage(pygame_menu.baseimage.IMAGE_EXAMPLE_PYGAME_MENU)
203+
img = BaseImage(IMAGE_EXAMPLE_PYGAME_MENU)
201204
img.scale(0.15, 0.15)
202205
deco.add_surface(60, 60, img.get_surface(), prev=False)
203206

@@ -244,9 +247,9 @@ def test_general():
244247
assert deco._coord_cache[img_dec] == (300, 173, ((300, 173),))
245248

246249
with pytest.raises(ValueError):
247-
deco.add_text(100, 200, "nice", pygame_menu.font.FONT_8BIT, 0, color)
250+
deco.add_text(100, 200, "nice", FONT_8BIT, 0, color)
248251

249-
deco.add_text(-150, 0, "nice", pygame_menu.font.FONT_8BIT, 20, color, centered=True)
252+
deco.add_text(-150, 0, "nice", FONT_8BIT, 20, color, centered=True)
250253
menu.draw(surface)
251254

252255
with pytest.raises(AssertionError):

test/test_sound.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
import copy
1010
import time
11-
from pathlib import Path
1211

1312
import pygame
1413
import pytest
1514

16-
import pygame_menu
1715
from pygame_menu.sound import (
1816
SOUND_EXAMPLES,
1917
SOUND_INITIALIZED,

test/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import pytest
1010

11-
import pygame_menu
1211
import pygame_menu.utils as ut
1312
from pygame_menu.locals import POSITION_NORTHWEST
13+
from pygame_menu.widgets.widget.button import Button
1414

1515

1616
def test_alpha():
@@ -46,7 +46,7 @@ def test_padding(value, expected):
4646

4747

4848
def test_terminal_widget_title():
49-
w = pygame_menu.widgets.Button("epic")
49+
w = Button("epic")
5050
w.hide()
5151
s = ut.widget_terminal_title(w)
5252
assert "╳" in s

0 commit comments

Comments
 (0)