Skip to content

Commit 8541a5d

Browse files
authored
Merge pull request #520 from JaskRendix/ruff
Enable Ruff UP & TCH Rules and Apply Automatic Fixes
2 parents b93e3d5 + 75ac9bf commit 8541a5d

91 files changed

Lines changed: 2012 additions & 1415 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ jobs:
3737

3838
steps:
3939
- name: Checkout repository
40-
uses: actions/checkout@v4
40+
uses: actions/checkout@v6
4141

4242
- name: Set up Python
43-
uses: actions/setup-python@v5
43+
uses: actions/setup-python@v6
4444
with:
4545
python-version: '3.12'
4646

@@ -57,3 +57,18 @@ jobs:
5757
- name: Run Semgrep
5858
run: |
5959
semgrep --config p/ci .
60+
61+
ruff:
62+
runs-on: ubuntu-latest
63+
steps:
64+
- uses: actions/checkout@v6
65+
- name: Set up Python
66+
uses: actions/setup-python@v6
67+
with:
68+
python-version: '3.9'
69+
- name: Install dependencies
70+
run: |
71+
python -m pip install --upgrade pip
72+
pip install ruff
73+
- name: Run Ruff
74+
run: ruff check .

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ run_gource.bat
2727
# Tox
2828
tox.ini
2929

30-
# Pyproject
31-
pyproject_update.py
32-
pyproject.toml
33-
3430
# Lock file
3531
*.lock
3632

docs/add_widgets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def change_background_color(selected_value, color, **kwargs):
6868
print('Change widget color to', value_tuple[0]) # selected_value format ('Color', surface, color)
6969
if color == (-1, -1, -1): # Generate a random color
7070
color = (randrange(0, 255), randrange(0, 255), randrange(0, 255))
71-
widget: 'pygame_menu.widgets.Selector' = kwargs.get('widget')
71+
widget: pygame_menu.widgets.Selector = kwargs.get('widget')
7272
widget.update_font({'selected_color': color})
7373
widget.get_selection_effect().color = color
7474

@@ -252,7 +252,7 @@ def open_link(*args) -> None:
252252
"""
253253
Opens link.
254254
"""
255-
link: 'pygame_menu.widgets.MenuLink' = args[-1]
255+
link: pygame_menu.widgets.MenuLink = args[-1]
256256
link.open()
257257

258258

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# -- Path setup ---------------------------------------------------------------
1414

1515
import sys
16+
1617
# If extensions (or modules to document with autodoc) are in another directory,
1718
# add these directories to sys.path here. If the directory is relative to the
1819
# documentation root, use pathlib to build absolute paths in a portable way.

pygame_menu/__pyinstaller/hook-pygame_menu.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def _append_to_datas(file_path: str, target_folder: str, base_target_folder: Pat
4343

4444

4545
from pygame_menu.baseimage import IMAGE_EXAMPLES
46+
4647
# Append data
4748
from pygame_menu.font import FONT_EXAMPLES
4849
from pygame_menu.sound import SOUND_EXAMPLES

pygame_menu/_base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
__all__ = ['Base']
1212

13-
from typing import Any, Optional
13+
from typing import Any
1414

1515
from pygame_menu._types import NumberInstance, NumberType
1616
from pygame_menu.utils import uuid4
@@ -20,7 +20,7 @@ class Base:
2020
"""
2121
Base object.
2222
"""
23-
_attributes: Optional[dict[str, Any]]
23+
_attributes: dict[str, Any] | None
2424
_class_id__repr__: bool
2525
_id: str
2626
_id__repr__: bool
@@ -63,7 +63,7 @@ def __repr__(self) -> str:
6363

6464
return sup_repr
6565

66-
def _update__repr___(self, obj: 'Base') -> None:
66+
def _update__repr___(self, obj: Base) -> None:
6767
"""
6868
Update __repr__ from other Base object.
6969
@@ -72,7 +72,7 @@ def _update__repr___(self, obj: 'Base') -> None:
7272
self._class_id__repr__ = obj._class_id__repr__
7373
self._id__repr__ = obj._id__repr__
7474

75-
def set_attribute(self, key: str, value: Any = None) -> 'Base':
75+
def set_attribute(self, key: str, value: Any = None) -> Base:
7676
"""
7777
Set an attribute.
7878
@@ -147,7 +147,7 @@ def has_attribute(self, key: str) -> bool:
147147

148148
return self._attributes is not None and key in self._attributes
149149

150-
def remove_attribute(self, key: str) -> 'Base':
150+
def remove_attribute(self, key: str) -> Base:
151151
"""
152152
Removes the given attribute from the object. Throws ``IndexError`` if
153153
the given key does not exist.

pygame_menu/_decorator.py

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,36 @@
1212
__all__ = ['Decorator']
1313

1414
import math
15-
from collections.abc import Callable
1615
from math import pi
17-
from typing import Any, Optional, Union
16+
from typing import TYPE_CHECKING, Any
1817

1918
import pygame
2019
import pygame.draw as pydraw
2120
import pygame.gfxdraw as gfxdraw
2221

2322
import pygame_menu
2423
from pygame_menu._base import Base
25-
from pygame_menu._types import (CallableNoArgsType, ColorInputType,
26-
NumberInstance, NumberType, Tuple2IntType,
27-
Tuple2NumberType)
28-
from pygame_menu.font import FontType
29-
from pygame_menu.utils import (assert_color, assert_list_vector, assert_vector,
30-
make_surface, uuid4, warn)
24+
from pygame_menu._types import (
25+
CallableNoArgsType,
26+
ColorInputType,
27+
NumberInstance,
28+
NumberType,
29+
Tuple2IntType,
30+
Tuple2NumberType,
31+
)
32+
from pygame_menu.utils import (
33+
assert_color,
34+
assert_list_vector,
35+
assert_vector,
36+
make_surface,
37+
uuid4,
38+
warn,
39+
)
40+
41+
if TYPE_CHECKING:
42+
from collections.abc import Callable
43+
44+
from pygame_menu.font import FontType
3145

3246
# Decoration constants
3347
DECORATION_ARC: int = 2000
@@ -62,21 +76,21 @@ class Decorator(Base):
6276
:param verbose: Enable/disable verbose mode (warnings/errors)
6377
"""
6478
_coord_cache: dict[
65-
str, tuple[int, int, Union[tuple[Tuple2NumberType, ...], Tuple2NumberType]]] # centerx, centery, coords
79+
str, tuple[int, int, tuple[Tuple2NumberType, ...] | Tuple2NumberType]] # centerx, centery, coords
6680
_cache_last_status: dict[str, tuple[int, int, int, int, int, int]]
6781
_cache_needs_update: dict[str, bool]
68-
_cache_surface: dict[str, Optional['pygame.Surface']]
82+
_cache_surface: dict[str, pygame.Surface | None]
6983
_decor: dict[str, list[tuple[int, str, Any]]] # type, id, data
7084
_decor_enabled: dict[str, bool]
7185
_decor_prev_id: list[str]
72-
_obj: Union['pygame_menu.widgets.Widget', 'pygame_menu._scrollarea.ScrollArea', 'pygame_menu.Menu']
86+
_obj: pygame_menu.widgets.Widget | pygame_menu._scrollarea.ScrollArea | pygame_menu.Menu
7387
_post_enabled: bool
7488
_prev_enabled: bool
7589
cache: bool
7690

7791
def __init__(
7892
self,
79-
obj: Union['pygame_menu.widgets.Widget', 'pygame_menu._scrollarea.ScrollArea', 'pygame_menu.Menu'],
93+
obj: pygame_menu.widgets.Widget | pygame_menu._scrollarea.ScrollArea | pygame_menu.Menu,
8094
decorator_id: str = '',
8195
verbose: bool = True
8296
) -> None:
@@ -112,15 +126,15 @@ def __init__(
112126
self._cache_needs_update = {DECOR_TYPE_PREV: False, DECOR_TYPE_POST: False}
113127
self._cache_surface = {DECOR_TYPE_PREV: None, DECOR_TYPE_POST: None}
114128

115-
def __copy__(self) -> 'Decorator':
129+
def __copy__(self) -> Decorator:
116130
"""
117131
Copy method.
118132
119133
:return: Raises copy exception
120134
"""
121135
raise _DecoratorCopyException('Decorator class cannot be copied')
122136

123-
def __deepcopy__(self, memodict: dict[int, Any]) -> 'Decorator':
137+
def __deepcopy__(self, memodict: dict[int, Any]) -> Decorator:
124138
"""
125139
Deep-copy method.
126140
@@ -180,7 +194,7 @@ def _total_decor(self) -> int:
180194
"""
181195
return len(self._decor[DECOR_TYPE_PREV]) + len(self._decor[DECOR_TYPE_POST])
182196

183-
def force_cache_update(self, prev: Optional[bool] = None) -> 'Decorator':
197+
def force_cache_update(self, prev: bool | None = None) -> Decorator:
184198
"""
185199
Forces cache update.
186200
@@ -196,7 +210,7 @@ def force_cache_update(self, prev: Optional[bool] = None) -> 'Decorator':
196210

197211
def add_polygon(
198212
self,
199-
coords: Union[list[Tuple2NumberType], tuple[Tuple2NumberType, ...]],
213+
coords: list[Tuple2NumberType] | tuple[Tuple2NumberType, ...],
200214
color: ColorInputType,
201215
filled: bool,
202216
width: int = 0,
@@ -236,7 +250,7 @@ def add_polygon(
236250

237251
def add_bezier(
238252
self,
239-
coords: Union[list[Tuple2NumberType], tuple[Tuple2NumberType, ...]],
253+
coords: list[Tuple2NumberType] | tuple[Tuple2NumberType, ...],
240254
color: ColorInputType,
241255
steps: int = 5,
242256
prev: bool = True,
@@ -396,7 +410,7 @@ def add_surface(
396410
self,
397411
x: NumberType,
398412
y: NumberType,
399-
surface: 'pygame.Surface',
413+
surface: pygame.Surface,
400414
prev: bool = True,
401415
centered: bool = False,
402416
**kwargs
@@ -427,7 +441,7 @@ def add_baseimage(
427441
self,
428442
x: NumberType,
429443
y: NumberType,
430-
image: 'pygame_menu.BaseImage',
444+
image: pygame_menu.BaseImage,
431445
prev: bool = True,
432446
centered: bool = False,
433447
**kwargs
@@ -463,7 +477,7 @@ def add_rect(
463477
self,
464478
x: NumberType,
465479
y: NumberType,
466-
rect: 'pygame.Rect',
480+
rect: pygame.Rect,
467481
color: ColorInputType,
468482
width: int = 0,
469483
prev: bool = True,
@@ -639,7 +653,7 @@ def add_pixel(
639653

640654
def add_callable(
641655
self,
642-
fun: Union[Callable[['pygame.Surface', Any], Any], CallableNoArgsType],
656+
fun: Callable[[pygame.Surface, Any], Any] | CallableNoArgsType,
643657
prev: bool = True,
644658
pass_args: bool = True
645659
) -> str:
@@ -671,8 +685,8 @@ def add_callable(
671685

672686
def add_textured_polygon(
673687
self,
674-
coords: Union[list[Tuple2NumberType], tuple[Tuple2NumberType, ...]],
675-
texture: Union['pygame.Surface', 'pygame_menu.BaseImage'],
688+
coords: list[Tuple2NumberType] | tuple[Tuple2NumberType, ...],
689+
texture: pygame.Surface | pygame_menu.BaseImage,
676690
tx: int = 0,
677691
ty: int = 0,
678692
prev: bool = True,
@@ -811,7 +825,7 @@ def add_vline(
811825
assert y1 != y2
812826
return self.add_line((x, y1), (x, y2), color, width, prev, **kwargs)
813827

814-
def disable(self, decorid: str) -> 'Decorator':
828+
def disable(self, decorid: str) -> Decorator:
815829
"""
816830
Disable a certain decoration from ID. Raises ``IndexError`` if decoration was
817831
not found.
@@ -825,7 +839,7 @@ def disable(self, decorid: str) -> 'Decorator':
825839
self.force_cache_update(prev=decorid in self._decor_prev_id)
826840
return self
827841

828-
def enable(self, decorid: str) -> 'Decorator':
842+
def enable(self, decorid: str) -> Decorator:
829843
"""
830844
Enable a certain decoration from ID. Raises ``IndexError`` if decoration
831845
was not found.
@@ -851,7 +865,7 @@ def is_enabled(self, decorid: str) -> bool:
851865
raise IndexError(f'decoration<"{decorid}"> was not found')
852866
return self._decor_enabled[decorid]
853867

854-
def remove(self, decorid: str) -> 'Decorator':
868+
def remove(self, decorid: str) -> Decorator:
855869
"""
856870
Remove a decoration from a given ID. Raises ``IndexError`` if decoration
857871
was not found.
@@ -873,7 +887,7 @@ def remove(self, decorid: str) -> 'Decorator':
873887
return self
874888
raise IndexError(f'decoration<"{decorid}"> was not found')
875889

876-
def remove_all(self, prev: Optional[bool] = None) -> 'Decorator':
890+
def remove_all(self, prev: bool | None = None) -> Decorator:
877891
"""
878892
Remove all decorations.
879893
@@ -894,7 +908,7 @@ def _draw_assemble_cache(
894908
self,
895909
prev: str,
896910
deco: list[tuple[int, str, Any]],
897-
surface: 'pygame.Surface'
911+
surface: pygame.Surface
898912
) -> None:
899913
"""
900914
Draw cache, assemble if needed.
@@ -931,7 +945,7 @@ def _draw_assemble_cache(
931945

932946
surface.blit(self._cache_surface[prev], (0, 0))
933947

934-
def draw_prev(self, surface: 'pygame.Surface') -> 'Decorator':
948+
def draw_prev(self, surface: pygame.Surface) -> Decorator:
935949
"""
936950
Draw prev.
937951
@@ -944,7 +958,7 @@ def draw_prev(self, surface: 'pygame.Surface') -> 'Decorator':
944958
self._draw_assemble_cache(DECOR_TYPE_PREV, self._decor[DECOR_TYPE_PREV], surface)
945959
return self
946960

947-
def draw_post(self, surface: 'pygame.Surface') -> 'Decorator':
961+
def draw_post(self, surface: pygame.Surface) -> Decorator:
948962
"""
949963
Draw post.
950964
@@ -957,7 +971,7 @@ def draw_post(self, surface: 'pygame.Surface') -> 'Decorator':
957971
self._draw_assemble_cache(DECOR_TYPE_POST, self._decor[DECOR_TYPE_POST], surface)
958972
return self
959973

960-
def _draw(self, deco: list[tuple[int, str, Any]], surface: 'pygame.Surface') -> None:
974+
def _draw(self, deco: list[tuple[int, str, Any]], surface: pygame.Surface) -> None:
961975
"""
962976
Draw.
963977
@@ -1055,7 +1069,7 @@ def _draw(self, deco: list[tuple[int, str, Any]], surface: 'pygame.Surface') ->
10551069
surface.fill(data, rect)
10561070

10571071
elif dtype == DECORATION_RECT:
1058-
d_rect: 'pygame.Rect'
1072+
d_rect: pygame.Rect
10591073
pos, d_rect, color, width, kwargs = data
10601074
pos = self._update_pos_list(rect, decoid, pos, **kwargs)[0]
10611075
d_rect = d_rect.copy()
@@ -1078,11 +1092,11 @@ def _draw(self, deco: list[tuple[int, str, Any]], surface: 'pygame.Surface') ->
10781092

10791093
def _update_pos_list(
10801094
self,
1081-
rect: 'pygame.Rect',
1095+
rect: pygame.Rect,
10821096
decoid: str,
1083-
pos: Union[Tuple2NumberType, tuple[Tuple2NumberType, ...]], # only (x, y) or ((x1,y1), ...
1097+
pos: Tuple2NumberType | tuple[Tuple2NumberType, ...], # only (x, y) or ((x1,y1), ...
10841098
use_center_positioning=True
1085-
) -> Union[tuple[Tuple2IntType, ...], Tuple2IntType]:
1099+
) -> tuple[Tuple2IntType, ...] | Tuple2IntType:
10861100
"""
10871101
Updates position list based on rect center. If position of the rect changes,
10881102
update the coords.

0 commit comments

Comments
 (0)