Skip to content

Commit 89d8e7b

Browse files
committed
ruff
1 parent 99134af commit 89d8e7b

50 files changed

Lines changed: 564 additions & 462 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/ruff.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Python package
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
ruff:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v5
10+
- name: Set up Python
11+
uses: actions/setup-python@v6
12+
with:
13+
python-version: '3.9'
14+
- name: Install dependencies
15+
run: |
16+
python -m pip install --upgrade pip
17+
pip install ruff
18+
- name: Run Ruff
19+
run: ruff check . --select UP,TCH --target-version py39

pygame_menu/_base.py

Lines changed: 2 additions & 2 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

pygame_menu/_decorator.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
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
@@ -25,10 +24,14 @@
2524
from pygame_menu._types import (CallableNoArgsType, ColorInputType,
2625
NumberInstance, NumberType, Tuple2IntType,
2726
Tuple2NumberType)
28-
from pygame_menu.font import FontType
2927
from pygame_menu.utils import (assert_color, assert_list_vector, assert_vector,
3028
make_surface, uuid4, warn)
3129

30+
if TYPE_CHECKING:
31+
from collections.abc import Callable
32+
33+
from pygame_menu.font import FontType
34+
3235
# Decoration constants
3336
DECORATION_ARC: int = 2000
3437
DECORATION_BASEIMAGE: int = 2001
@@ -62,21 +65,21 @@ class Decorator(Base):
6265
:param verbose: Enable/disable verbose mode (warnings/errors)
6366
"""
6467
_coord_cache: dict[
65-
str, tuple[int, int, Union[tuple[Tuple2NumberType, ...], Tuple2NumberType]]] # centerx, centery, coords
68+
str, tuple[int, int, tuple[Tuple2NumberType, ...] | Tuple2NumberType]] # centerx, centery, coords
6669
_cache_last_status: dict[str, tuple[int, int, int, int, int, int]]
6770
_cache_needs_update: dict[str, bool]
68-
_cache_surface: dict[str, Optional[pygame.Surface]]
71+
_cache_surface: dict[str, pygame.Surface | None]
6972
_decor: dict[str, list[tuple[int, str, Any]]] # type, id, data
7073
_decor_enabled: dict[str, bool]
7174
_decor_prev_id: list[str]
72-
_obj: Union[pygame_menu.widgets.Widget, pygame_menu._scrollarea.ScrollArea, pygame_menu.Menu]
75+
_obj: pygame_menu.widgets.Widget | pygame_menu._scrollarea.ScrollArea | pygame_menu.Menu
7376
_post_enabled: bool
7477
_prev_enabled: bool
7578
cache: bool
7679

7780
def __init__(
7881
self,
79-
obj: Union[pygame_menu.widgets.Widget, pygame_menu._scrollarea.ScrollArea, pygame_menu.Menu],
82+
obj: pygame_menu.widgets.Widget | pygame_menu._scrollarea.ScrollArea | pygame_menu.Menu,
8083
decorator_id: str = '',
8184
verbose: bool = True
8285
) -> None:
@@ -180,7 +183,7 @@ def _total_decor(self) -> int:
180183
"""
181184
return len(self._decor[DECOR_TYPE_PREV]) + len(self._decor[DECOR_TYPE_POST])
182185

183-
def force_cache_update(self, prev: Optional[bool] = None) -> Decorator:
186+
def force_cache_update(self, prev: bool | None = None) -> Decorator:
184187
"""
185188
Forces cache update.
186189
@@ -196,7 +199,7 @@ def force_cache_update(self, prev: Optional[bool] = None) -> Decorator:
196199

197200
def add_polygon(
198201
self,
199-
coords: Union[list[Tuple2NumberType], tuple[Tuple2NumberType, ...]],
202+
coords: list[Tuple2NumberType] | tuple[Tuple2NumberType, ...],
200203
color: ColorInputType,
201204
filled: bool,
202205
width: int = 0,
@@ -236,7 +239,7 @@ def add_polygon(
236239

237240
def add_bezier(
238241
self,
239-
coords: Union[list[Tuple2NumberType], tuple[Tuple2NumberType, ...]],
242+
coords: list[Tuple2NumberType] | tuple[Tuple2NumberType, ...],
240243
color: ColorInputType,
241244
steps: int = 5,
242245
prev: bool = True,
@@ -639,7 +642,7 @@ def add_pixel(
639642

640643
def add_callable(
641644
self,
642-
fun: Union[Callable[[pygame.Surface, Any], Any], CallableNoArgsType],
645+
fun: Callable[[pygame.Surface, Any], Any] | CallableNoArgsType,
643646
prev: bool = True,
644647
pass_args: bool = True
645648
) -> str:
@@ -671,8 +674,8 @@ def add_callable(
671674

672675
def add_textured_polygon(
673676
self,
674-
coords: Union[list[Tuple2NumberType], tuple[Tuple2NumberType, ...]],
675-
texture: Union[pygame.Surface, pygame_menu.BaseImage],
677+
coords: list[Tuple2NumberType] | tuple[Tuple2NumberType, ...],
678+
texture: pygame.Surface | pygame_menu.BaseImage,
676679
tx: int = 0,
677680
ty: int = 0,
678681
prev: bool = True,
@@ -873,7 +876,7 @@ def remove(self, decorid: str) -> Decorator:
873876
return self
874877
raise IndexError(f'decoration<"{decorid}"> was not found')
875878

876-
def remove_all(self, prev: Optional[bool] = None) -> Decorator:
879+
def remove_all(self, prev: bool | None = None) -> Decorator:
877880
"""
878881
Remove all decorations.
879882
@@ -1080,9 +1083,9 @@ def _update_pos_list(
10801083
self,
10811084
rect: pygame.Rect,
10821085
decoid: str,
1083-
pos: Union[Tuple2NumberType, tuple[Tuple2NumberType, ...]], # only (x, y) or ((x1,y1), ...
1086+
pos: Tuple2NumberType | tuple[Tuple2NumberType, ...], # only (x, y) or ((x1,y1), ...
10841087
use_center_positioning=True
1085-
) -> Union[tuple[Tuple2IntType, ...], Tuple2IntType]:
1088+
) -> tuple[Tuple2IntType, ...] | Tuple2IntType:
10861089
"""
10871090
Updates position list based on rect center. If position of the rect changes,
10881091
update the coords.

pygame_menu/_scrollarea.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
]
2020

2121
from itertools import product
22-
from typing import Any, Optional, Union
22+
from typing import Any
2323

2424
import pygame
2525

@@ -46,7 +46,7 @@
4646

4747
def get_scrollbars_from_position(
4848
position: str
49-
) -> Union[str, tuple[str, str], tuple[str, str, str, str]]:
49+
) -> str | tuple[str, str] | tuple[str, str, str, str]:
5050
"""
5151
Return the scrollbars from the given position.
5252
@@ -125,41 +125,41 @@ class ScrollArea(Base):
125125
:param shadow_position: Position of the scrollbar shadow. See :py:mod:`pygame_menu.locals`
126126
:param world: Surface to draw and scroll
127127
"""
128-
_area_color: Optional[Union[ColorInputType, pygame_menu.BaseImage]]
129-
_border_color: Optional[Union[ColorInputType, pygame_menu.BaseImage]]
128+
_area_color: ColorInputType | pygame_menu.BaseImage | None
129+
_border_color: ColorInputType | pygame_menu.BaseImage | None
130130
_border_tiles: list[pygame.Surface]
131131
_border_tiles_size: Tuple2IntType
132132
_border_width: int
133-
_bg_surface: Optional[pygame.Surface]
133+
_bg_surface: pygame.Surface | None
134134
_decorator: Decorator
135135
_extend_x: int
136136
_extend_y: int
137-
_menu: Optional[pygame_menu.Menu]
137+
_menu: pygame_menu.Menu | None
138138
_menubar: pygame_menu.widgets.MenuBar
139139
_parent_scrollarea: ScrollArea
140140
_rect: pygame.Rect
141-
_scrollbar_positions: Union[str, tuple[str, ...]]
141+
_scrollbar_positions: str | tuple[str, ...]
142142
_scrollbars: list[ScrollBar]
143143
_scrollbars_props: tuple[Any, ...]
144144
_translate: Tuple2IntType
145145
_view_rect: pygame.Rect
146-
_world: Optional[pygame.Surface]
146+
_world: pygame.Surface | None
147147

148148
def __init__(
149149
self,
150150
area_width: int,
151151
area_height: int,
152-
area_color: Optional[Union[ColorInputType, pygame_menu.BaseImage]] = None,
153-
border_color: Optional[Union[ColorInputType, pygame_menu.BaseImage]] = None,
152+
area_color: ColorInputType | pygame_menu.BaseImage | None = None,
153+
border_color: ColorInputType | pygame_menu.BaseImage | None = None,
154154
border_width: int = 0,
155155
controls_joystick: bool = True,
156156
controls_keyboard: bool = True,
157157
controls_mouse: bool = True,
158158
controls_touchscreen: bool = True,
159159
extend_x: int = 0,
160160
extend_y: int = 0,
161-
menubar: Optional[pygame_menu.widgets.MenuBar] = None,
162-
parent_scrollarea: Optional[ScrollArea] = None,
161+
menubar: pygame_menu.widgets.MenuBar | None = None,
162+
parent_scrollarea: ScrollArea | None = None,
163163
scrollarea_id: str = '',
164164
scrollbar_color: ColorInputType = (235, 235, 235),
165165
scrollbar_cursor: CursorInputType = None, # type: ignore
@@ -172,7 +172,7 @@ def __init__(
172172
shadow_color: ColorInputType = (0, 0, 0),
173173
shadow_offset: int = 2,
174174
shadow_position: str = POSITION_SOUTHEAST,
175-
world: Optional[pygame.Surface] = None
175+
world: pygame.Surface | None = None
176176
) -> None:
177177
super().__init__(object_id=scrollarea_id)
178178

@@ -344,7 +344,7 @@ def _make_background_surface(self) -> None:
344344

345345
def update_area_color(
346346
self,
347-
color: Optional[Union[ColorInputType, pygame_menu.BaseImage]]
347+
color: ColorInputType | pygame_menu.BaseImage | None
348348
) -> ScrollArea:
349349
"""
350350
Updates area color (background).
@@ -357,7 +357,7 @@ def update_area_color(
357357
self._make_background_surface()
358358
return self
359359

360-
def set_parent_scrollarea(self, parent: Optional[ScrollArea]) -> ScrollArea:
360+
def set_parent_scrollarea(self, parent: ScrollArea | None) -> ScrollArea:
361361
"""
362362
Set parent ScrollArea.
363363
@@ -368,7 +368,7 @@ def set_parent_scrollarea(self, parent: Optional[ScrollArea]) -> ScrollArea:
368368
self._parent_scrollarea = parent
369369
return self
370370

371-
def get_parent(self) -> Optional[ScrollArea]:
371+
def get_parent(self) -> ScrollArea | None:
372372
"""
373373
Return the parent ScrollArea.
374374
@@ -547,7 +547,7 @@ def draw(self, surface: pygame.Surface) -> ScrollArea:
547547
top -= th
548548

549549
# draw top and bottom tiles
550-
area: Optional[tuple[int, int, int, int]]
550+
area: tuple[int, int, int, int] | None
551551

552552
for x in range(border_rect.left, border_rect.right, tw):
553553
if x + tw >= border_rect.right:
@@ -1072,7 +1072,7 @@ def set_world(self, surface: pygame.Surface) -> ScrollArea:
10721072
self._apply_size_changes()
10731073
return self
10741074

1075-
def get_world(self) -> Optional[pygame.Surface]:
1075+
def get_world(self) -> pygame.Surface | None:
10761076
"""
10771077
Return the world surface area.
10781078
@@ -1140,9 +1140,9 @@ def get_absolute_view_rect(self) -> pygame.Rect:
11401140

11411141
def to_real_position(
11421142
self,
1143-
virtual: Union[pygame.Rect, Tuple2NumberType],
1143+
virtual: pygame.Rect | Tuple2NumberType,
11441144
visible: bool = False
1145-
) -> Union[pygame.Rect, Tuple2IntType]:
1145+
) -> pygame.Rect | Tuple2IntType:
11461146
"""
11471147
Return the real position/Rect according to the ScrollArea origin of a
11481148
position/Rect in the world surface reference.
@@ -1174,8 +1174,8 @@ def to_real_position(
11741174

11751175
def to_world_position(
11761176
self,
1177-
real: Union[pygame.Rect, Tuple2NumberType]
1178-
) -> Union[pygame.Rect, Tuple2IntType]:
1177+
real: pygame.Rect | Tuple2NumberType
1178+
) -> pygame.Rect | Tuple2IntType:
11791179
"""
11801180
Return the position/Rect in the world surface reference of a real
11811181
position/Rect according to the ScrollArea origin.
@@ -1242,7 +1242,7 @@ def set_menu(self, menu: pygame_menu.Menu) -> ScrollArea:
12421242
sbar.set_menu(menu)
12431243
return self
12441244

1245-
def get_menu(self) -> Optional[pygame_menu.Menu]:
1245+
def get_menu(self) -> pygame_menu.Menu | None:
12461246
"""
12471247
Return the Menu reference (if exists).
12481248
@@ -1252,7 +1252,7 @@ def get_menu(self) -> Optional[pygame_menu.Menu]:
12521252

12531253
def collide(
12541254
self,
1255-
widget: Union[pygame_menu.widgets.Widget, pygame.Rect],
1255+
widget: pygame_menu.widgets.Widget | pygame.Rect,
12561256
event: EventType
12571257
) -> bool:
12581258
"""
@@ -1269,7 +1269,7 @@ def collide(
12691269
widget_rect = widget
12701270
return bool(widget_rect.collidepoint(*get_finger_pos(self._menu, event)))
12711271

1272-
def get_scrollbar(self, position: str) -> Optional[ScrollBar]:
1272+
def get_scrollbar(self, position: str) -> ScrollBar | None:
12731273
"""
12741274
Returns the scrollbar at the given position, or None if not present.
12751275
:param position: The position of the scrollbar (e.g., POSITION_NORTH, POSITION_EAST).

0 commit comments

Comments
 (0)