Skip to content

Commit ef50180

Browse files
committed
Pylint processing
1 parent e883e5d commit ef50180

39 files changed

+559
-267
lines changed

pygame_gui/core/colour_parser.py

+87-11
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
of any additions to this parser TL,DR: Dev life will be easier if it is ensured that commas in colour schemas
2828
are inside of parentheses, brackets, or curly braces (like "rgb(20, 20, 20)")"""
2929

30-
import pygame
30+
import enum
3131
from typing import (
3232
Callable,
3333
Union,
34-
Iterable,
3534
TypeVar,
3635
Optional,
3736
List,
@@ -41,7 +40,8 @@
4140
TypedDict,
4241
)
4342

44-
import enum
43+
import pygame
44+
4545
from pygame_gui.core.colour_gradient import ColourGradient
4646
from pygame_gui.core.utility import premul_col
4747
from pygame_gui._constants import _namedColours
@@ -61,6 +61,13 @@ class NumParserType(enum.Enum):
6161

6262

6363
def is_num_str(string: str) -> bool:
64+
"""
65+
Check if a string converts to a number.
66+
67+
:param string: the string to check
68+
69+
:return: A boolean, True if the string converts to a number, False otherwise
70+
"""
6471
try:
6572
float(string)
6673
return True
@@ -69,14 +76,36 @@ def is_num_str(string: str) -> bool:
6976

7077

7178
def is_int_str(string: str) -> bool:
79+
"""
80+
Check if a string converts to a round number.
81+
82+
:param string: the string to check
83+
84+
:return: A boolean, True if the string converts to a non-fractional number, False otherwise
85+
"""
7286
return is_num_str(string) and "." not in string
7387

7488

7589
def is_float_str(string: str):
90+
"""
91+
Check if a string converts to a fractional number.
92+
93+
:param string: the string to check
94+
95+
:return: A boolean, True if the string converts to a fractional number, False otherwise
96+
"""
7697
return is_num_str(string) and "." in string
7798

7899

79100
def is_degree_string(strdata: str) -> bool:
101+
"""
102+
Check if a string converts to a degree. This means that it either ends 'deg' and the rest is an integer number
103+
between -360 and 360; or it is just an integer number between -360 and 360.
104+
105+
:param strdata: the string to check
106+
107+
:return: A boolean, True if the string converts to a degree, False otherwise
108+
"""
80109
if strdata != "":
81110
if strdata.endswith("deg") and len(strdata) > 3:
82111
if is_int_str(strdata[:-3]):
@@ -89,30 +118,73 @@ def is_degree_string(strdata: str) -> bool:
89118

90119

91120
def parse_degree_string(strdata: str) -> int:
121+
"""
122+
Converts a string to a degree integer. This means that it either ends 'deg' and the rest is an integer number
123+
between -360 and 360; or it is just an integer number between -360 and 360.
124+
125+
Should be used with 'is_degree_string()' to check for validity.
126+
127+
:param strdata: the string to parse.
128+
129+
:return: an integer between -360 and 360
130+
"""
92131
if strdata != "":
93132
return int(strdata[:-3]) if strdata.endswith("deg") else int(strdata)
133+
return 0
94134

95135

96136
def is_percentage_string(strdata: str) -> bool:
137+
"""
138+
Check if a string converts to a percentage. This means that it either ends '%' and the rest is an integer number;
139+
or it is a float number between 0.0 and 1.0.
140+
141+
:param strdata: the string to check
142+
143+
:return: A boolean, True if the string converts to a percentage, False otherwise
144+
"""
97145
if strdata != "":
98146
if strdata[-1] == "%" and is_int_str(strdata[:-1]):
99147
return True
100148

101149
if is_float_str(strdata):
102150
float_value = float(strdata)
103-
return 0 <= float_value <= 1
151+
return 0.0 <= float_value <= 1.0
104152
return False
105153

106154

107155
def parse_percentage_string(strdata: str) -> float:
156+
"""
157+
returns a percentage float value between 0.0 and 1.0 parsed from the input string.
158+
159+
Should be used in conjunction with is_percentage_string()
160+
161+
:param strdata: the string to parse
162+
163+
:return: a float between 0.0 and 1.0
164+
"""
108165
return float(strdata) if is_float_str(strdata) else float(strdata[:-1]) / 100
109166

110167

111168
def is_u8_string(strdata: str) -> bool:
169+
"""
170+
Checks if the string can be converted to an integer between 0 and 255.
171+
172+
:param strdata: the string to parse
173+
:return: a boolean - True if the string can convert to an integer between 0 and 255
174+
"""
112175
return is_int_str(strdata) and 0 <= int(strdata) <= 255
113176

114177

115178
def parse_u8_string(strdata: str) -> int:
179+
"""
180+
Parse the input string to an integer (between 0 and 255).
181+
182+
Should be used in conjunction with is_u8_string()
183+
184+
:param strdata: the string to parse
185+
186+
:return: an integer between 0 and 255
187+
"""
116188
return int(strdata)
117189

118190

@@ -121,6 +193,11 @@ def parse_u8_string(strdata: str) -> int:
121193

122194

123195
class ColourValueParserData(TypedDict):
196+
"""
197+
A class that groups together parsers and validators for turning strings into numerical values for colours.
198+
199+
"""
200+
124201
validator: ColourValueValidator
125202
parser: ColourValueParser
126203

@@ -137,9 +214,9 @@ class ColourValueParserData(TypedDict):
137214
},
138215
NumParserType.FLOAT: {
139216
"validator": is_float_str,
140-
"parser": lambda string: float(string),
217+
"parser": float,
141218
},
142-
NumParserType.INT: {"validator": is_int_str, "parser": lambda string: int(string)},
219+
NumParserType.INT: {"validator": is_int_str, "parser": int},
143220
}
144221
"""A mapping for each NumParserType to its corresponding validator and parser for strings of that type's
145222
specification"""
@@ -669,8 +746,7 @@ def get_commas_outside_enclosing_glyphs(strdata: str) -> List[int]:
669746
opening_stack = []
670747
comma_indices_outside_parentheses: List[int] = []
671748

672-
for i in range(len(strdata)):
673-
ch = strdata[i]
749+
for i, ch in enumerate(strdata):
674750
if ch == "," and not opening_stack:
675751
comma_indices_outside_parentheses.append(i)
676752
if ch in glyphs.values():
@@ -720,9 +796,9 @@ def split_string_at_indices(
720796

721797
splits = []
722798
last = 0
723-
for i in range(len(indices)):
724-
splits.append(strdata[last : indices[i]])
725-
last = indices[i] + 1
799+
for index in indices:
800+
splits.append(strdata[last:index])
801+
last = index + 1
726802
if last >= len(strdata):
727803
return splits
728804
splits.append(strdata[last:])

pygame_gui/core/drawable_shapes/drawable_shape.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ def apply_active_text_changes(self):
804804
805805
"""
806806
if self.text_box_layout is not None:
807-
for state_id, state in self.states.items():
807+
for _, state in self.states.items():
808808
if (
809809
state.pre_text_surface is not None
810810
and state.text_surface is not None

pygame_gui/core/drawable_shapes/ellipse_drawable_shape.py

+11-22
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,18 @@ def full_rebuild_on_size_change(self):
6060
"""
6161
super().full_rebuild_on_size_change()
6262
# clamping border and shadow widths so we can't form impossible negative sized surfaces
63-
if self.shadow_width > min(
63+
self.shadow_width = min(
64+
self.shadow_width,
6465
math.floor(self.containing_rect.width / 2),
6566
math.floor(self.containing_rect.height / 2),
66-
):
67-
self.shadow_width = min(
68-
math.floor(self.containing_rect.width / 2),
69-
math.floor(self.containing_rect.height / 2),
70-
)
71-
67+
)
7268
self.shadow_width = max(self.shadow_width, 0)
7369

74-
if self.border_width > min(
75-
math.floor((self.containing_rect.width - (self.shadow_width * 2)) / 2),
76-
math.floor((self.containing_rect.height - (self.shadow_width * 2)) / 2),
77-
):
78-
self.border_width = min(
79-
math.floor((self.containing_rect.width - (self.shadow_width * 2)) / 2),
80-
math.floor((self.containing_rect.height - (self.shadow_width * 2)) / 2),
81-
)
82-
70+
self.border_width = min(
71+
self.border_width,
72+
math.floor((self.containing_rect.width - self.shadow_width * 2) / 2),
73+
math.floor((self.containing_rect.height - self.shadow_width * 2) / 2),
74+
)
8375
self.border_width = max(self.border_width, 0)
8476

8577
if self.shadow_width > 0:
@@ -202,9 +194,6 @@ def redraw_state(self, state_str: str, add_text: bool = True):
202194
else:
203195
border_colour_state_str = f"{state_str}_border"
204196
bg_colour_state_str = f"{state_str}_bg"
205-
text_colour_state_str = f"{state_str}_text"
206-
text_shadow_colour_state_str = f"{state_str}_text_shadow"
207-
image_state_str = f"{state_str}_image"
208197
border_overlap = 0
209198
if "border_overlap" in self.theming:
210199
border_overlap = self.theming["border_overlap"]
@@ -354,10 +343,10 @@ def redraw_state(self, state_str: str, add_text: bool = True):
354343
)
355344

356345
self.finalise_images_and_text(
357-
image_state_str,
346+
f"{state_str}_image",
358347
state_str,
359-
text_colour_state_str,
360-
text_shadow_colour_state_str,
348+
f"{state_str}_text",
349+
f"{state_str}_text_shadow",
361350
add_text,
362351
)
363352

pygame_gui/core/drawable_shapes/rect_drawable_shape.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,18 @@ def full_rebuild_on_size_change(self):
6565

6666
self.base_surface = None
6767

68-
if self.shadow_width > min(
68+
self.shadow_width = min(
69+
self.shadow_width,
6970
math.floor(self.containing_rect.width / 2),
7071
math.floor(self.containing_rect.height / 2),
71-
):
72-
self.shadow_width = min(
73-
math.floor(self.containing_rect.width / 2),
74-
math.floor(self.containing_rect.height / 2),
75-
)
76-
72+
)
7773
self.shadow_width = max(self.shadow_width, 0)
7874

79-
if self.border_width > min(
80-
math.floor((self.containing_rect.width - (self.shadow_width * 2)) / 2),
81-
math.floor((self.containing_rect.height - (self.shadow_width * 2)) / 2),
82-
):
83-
self.border_width = min(
84-
math.floor((self.containing_rect.width - (self.shadow_width * 2)) / 2),
85-
math.floor((self.containing_rect.height - (self.shadow_width * 2)) / 2),
86-
)
87-
75+
self.border_width = min(
76+
self.border_width,
77+
math.floor((self.containing_rect.width - self.shadow_width * 2) / 2),
78+
math.floor((self.containing_rect.height - self.shadow_width * 2) / 2),
79+
)
8880
self.border_width = max(self.border_width, 0)
8981

9082
if self.shadow_width > 0:

pygame_gui/core/gui_font_freetype.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1-
import pygame
2-
3-
from pygame_gui.core.interfaces.gui_font_interface import IGUIFontInterface
4-
from pygame.freetype import Font
51
from typing import Union, IO, Optional, Dict, Tuple
62
from os import PathLike
3+
4+
import pygame
75
from pygame import Color, Surface, Rect
6+
from pygame.freetype import Font
7+
8+
from pygame_gui.core.interfaces.gui_font_interface import IGUIFontInterface
89

910
AnyPath = Union[str, bytes, PathLike]
1011
FileArg = Union[AnyPath, IO]
1112

1213

1314
class GUIFontFreetype(IGUIFontInterface):
15+
"""
16+
A GUI Font wrapping the 'freetype' Font class from pygame-ce to a common interface with the SDL based Font class
17+
also from pygame-ce. This was to facilitate easy switching between the two when testing features.
18+
19+
NB: At this point the SDL based Font class seems clearly superior so this is legacy code that will likely be
20+
removed in a future version
21+
22+
:param file: the font file
23+
:param size: the font point size
24+
:param force_style: whether we force the styling when the available font does not support it.
25+
:param style: a style dictionary to set styling parameters like bold and italic
26+
"""
27+
1428
def __init__(
1529
self,
1630
file: Optional[FileArg],
@@ -23,8 +37,8 @@ def __init__(
2337
self.__internal_font.origin = True
2438
self.__internal_font.kerning = True
2539

26-
self.__underline = False
27-
self.__underline_adjustment = 0.0
40+
self.__underline = False # pylint: disable=unused-private-member
41+
self.__underline_adjustment = 0.0 # pylint: disable=unused-private-member
2842

2943
self.point_size = size
3044

@@ -54,21 +68,19 @@ def underline_adjustment(self) -> float:
5468
def underline_adjustment(self, value: float):
5569
self.__internal_font.underline_adjustment = value
5670

57-
def get_point_size(self):
71+
def get_point_size(self) -> int:
5872
return self.point_size
5973

6074
def get_rect(self, text: str) -> Rect:
6175
supposed_rect = self.__internal_font.get_rect(text)
62-
text_surface, text_rect = self.__internal_font.render(
63-
text, pygame.Color("white")
64-
)
76+
text_surface, _ = self.__internal_font.render(text, pygame.Color("white"))
6577
return pygame.Rect(supposed_rect.topleft, text_surface.get_size())
6678

6779
def get_metrics(self, text: str):
6880
return self.__internal_font.get_metrics(text)
6981

7082
def render_premul(self, text: str, text_color: Color) -> Surface:
71-
text_surface, text_rect = self.__internal_font.render(text, text_color)
83+
text_surface, _ = self.__internal_font.render(text, text_color)
7284
text_surface = text_surface.convert_alpha()
7385
if text_surface.get_width() > 0 and text_surface.get_height() > 0:
7486
text_surface = text_surface.premul_alpha()
@@ -89,7 +101,7 @@ def render_premul_to(
89101
text_surface = text_surface.premul_alpha()
90102
return text_surface
91103

92-
def get_padding_height(self):
104+
def get_padding_height(self) -> int:
93105
# 'font padding' this determines the amount of padding that
94106
# font.pad adds to the top of text excluding
95107
# any padding added to make glyphs even - this is useful

0 commit comments

Comments
 (0)