Skip to content

fix type issues #2489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 45 additions & 34 deletions arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
import logging
import os
import time
from typing import TYPE_CHECKING
from typing import Sequence, TYPE_CHECKING

import pyglet
import pyglet.gl as gl
import pyglet.window.mouse
from pyglet.display.base import Screen, ScreenMode
from pyglet.event import EVENT_HANDLE_STATE, EVENT_UNHANDLED
from pyglet.window import MouseCursor

import arcade
from arcade.clock import GLOBAL_CLOCK, GLOBAL_FIXED_CLOCK, _setup_clock, _setup_fixed_clock
from arcade.color import BLACK
from arcade.context import ArcadeContext
from arcade.types import LBWH, Color, Rect, RGBANormalized, RGBOrA255
from arcade.types import Color, LBWH, RGBANormalized, RGBOrA255, Rect
from arcade.utils import is_raspberry_pi
from arcade.window_commands import get_display_size, set_window

Expand All @@ -29,7 +30,6 @@
from arcade.camera.default import DefaultProjector
from arcade.start_finish_data import StartFinishRenderData


LOG = logging.getLogger(__name__)

MOUSE_BUTTON_LEFT = 1
Expand Down Expand Up @@ -180,7 +180,7 @@ def __init__(
# Attempt to make window with antialiasing
if antialiasing:
try:
config = pyglet.gl.Config(
config = gl.Config(
major_version=gl_version[0],
minor_version=gl_version[1],
opengl_api=gl_api, # type: ignore # pending: upstream fix
Expand All @@ -204,7 +204,7 @@ def __init__(
antialiasing = False
# If we still don't have a config
if not config:
config = pyglet.gl.Config(
config = gl.Config(
major_version=gl_version[0],
minor_version=gl_version[1],
opengl_api=gl_api, # type: ignore # pending: upstream fix
Expand Down Expand Up @@ -239,7 +239,7 @@ def __init__(
if antialiasing:
try:
gl.glEnable(gl.GL_MULTISAMPLE_ARB)
except pyglet.gl.GLException:
except gl.GLException:
LOG.warning("Warning: Anti-aliasing not supported on this computer.")

_setup_clock()
Expand Down Expand Up @@ -338,7 +338,7 @@ def ctx(self) -> ArcadeContext:
"""
return self._ctx

def clear(
def clear( # type: ignore # not sure what to do here, BaseWindow.clear is static
self,
color: RGBOrA255 | None = None,
color_normalized: RGBANormalized | None = None,
Expand Down Expand Up @@ -554,7 +554,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) -> bool | None:
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> EVENT_HANDLE_STATE:
"""
Called repeatedly while the mouse is moving in the window area.

Expand All @@ -568,7 +568,7 @@ def on_mouse_motion(self, x: int, y: int, dx: int, dy: int) -> bool | None:
"""
pass

def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool | None:
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> EVENT_HANDLE_STATE:
"""
Called once whenever a mouse button gets pressed down.

Expand Down Expand Up @@ -596,7 +596,7 @@ def on_mouse_press(self, x: int, y: int, button: int, modifiers: int) -> bool |

def on_mouse_drag(
self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int
) -> bool | None:
) -> EVENT_HANDLE_STATE:
"""
Called repeatedly while the mouse moves with a button down.

Expand All @@ -619,7 +619,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) -> bool | None:
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> EVENT_HANDLE_STATE:
"""
Called once whenever a mouse button gets released.

Expand All @@ -642,9 +642,11 @@ def on_mouse_release(self, x: int, y: int, button: int, modifiers: int) -> bool
Bitwise 'and' of all modifiers (shift, ctrl, num lock)
active during this event. See :ref:`keyboard_modifiers`.
"""
return False
return EVENT_UNHANDLED

def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> bool | None:
def on_mouse_scroll(
self, x: int, y: int, scroll_x: float, scroll_y: float
) -> EVENT_HANDLE_STATE:
"""
Called repeatedly while a mouse scroll wheel moves.

Expand Down Expand Up @@ -676,7 +678,7 @@ def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> bool
scroll_y:
Number of steps scrolled vertically since the last call of this function
"""
return False
return EVENT_UNHANDLED

def set_mouse_visible(self, visible: bool = True) -> None:
"""
Expand Down Expand Up @@ -724,7 +726,7 @@ def on_action(self, action_name: str, state) -> None:
"""
pass

def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
def on_key_press(self, symbol: int, modifiers: int) -> EVENT_HANDLE_STATE:
"""
Called once when a key gets pushed down.

Expand All @@ -741,9 +743,9 @@ def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
Bitwise 'and' of all modifiers (shift, ctrl, num lock)
active during this event. See :ref:`keyboard_modifiers`.
"""
return False
return EVENT_UNHANDLED

def on_key_release(self, symbol: int, modifiers: int) -> bool | None:
def on_key_release(self, symbol: int, modifiers: int) -> EVENT_HANDLE_STATE:
"""
Called once when a key gets released.

Expand All @@ -763,9 +765,9 @@ def on_key_release(self, symbol: int, modifiers: int) -> bool | None:
ctrl, num lock) active during this event.
See :ref:`keyboard_modifiers`.
"""
return False
return EVENT_UNHANDLED

def on_draw(self) -> bool | None:
def on_draw(self) -> EVENT_HANDLE_STATE:
"""
Override this function to add your custom drawing code.

Expand All @@ -781,9 +783,9 @@ def on_draw(self) -> bool | None:
self._start_finish_render_data.draw()
return True

return False
return EVENT_UNHANDLED

def _on_resize(self, width: int, height: int) -> bool | None:
def _on_resize(self, width: int, height: int) -> EVENT_HANDLE_STATE:
"""
The internal method called when the window is resized.

Expand All @@ -799,9 +801,9 @@ def _on_resize(self, width: int, height: int) -> bool | None:
# Retain viewport
self.viewport = (0, 0, width, height)

return False
return EVENT_UNHANDLED

def on_resize(self, width: int, height: int) -> bool | None:
def on_resize(self, width: int, height: int) -> EVENT_HANDLE_STATE:
"""
Override this method to add custom actions when the window is resized.

Expand Down Expand Up @@ -855,7 +857,7 @@ def get_size(self) -> tuple[int, int]:

def get_location(self) -> tuple[int, int]:
"""Get the current X/Y coordinates of the window."""
return super().get_location()
return super().get_location() # type: ignore # Window typed at runtime

def set_visible(self, visible: bool = True):
"""
Expand Down Expand Up @@ -1038,34 +1040,34 @@ def flip(self) -> None:
num_collected = self.ctx.gc()
LOG.debug("Garbage collected %s OpenGL resource(s)", num_collected)

super().flip()
super().flip() # type: ignore # Window typed at runtime

def switch_to(self) -> None:
"""Switch the this window context.

This is normally only used in multi-window applications.
"""
super().switch_to()
super().switch_to() # type: ignore # Window typed at runtime

def set_caption(self, caption) -> None:
"""Set the caption/title of the window."""
super().set_caption(caption)
super().set_caption(caption) # type: ignore # Window typed at runtime

def set_location(self, x, y) -> None:
"""Set location of the window."""
super().set_location(x, y)
super().set_location(x, y) # type: ignore # Window typed at runtime

def activate(self) -> None:
"""Activate this window."""
super().activate()
super().activate() # type: ignore # Window typed at runtime

def minimize(self) -> None:
"""Minimize the window."""
super().minimize()
super().minimize() # type: ignore # Window typed at runtime

def maximize(self) -> None:
"""Maximize the window."""
super().maximize()
super().maximize() # type: ignore # Window typed at runtime

def set_vsync(self, vsync: bool) -> None:
"""Set if we sync our draws to the monitors vertical sync rate."""
Expand Down Expand Up @@ -1097,9 +1099,9 @@ def get_system_mouse_cursor(self, name) -> MouseCursor:

def dispatch_events(self) -> None:
"""Dispatch events"""
super().dispatch_events()
super().dispatch_events() # type: ignore # Window typed at runtime

def on_mouse_enter(self, x: int, y: int) -> bool | None:
def on_mouse_enter(self, x: int, y: int) -> EVENT_HANDLE_STATE:
"""
Called once whenever the mouse enters the window area on screen.

Expand All @@ -1112,7 +1114,7 @@ def on_mouse_enter(self, x: int, y: int) -> bool | None:
"""
pass

def on_mouse_leave(self, x: int, y: int) -> bool | None:
def on_mouse_leave(self, x: int, y: int) -> EVENT_HANDLE_STATE:
"""
Called once whenever the mouse leaves the window area on screen.

Expand Down Expand Up @@ -1183,6 +1185,15 @@ def fixed_delta_time(self) -> float:
"""The configured fixed update rate"""
return self._fixed_rate

# required because pyglet marks the method as abstract methods,
# but resolves class during runtime
def _create(self) -> None:
"""Internal method to create the window."""
super()._create() # type: ignore

def _recreate(self, changes: Sequence[str]) -> None:
super()._recreate(changes) # type: ignore


def open_window(
width: int,
Expand Down
30 changes: 16 additions & 14 deletions arcade/camera/camera_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

from contextlib import contextmanager
from math import atan2, cos, degrees, radians, sin
from typing import TYPE_CHECKING, Generator
from typing import Generator, TYPE_CHECKING

from pyglet.math import Vec2, Vec3
from typing_extensions import Self

from arcade.camera.data_types import (
CameraData,
DEFAULT_FAR,
DEFAULT_NEAR_ORTHO,
CameraData,
OrthographicProjectionData,
ZeroProjectionDimension,
)
Expand All @@ -20,7 +20,7 @@
project_orthographic,
unproject_orthographic,
)
from arcade.types import LBWH, LRBT, XYWH, Point, Rect
from arcade.types import LBWH, LRBT, Point, Rect, XYWH
from arcade.types.vector_like import Point2
from arcade.window_commands import get_window

Expand Down Expand Up @@ -544,10 +544,12 @@ def position(self) -> Vec2:
"""The 2D world position of the camera along the X and Y axes."""
return Vec2(self._camera_data.position[0], self._camera_data.position[1])

# Setter with different signature will cause mypy issues
# https://github.com/python/mypy/issues/3004
@position.setter
def position(self, _pos: Point) -> None:
x, y, *z = _pos
z = self._camera_data.position[2] if not z else z[0]
x, y, *_z = _pos
z = self._camera_data.position[2] if not _z else _z[0]
self._camera_data.position = (x, y, z)

@property
Expand Down Expand Up @@ -900,7 +902,7 @@ def top_left(self, new_corner: Point2):
left = self.left

x, y = new_corner
self.position = (x - ux * top - rx * left, y - uy * top - ry * left)
self.position = (x - ux * top - rx * left, y - uy * top - ry * left) # type: ignore

# top_center
@property
Expand All @@ -918,7 +920,7 @@ def top_center(self, new_top: Point2):
top = self.top

x, y = new_top
self.position = x - ux * top, y - uy * top
self.position = x - ux * top, y - uy * top # type: ignore

# top_right
@property
Expand All @@ -942,7 +944,7 @@ def top_right(self, new_corner: Point2):
right = self.right

x, y = new_corner
self.position = (x - ux * top - rx * right, y - uy * top - ry * right)
self.position = (x - ux * top - rx * right, y - uy * top - ry * right) # type: ignore

# center_right
@property
Expand All @@ -959,7 +961,7 @@ def center_right(self, new_right: Point2):
right = self.right

x, y = new_right
self.position = x - uy * right, y + ux * right
self.position = x - uy * right, y + ux * right # type: ignore

# bottom_right
@property
Expand All @@ -985,7 +987,7 @@ def bottom_right(self, new_corner: Point2):
self.position = (
x - ux * bottom - rx * right,
y - uy * bottom - ry * right,
)
) # type: ignore

# bottom_center
@property
Expand All @@ -995,15 +997,15 @@ def bottom_center(self) -> Vec2:
ux, uy, *_ = self._camera_data.up
bottom = self.bottom

return Vec2(pos.x + ux * bottom, pos.y + uy * bottom)
return pos.x + ux * bottom, pos.y + uy * bottom # type: ignore

@bottom_center.setter
def bottom_center(self, new_bottom: Point2):
ux, uy, *_ = self._camera_data.up
bottom = self.bottom

x, y = new_bottom
self.position = x - ux * bottom, y - uy * bottom
self.position = x - ux * bottom, y - uy * bottom # type: ignore

# bottom_left
@property
Expand All @@ -1027,7 +1029,7 @@ def bottom_left(self, new_corner: Point2):
left = self.left

x, y = new_corner
self.position = (x - ux * bottom - rx * left, y - uy * bottom - ry * left)
self.position = x - ux * bottom - rx * left, y - uy * bottom - ry * left # type: ignore

# center_left
@property
Expand All @@ -1044,4 +1046,4 @@ def center_left(self, new_left: Point2):
left = self.left

x, y = new_left
self.position = x - uy * left, y + ux * left
self.position = Vec2(x - uy * left, y + ux * left)
Loading