Skip to content

Commit bf66594

Browse files
authored
Merge pull request #2782 from cclauss/auto-walrus
PEP572 Use Python's assignment expression
2 parents 8e9eed7 + b72fee7 commit bf66594

File tree

7 files changed

+13
-27
lines changed

7 files changed

+13
-27
lines changed

buildconfig/msysio.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ def print_(*args, **kwds):
2121

2222
stream = kwds.get('file', sys.stdout)
2323
sep = kwds.get('sep', ' ')
24-
end = kwds.get('end', '\n')
2524

2625
if args:
2726
stream.write(sep.join([str(arg) for arg in args]))
28-
if end:
27+
if end := kwds.get('end', '\n'):
2928
stream.write(end)
3029
try:
3130
stream.flush()

src_py/sprite.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,7 @@ def remove_internal(self, sprite):
410410
411411
:param sprite: The sprite we are removing.
412412
"""
413-
lost_rect = self.spritedict[sprite]
414-
if lost_rect:
413+
if lost_rect := self.spritedict[sprite]:
415414
self.lostsprites.append(lost_rect)
416415
del self.spritedict[sprite]
417416

src_py/sysfont.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ def _font_finder_darwin():
162162
"/System/Library/Fonts/Supplemental",
163163
]
164164

165-
username = os.getenv("USER")
166-
if username:
165+
if username := os.getenv("USER"):
167166
locations.append(f"/Users/{username}/Library/Fonts")
168167

169168
strange_root = "/System/Library/Assets/com_apple_MobileAsset_Font3"

test/controller_test.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,19 @@ def _get_first_controller(self):
7777
return controller.Controller(i)
7878

7979
def test_construction(self):
80-
c = self._get_first_controller()
81-
if c:
80+
if c := self._get_first_controller():
8281
self.assertIsInstance(c, controller.Controller)
8382
else:
8483
self.skipTest("No controller connected")
8584

8685
def test__auto_init(self):
87-
c = self._get_first_controller()
88-
if c:
86+
if c := self._get_first_controller():
8987
self.assertTrue(c.get_init())
9088
else:
9189
self.skipTest("No controller connected")
9290

9391
def test_get_init(self):
94-
c = self._get_first_controller()
95-
if c:
92+
if c := self._get_first_controller():
9693
self.assertTrue(c.get_init())
9794
c.quit()
9895
self.assertFalse(c.get_init())
@@ -111,25 +108,22 @@ def test_from_joystick(self):
111108
self.assertIsInstance(c, controller.Controller)
112109

113110
def test_as_joystick(self):
114-
c = self._get_first_controller()
115-
if c:
111+
if c := self._get_first_controller():
116112
joy = c.as_joystick()
117113
self.assertIsInstance(joy, type(pygame.joystick.Joystick(0)))
118114
else:
119115
self.skipTest("No controller connected")
120116

121117
def test_get_mapping(self):
122-
c = self._get_first_controller()
123-
if c:
118+
if c := self._get_first_controller():
124119
mapping = c.get_mapping()
125120
self.assertIsInstance(mapping, dict)
126121
self.assertIsNotNone(mapping["a"])
127122
else:
128123
self.skipTest("No controller connected")
129124

130125
def test_set_mapping(self):
131-
c = self._get_first_controller()
132-
if c:
126+
if c := self._get_first_controller():
133127
mapping = c.get_mapping()
134128
mapping["a"] = "b3"
135129
mapping["y"] = "b0"

test/mask_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def random_mask(size=(100, 100)):
2525

2626
def maskFromSurface(surface, threshold=127):
2727
mask = pygame.Mask(surface.get_size())
28-
key = surface.get_colorkey()
29-
if key:
28+
if key := surface.get_colorkey():
3029
for y in range(surface.get_height()):
3130
for x in range(surface.get_width()):
3231
if surface.get_at((x + 0.1, y + 0.1)) != key:

test/mixer_test.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ def test_set_soundfont(self):
163163
mixer.init()
164164

165165
# test that initially, get_soundfont returns only real files
166-
initial_sf = mixer.get_soundfont()
167-
if initial_sf is not None:
166+
if (initial_sf := mixer.get_soundfont()) is not None:
168167
for i in initial_sf.split(";"):
169168
os.path.exists(i)
170169

@@ -552,9 +551,7 @@ def test_find_channel(self):
552551
filename = example_path(os.path.join("data", "house_lo.wav"))
553552
sound = mixer.Sound(file=filename)
554553

555-
num_channels = mixer.get_num_channels()
556-
557-
if num_channels > 0:
554+
if (num_channels := mixer.get_num_channels()) > 0:
558555
found_channel = mixer.find_channel()
559556
self.assertIsNotNone(found_channel)
560557

test/test_utils/test_runner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ def combine_results(all_results, t):
240240

241241

242242
def get_test_results(raw_return):
243-
test_results = TEST_RESULTS_RE.search(raw_return)
244-
if test_results:
243+
if test_results := TEST_RESULTS_RE.search(raw_return):
245244
try:
246245
return eval(test_results.group(1))
247246
except:

0 commit comments

Comments
 (0)