1212__all__ = ['Decorator' ]
1313
1414import math
15- from collections .abc import Callable
1615from math import pi
17- from typing import Any , Optional , Union
16+ from typing import TYPE_CHECKING , Any
1817
1918import pygame
2019import pygame .draw as pydraw
2120import pygame .gfxdraw as gfxdraw
2221
2322import pygame_menu
2423from 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
3347DECORATION_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