Skip to content

Commit

Permalink
Clean up old type usage (pythonarcade#2280)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
einarf authored Jul 18, 2024
1 parent 6e8259d commit 1ac532b
Show file tree
Hide file tree
Showing 80 changed files with 633 additions and 665 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/selfhosted_runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
which python
python -c "import pyglet; print('pyglet version', pyglet.__version__)"
python -c "import PIL; print('Pillow version', PIL.__version__)"
pytest --maxfail=1
pytest --maxfail=10
# Prepare the Pull Request Payload artifact. If this fails, we
# we fail silently using the `continue-on-error` option. It's
Expand Down
4 changes: 2 additions & 2 deletions arcade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
# Error out if we import Arcade with an incompatible version of Python.
import sys
import os
from typing import Final, Optional
from typing import Final

from pathlib import Path

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


def configure_logging(level: Optional[int] = None):
def configure_logging(level: int | None = None):
"""Set up basic logging.
:param level: The log level. Defaults to DEBUG.
"""
Expand Down
86 changes: 43 additions & 43 deletions arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import os
import time
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING

import pyglet
import pyglet.gl as gl
Expand Down Expand Up @@ -260,13 +260,13 @@ def __init__(
self._ctx: ArcadeContext = ArcadeContext(self, gc_mode=gc_mode, gl_api=gl_api)
self._background_color: Color = TRANSPARENT_BLACK

self._current_view: Optional[View] = None
self._current_view: View | None = None

# See if we should center the window
if center_window:
self.center_window()

self.keyboard: Optional[pyglet.window.key.KeyStateHandler] = None
self.keyboard: pyglet.window.key.KeyStateHandler | None = None
"""
A pyglet KeyStateHandler that can be used to poll the state of the keyboard.
Expand All @@ -275,7 +275,7 @@ def __init__(
if self.window.keyboard[key.SPACE]:
print("The space key is currently being held down.")
"""
self.mouse: Optional[pyglet.window.mouse.MouseStateHandler] = None
self.mouse: pyglet.window.mouse.MouseStateHandler | None = None
"""
A pyglet MouseStateHandler that can be used to poll the state of the mouse.
Expand Down Expand Up @@ -306,10 +306,10 @@ def __init__(
# These are typically functions just at module level wrapped in
# start_render and finish_render calls. The framebuffer is repeatedly
# rendered to the window when the event loop starts.
self._start_finish_render_data: Optional[StartFinishRenderData] = None
self._start_finish_render_data: StartFinishRenderData | None = None

@property
def current_view(self) -> Optional["View"]:
def current_view(self) -> View | None:
"""
The currently active view.
Expand All @@ -330,9 +330,9 @@ def ctx(self) -> ArcadeContext:

def clear(
self,
color: Optional[RGBOrA255] = None,
color_normalized: Optional[RGBANormalized] = None,
viewport: Optional[tuple[int, int, int, int]] = None,
color: RGBOrA255 | None = None,
color_normalized: RGBANormalized | None = None,
viewport: tuple[int, int, int, int] | None = None,
) -> None:
"""
Clears the window with the configured background color
Expand Down Expand Up @@ -461,7 +461,7 @@ def center_window(self) -> None:
# Center the window
self.set_location((screen_width - window_width) // 2, (screen_height - window_height) // 2)

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

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

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

def on_mouse_drag(
self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int
) -> Optional[bool]:
) -> bool | None:
"""
Called repeatedly while the mouse moves with a button down.
Expand All @@ -591,7 +591,7 @@ def on_mouse_drag(
"""
return self.on_mouse_motion(x, y, dx, dy)

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

def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> Optional[bool]:
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> bool | None:
"""
Called repeatedly while a mouse scroll wheel moves.
Expand Down Expand Up @@ -689,7 +689,7 @@ def on_action(self, action_name: str, state) -> None:
"""
pass

def on_key_press(self, symbol: int, modifiers: int) -> Optional[bool]:
def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
"""
Called once when a key gets pushed down.
Expand All @@ -707,7 +707,7 @@ def on_key_press(self, symbol: int, modifiers: int) -> Optional[bool]:
"""
return False

def on_key_release(self, symbol: int, modifiers: int) -> Optional[bool]:
def on_key_release(self, symbol: int, modifiers: int) -> bool | None:
"""
Called once when a key gets released.
Expand All @@ -729,7 +729,7 @@ def on_key_release(self, symbol: int, modifiers: int) -> Optional[bool]:
"""
return False

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

return False

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

return False

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

def on_mouse_enter(self, x: int, y: int) -> Optional[bool]:
def on_mouse_enter(self, x: int, y: int) -> bool | None:
"""
Called once whenever the mouse enters the window area on screen.
Expand All @@ -1100,7 +1100,7 @@ def on_mouse_enter(self, x: int, y: int) -> Optional[bool]:
"""
pass

def on_mouse_leave(self, x: int, y: int) -> Optional[bool]:
def on_mouse_leave(self, x: int, y: int) -> bool | None:
"""
Called once whenever the mouse leaves the window area on screen.
Expand Down Expand Up @@ -1175,7 +1175,7 @@ def fixed_delta_time(self) -> float:
def open_window(
width: int,
height: int,
window_title: Optional[str] = None,
window_title: str | None = None,
resizable: bool = False,
antialiasing: bool = True,
) -> Window:
Expand Down Expand Up @@ -1214,10 +1214,10 @@ class View:
the current window is used. (Normally you don't need to provide this).
"""

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

@property
def section_manager(self) -> SectionManager:
Expand All @@ -1240,8 +1240,8 @@ def has_sections(self) -> bool:
def add_section(
self,
section: arcade.Section,
at_index: Optional[int] = None,
at_draw_order: Optional[int] = None,
at_index: int | None = None,
at_draw_order: int | None = None,
) -> None:
"""
Adds a section to the view Section Manager.
Expand All @@ -1257,9 +1257,9 @@ def add_section(

def clear(
self,
color: Optional[RGBOrA255] = None,
color_normalized: Optional[RGBANormalized] = None,
viewport: Optional[tuple[int, int, int, int]] = None,
color: RGBOrA255 | None = None,
color_normalized: RGBANormalized | None = None,
viewport: tuple[int, int, int, int] | None = None,
) -> None:
"""
Clears the window with the configured background color
Expand All @@ -1279,7 +1279,7 @@ def clear(
"""
self.window.clear(color=color, color_normalized=color_normalized, viewport=viewport)

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

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

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

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

def on_mouse_drag(
self, x: int, y: int, dx: int, dy: int, _buttons: int, _modifiers: int
) -> Optional[bool]:
) -> bool | None:
"""
Called repeatedly while the mouse moves with a button down.
Expand All @@ -1386,7 +1386,7 @@ def on_mouse_drag(
self.on_mouse_motion(x, y, dx, dy)
return False

def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> Optional[bool]:
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
"""
Called once whenever a mouse button gets released.
Expand All @@ -1408,7 +1408,7 @@ def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> Optio
"""
pass

def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> Optional[bool]:
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> bool | None:
"""
Called repeatedly while a mouse scroll wheel moves.
Expand Down Expand Up @@ -1440,7 +1440,7 @@ def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> Optio
"""
pass

def on_key_press(self, symbol: int, modifiers: int) -> Optional[bool]:
def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
"""
Called once when a key gets pushed down.
Expand All @@ -1458,7 +1458,7 @@ def on_key_press(self, symbol: int, modifiers: int) -> Optional[bool]:
"""
return False

def on_key_release(self, _symbol: int, _modifiers: int) -> Optional[bool]:
def on_key_release(self, _symbol: int, _modifiers: int) -> bool | None:
"""
Called once when a key gets released.
Expand All @@ -1480,7 +1480,7 @@ def on_key_release(self, _symbol: int, _modifiers: int) -> Optional[bool]:
"""
return False

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

def on_mouse_enter(self, x: int, y: int) -> Optional[bool]:
def on_mouse_enter(self, x: int, y: int) -> bool | None:
"""
Called once whenever the mouse enters the window area on screen.
Expand All @@ -1507,7 +1507,7 @@ def on_mouse_enter(self, x: int, y: int) -> Optional[bool]:
"""
pass

def on_mouse_leave(self, x: int, y: int) -> Optional[bool]:
def on_mouse_leave(self, x: int, y: int) -> bool | None:
"""
Called once whenever the mouse leaves the window area on screen.
Expand Down
8 changes: 4 additions & 4 deletions arcade/cache/hit_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import json
from collections import OrderedDict
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Union
from typing import TYPE_CHECKING

from arcade.resources import resolve
from arcade.types import Point2List
Expand Down Expand Up @@ -46,7 +46,7 @@ def __len__(self) -> int:
def __iter__(self):
return iter(self._entries)

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

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

def load(self, path: Union[str, Path]) -> None:
def load(self, path: str | Path) -> None:
"""
Load a json file containing hit boxes.
Expand Down
Loading

0 comments on commit 1ac532b

Please sign in to comment.