Skip to content

Commit 1ac532b

Browse files
authored
Clean up old type usage (#2280)
* Clean up old type usage * Stop pytest after 10 failed tests * Missing future annotation import * derp * Fix 3.9 typing * More typing fixes * missing future import * future annotations imports * physics_engines * format * Fix old Optional typing * Update old typing * More typing cleanup * More type modernization
1 parent 6e8259d commit 1ac532b

Some content is hidden

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

80 files changed

+633
-665
lines changed

.github/workflows/selfhosted_runner.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
which python
4646
python -c "import pyglet; print('pyglet version', pyglet.__version__)"
4747
python -c "import PIL; print('Pillow version', PIL.__version__)"
48-
pytest --maxfail=1
48+
pytest --maxfail=10
4949
5050
# Prepare the Pull Request Payload artifact. If this fails, we
5151
# we fail silently using the `continue-on-error` option. It's

arcade/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
# Error out if we import Arcade with an incompatible version of Python.
1111
import sys
1212
import os
13-
from typing import Final, Optional
13+
from typing import Final
1414

1515
from pathlib import Path
1616

1717
if sys.version_info[0] < 3 or (sys.version_info[0] == 3 and sys.version_info[1] < 9):
1818
sys.exit("The Arcade Library requires Python 3.9 or higher.")
1919

2020

21-
def configure_logging(level: Optional[int] = None):
21+
def configure_logging(level: int | None = None):
2222
"""Set up basic logging.
2323
:param level: The log level. Defaults to DEBUG.
2424
"""

arcade/application.py

+43-43
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import logging
99
import os
1010
import time
11-
from typing import TYPE_CHECKING, Optional
11+
from typing import TYPE_CHECKING
1212

1313
import pyglet
1414
import pyglet.gl as gl
@@ -260,13 +260,13 @@ def __init__(
260260
self._ctx: ArcadeContext = ArcadeContext(self, gc_mode=gc_mode, gl_api=gl_api)
261261
self._background_color: Color = TRANSPARENT_BLACK
262262

263-
self._current_view: Optional[View] = None
263+
self._current_view: View | None = None
264264

265265
# See if we should center the window
266266
if center_window:
267267
self.center_window()
268268

269-
self.keyboard: Optional[pyglet.window.key.KeyStateHandler] = None
269+
self.keyboard: pyglet.window.key.KeyStateHandler | None = None
270270
"""
271271
A pyglet KeyStateHandler that can be used to poll the state of the keyboard.
272272
@@ -275,7 +275,7 @@ def __init__(
275275
if self.window.keyboard[key.SPACE]:
276276
print("The space key is currently being held down.")
277277
"""
278-
self.mouse: Optional[pyglet.window.mouse.MouseStateHandler] = None
278+
self.mouse: pyglet.window.mouse.MouseStateHandler | None = None
279279
"""
280280
A pyglet MouseStateHandler that can be used to poll the state of the mouse.
281281
@@ -306,10 +306,10 @@ def __init__(
306306
# These are typically functions just at module level wrapped in
307307
# start_render and finish_render calls. The framebuffer is repeatedly
308308
# rendered to the window when the event loop starts.
309-
self._start_finish_render_data: Optional[StartFinishRenderData] = None
309+
self._start_finish_render_data: StartFinishRenderData | None = None
310310

311311
@property
312-
def current_view(self) -> Optional["View"]:
312+
def current_view(self) -> View | None:
313313
"""
314314
The currently active view.
315315
@@ -330,9 +330,9 @@ def ctx(self) -> ArcadeContext:
330330

331331
def clear(
332332
self,
333-
color: Optional[RGBOrA255] = None,
334-
color_normalized: Optional[RGBANormalized] = None,
335-
viewport: Optional[tuple[int, int, int, int]] = None,
333+
color: RGBOrA255 | None = None,
334+
color_normalized: RGBANormalized | None = None,
335+
viewport: tuple[int, int, int, int] | None = None,
336336
) -> None:
337337
"""
338338
Clears the window with the configured background color
@@ -461,7 +461,7 @@ def center_window(self) -> None:
461461
# Center the window
462462
self.set_location((screen_width - window_width) // 2, (screen_height - window_height) // 2)
463463

464-
def on_update(self, delta_time: float) -> Optional[bool]:
464+
def on_update(self, delta_time: float) -> bool | None:
465465
"""
466466
This method can be implemented and is reserved for game logic.
467467
Move sprites. Perform collision checks and other game logic.
@@ -535,7 +535,7 @@ def set_draw_rate(self, rate: float) -> None:
535535
pyglet.clock.unschedule(pyglet.app.event_loop._redraw_windows)
536536
pyglet.clock.schedule_interval(pyglet.app.event_loop._redraw_windows, self._draw_rate)
537537

538-
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> Optional[bool]:
538+
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> bool | None:
539539
"""
540540
Called repeatedly while the mouse is moving in the window area.
541541
@@ -549,7 +549,7 @@ def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> Optional[bool]:
549549
"""
550550
pass
551551

552-
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> Optional[bool]:
552+
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
553553
"""
554554
Called once whenever a mouse button gets pressed down.
555555
@@ -574,7 +574,7 @@ def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> Optiona
574574

575575
def on_mouse_drag(
576576
self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int
577-
) -> Optional[bool]:
577+
) -> bool | None:
578578
"""
579579
Called repeatedly while the mouse moves with a button down.
580580
@@ -591,7 +591,7 @@ def on_mouse_drag(
591591
"""
592592
return self.on_mouse_motion(x, y, dx, dy)
593593

594-
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> Optional[bool]:
594+
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
595595
"""
596596
Called once whenever a mouse button gets released.
597597
@@ -613,7 +613,7 @@ def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> Optio
613613
"""
614614
return False
615615

616-
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> Optional[bool]:
616+
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> bool | None:
617617
"""
618618
Called repeatedly while a mouse scroll wheel moves.
619619
@@ -689,7 +689,7 @@ def on_action(self, action_name: str, state) -> None:
689689
"""
690690
pass
691691

692-
def on_key_press(self, symbol: int, modifiers: int) -> Optional[bool]:
692+
def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
693693
"""
694694
Called once when a key gets pushed down.
695695
@@ -707,7 +707,7 @@ def on_key_press(self, symbol: int, modifiers: int) -> Optional[bool]:
707707
"""
708708
return False
709709

710-
def on_key_release(self, symbol: int, modifiers: int) -> Optional[bool]:
710+
def on_key_release(self, symbol: int, modifiers: int) -> bool | None:
711711
"""
712712
Called once when a key gets released.
713713
@@ -729,7 +729,7 @@ def on_key_release(self, symbol: int, modifiers: int) -> Optional[bool]:
729729
"""
730730
return False
731731

732-
def on_draw(self) -> Optional[bool]:
732+
def on_draw(self) -> bool | None:
733733
"""
734734
Override this function to add your custom drawing code.
735735
@@ -747,7 +747,7 @@ def on_draw(self) -> Optional[bool]:
747747

748748
return False
749749

750-
def _on_resize(self, width: int, height: int) -> Optional[bool]:
750+
def _on_resize(self, width: int, height: int) -> bool | None:
751751
"""
752752
The internal method called when the window is resized.
753753
@@ -765,7 +765,7 @@ def _on_resize(self, width: int, height: int) -> Optional[bool]:
765765

766766
return False
767767

768-
def on_resize(self, width: int, height: int) -> Optional[bool]:
768+
def on_resize(self, width: int, height: int) -> bool | None:
769769
"""
770770
Override this method to add custom actions when the window is resized.
771771
@@ -1087,7 +1087,7 @@ def dispatch_events(self) -> None:
10871087
"""Dispatch events"""
10881088
super().dispatch_events()
10891089

1090-
def on_mouse_enter(self, x: int, y: int) -> Optional[bool]:
1090+
def on_mouse_enter(self, x: int, y: int) -> bool | None:
10911091
"""
10921092
Called once whenever the mouse enters the window area on screen.
10931093
@@ -1100,7 +1100,7 @@ def on_mouse_enter(self, x: int, y: int) -> Optional[bool]:
11001100
"""
11011101
pass
11021102

1103-
def on_mouse_leave(self, x: int, y: int) -> Optional[bool]:
1103+
def on_mouse_leave(self, x: int, y: int) -> bool | None:
11041104
"""
11051105
Called once whenever the mouse leaves the window area on screen.
11061106
@@ -1175,7 +1175,7 @@ def fixed_delta_time(self) -> float:
11751175
def open_window(
11761176
width: int,
11771177
height: int,
1178-
window_title: Optional[str] = None,
1178+
window_title: str | None = None,
11791179
resizable: bool = False,
11801180
antialiasing: bool = True,
11811181
) -> Window:
@@ -1214,10 +1214,10 @@ class View:
12141214
the current window is used. (Normally you don't need to provide this).
12151215
"""
12161216

1217-
def __init__(self, window: Optional[Window] = None) -> None:
1217+
def __init__(self, window: Window | None = None) -> None:
12181218
self.window = arcade.get_window() if window is None else window
1219-
self.key: Optional[int] = None
1220-
self._section_manager: Optional[SectionManager] = None
1219+
self.key: int | None = None
1220+
self._section_manager: SectionManager | None = None
12211221

12221222
@property
12231223
def section_manager(self) -> SectionManager:
@@ -1240,8 +1240,8 @@ def has_sections(self) -> bool:
12401240
def add_section(
12411241
self,
12421242
section: arcade.Section,
1243-
at_index: Optional[int] = None,
1244-
at_draw_order: Optional[int] = None,
1243+
at_index: int | None = None,
1244+
at_draw_order: int | None = None,
12451245
) -> None:
12461246
"""
12471247
Adds a section to the view Section Manager.
@@ -1257,9 +1257,9 @@ def add_section(
12571257

12581258
def clear(
12591259
self,
1260-
color: Optional[RGBOrA255] = None,
1261-
color_normalized: Optional[RGBANormalized] = None,
1262-
viewport: Optional[tuple[int, int, int, int]] = None,
1260+
color: RGBOrA255 | None = None,
1261+
color_normalized: RGBANormalized | None = None,
1262+
viewport: tuple[int, int, int, int] | None = None,
12631263
) -> None:
12641264
"""
12651265
Clears the window with the configured background color
@@ -1279,7 +1279,7 @@ def clear(
12791279
"""
12801280
self.window.clear(color=color, color_normalized=color_normalized, viewport=viewport)
12811281

1282-
def on_update(self, delta_time: float) -> Optional[bool]:
1282+
def on_update(self, delta_time: float) -> bool | None:
12831283
"""
12841284
This method can be implemented and is reserved for game logic.
12851285
Move sprites. Perform collision checks and other game logic.
@@ -1305,7 +1305,7 @@ def on_fixed_update(self, delta_time: float):
13051305
"""
13061306
pass
13071307

1308-
def on_draw(self) -> Optional[bool]:
1308+
def on_draw(self) -> bool | None:
13091309
"""
13101310
Override this function to add your custom drawing code.
13111311
@@ -1329,7 +1329,7 @@ def on_hide_view(self) -> None:
13291329
"""Called once when this view is hidden."""
13301330
pass
13311331

1332-
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> Optional[bool]:
1332+
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> bool | None:
13331333
"""
13341334
Called repeatedly while the mouse is moving in the window area.
13351335
@@ -1343,7 +1343,7 @@ def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> Optional[bool]:
13431343
"""
13441344
pass
13451345

1346-
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> Optional[bool]:
1346+
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
13471347
"""
13481348
Called once whenever a mouse button gets pressed down.
13491349
@@ -1368,7 +1368,7 @@ def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> Optiona
13681368

13691369
def on_mouse_drag(
13701370
self, x: int, y: int, dx: int, dy: int, _buttons: int, _modifiers: int
1371-
) -> Optional[bool]:
1371+
) -> bool | None:
13721372
"""
13731373
Called repeatedly while the mouse moves with a button down.
13741374
@@ -1386,7 +1386,7 @@ def on_mouse_drag(
13861386
self.on_mouse_motion(x, y, dx, dy)
13871387
return False
13881388

1389-
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> Optional[bool]:
1389+
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
13901390
"""
13911391
Called once whenever a mouse button gets released.
13921392
@@ -1408,7 +1408,7 @@ def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> Optio
14081408
"""
14091409
pass
14101410

1411-
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> Optional[bool]:
1411+
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> bool | None:
14121412
"""
14131413
Called repeatedly while a mouse scroll wheel moves.
14141414
@@ -1440,7 +1440,7 @@ def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> Optio
14401440
"""
14411441
pass
14421442

1443-
def on_key_press(self, symbol: int, modifiers: int) -> Optional[bool]:
1443+
def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
14441444
"""
14451445
Called once when a key gets pushed down.
14461446
@@ -1458,7 +1458,7 @@ def on_key_press(self, symbol: int, modifiers: int) -> Optional[bool]:
14581458
"""
14591459
return False
14601460

1461-
def on_key_release(self, _symbol: int, _modifiers: int) -> Optional[bool]:
1461+
def on_key_release(self, _symbol: int, _modifiers: int) -> bool | None:
14621462
"""
14631463
Called once when a key gets released.
14641464
@@ -1480,7 +1480,7 @@ def on_key_release(self, _symbol: int, _modifiers: int) -> Optional[bool]:
14801480
"""
14811481
return False
14821482

1483-
def on_resize(self, width: int, height: int) -> Optional[bool]:
1483+
def on_resize(self, width: int, height: int) -> bool | None:
14841484
"""
14851485
Override this method to add custom actions when the window is resized.
14861486
@@ -1494,7 +1494,7 @@ def on_resize(self, width: int, height: int) -> Optional[bool]:
14941494
"""
14951495
pass
14961496

1497-
def on_mouse_enter(self, x: int, y: int) -> Optional[bool]:
1497+
def on_mouse_enter(self, x: int, y: int) -> bool | None:
14981498
"""
14991499
Called once whenever the mouse enters the window area on screen.
15001500
@@ -1507,7 +1507,7 @@ def on_mouse_enter(self, x: int, y: int) -> Optional[bool]:
15071507
"""
15081508
pass
15091509

1510-
def on_mouse_leave(self, x: int, y: int) -> Optional[bool]:
1510+
def on_mouse_leave(self, x: int, y: int) -> bool | None:
15111511
"""
15121512
Called once whenever the mouse leaves the window area on screen.
15131513

arcade/cache/hit_box.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import json
1616
from collections import OrderedDict
1717
from pathlib import Path
18-
from typing import TYPE_CHECKING, Optional, Union
18+
from typing import TYPE_CHECKING
1919

2020
from arcade.resources import resolve
2121
from arcade.types import Point2List
@@ -46,7 +46,7 @@ def __len__(self) -> int:
4646
def __iter__(self):
4747
return iter(self._entries)
4848

49-
def get(self, name_or_texture: Union[str, "Texture"]) -> Optional[Point2List]:
49+
def get(self, name_or_texture: str | Texture) -> Point2List | None:
5050
"""
5151
Get the hit box points for a texture with a given hash
5252
and hit box algorithm.
@@ -70,7 +70,7 @@ def get(self, name_or_texture: Union[str, "Texture"]) -> Optional[Point2List]:
7070
else:
7171
raise TypeError(f"Expected str or Texture: {name_or_texture}")
7272

73-
def put(self, name_or_texture: Union[str, "Texture"], points: Point2List) -> None:
73+
def put(self, name_or_texture: str | Texture, points: Point2List) -> None:
7474
"""
7575
Store hit box points for a texture.
7676
@@ -97,7 +97,7 @@ def put(self, name_or_texture: Union[str, "Texture"], points: Point2List) -> Non
9797
else:
9898
raise TypeError(f"Expected str or Texture: {name_or_texture}")
9999

100-
def load(self, path: Union[str, Path]) -> None:
100+
def load(self, path: str | Path) -> None:
101101
"""
102102
Load a json file containing hit boxes.
103103

0 commit comments

Comments
 (0)