Skip to content

typing(sprites): enhance typings on Sprite and SpriteList #1868

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

Merged
merged 8 commits into from
Sep 11, 2023
Merged
8 changes: 8 additions & 0 deletions arcade/gl/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import re
from typing import Dict, Optional, Iterable, List, Sequence, Tuple, Union
from typing_extensions import TypeAlias

from pyglet import gl

Expand All @@ -16,6 +17,13 @@
GLuintLike = Union[gl.GLuint, int]
PyGLuint = int


OpenGlFilter: TypeAlias = Tuple[PyGLenum, PyGLenum]
BlendFunction: TypeAlias = Union[
Tuple[PyGLenum, PyGLenum],
Tuple[PyGLenum, PyGLenum, PyGLenum, PyGLenum]
]

_float_base_format = (0, gl.GL_RED, gl.GL_RG, gl.GL_RGB, gl.GL_RGBA)
_int_base_format = (
0,
Expand Down
7 changes: 4 additions & 3 deletions arcade/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

from __future__ import annotations

from typing import Dict, List, Optional, Union, Iterable, Tuple
from typing import Dict, List, Optional, Union, Iterable

from arcade import Sprite, SpriteList
from arcade.types import Color, RGBA255
from arcade.gl.types import OpenGlFilter, BlendFunction
from arcade.tilemap import TileMap

from warnings import warn
Expand Down Expand Up @@ -434,9 +435,9 @@ def update_animation(
def draw(
self,
names: Optional[Iterable[str]] = None,
filter: Optional[int] = None,
filter: Optional[OpenGlFilter] = None,
pixelated: bool = False,
blend_function: Optional[Union[Tuple[int, int], Tuple[int, int, int, int]]] = None,
blend_function: Optional[BlendFunction] = None,
**kwargs
) -> None:
"""
Expand Down
4 changes: 2 additions & 2 deletions arcade/sprite/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Iterable, List, TypeVar
from typing import TYPE_CHECKING, Iterable, List, TypeVar, Any

import arcade
from arcade.types import Point, Color, RGBA255, PointList
Expand Down Expand Up @@ -49,7 +49,7 @@ def __init__(
scale: float = 1.0,
center_x: float = 0,
center_y: float = 0,
**kwargs,
**kwargs: Any,
) -> None:
self._position = (center_x, center_y)
self._depth = 0.0
Expand Down
15 changes: 11 additions & 4 deletions arcade/sprite/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from arcade.hitbox import HitBox, RotatableHitBox
from arcade.texture import get_default_texture
from arcade.types import PathOrTexture, Point
from arcade.gl.types import OpenGlFilter, BlendFunction

from .base import BasicSprite
from .mixins import PymunkMixin
Expand Down Expand Up @@ -68,7 +69,7 @@ def __init__(
center_x: float = 0.0,
center_y: float = 0.0,
angle: float = 0.0,
**kwargs,
**kwargs: Any,
):
if isinstance(path_or_texture, Texture):
_texture = path_or_texture
Expand Down Expand Up @@ -256,7 +257,7 @@ def properties(self) -> Dict[str, Any]:
return self._properties

@properties.setter
def properties(self, value):
def properties(self, value: Dict[str, Any]):
self._properties = value

# --- Movement methods -----
Expand Down Expand Up @@ -314,7 +315,13 @@ def stop(self) -> None:

# ---- Draw Methods ----

def draw(self, *, filter=None, pixelated=None, blend_function=None) -> None:
def draw(
self,
*,
filter: Optional[OpenGlFilter] = None,
pixelated: Optional[bool] = None,
blend_function: Optional[BlendFunction] = None
) -> None:
"""
A debug method which draws the sprite into the current OpenGL context.

Expand Down Expand Up @@ -400,7 +407,7 @@ def remove_from_sprite_lists(self) -> None:

self.physics_engines.clear()

def register_physics_engine(self, physics_engine) -> None:
def register_physics_engine(self, physics_engine: Any) -> None:
"""
Register a physics engine on the sprite.
This is only needed if you actually need a reference
Expand Down
18 changes: 15 additions & 3 deletions arcade/sprite_list/sprite_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Union,
Generic,
Callable,
cast,
cast, Sized,
)

from arcade import (
Expand All @@ -34,6 +34,7 @@
gl,
)
from arcade.types import Color, RGBA255
from arcade.gl.types import OpenGlFilter, BlendFunction, PyGLenum
from arcade.gl.buffer import Buffer
from arcade.gl.vertex_array import Geometry

Expand Down Expand Up @@ -957,7 +958,13 @@ def initialize(self):
"""
self._init_deferred()

def draw(self, *, filter=None, pixelated=None, blend_function=None):
def draw(
self,
*,
filter: Optional[Union[PyGLenum, OpenGlFilter]] = None,
pixelated: Optional[bool] = None,
blend_function: Optional[BlendFunction] = None
) -> None:
"""
Draw this list of sprites.

Expand All @@ -983,7 +990,12 @@ def draw(self, *, filter=None, pixelated=None, blend_function=None):

# Set custom filter or reset to default
if filter:
self.atlas.texture.filter = filter, filter
if hasattr(filter, '__len__', ): # assume it's a collection
if len(cast(Sized, filter)) != 2:
raise ValueError("Can't use sequence of length != 2")
self.atlas.texture.filter = tuple(filter) # type: ignore
else: # assume it's an int
self.atlas.texture.filter = cast(OpenGlFilter, (filter, filter))
else:
self.atlas.texture.filter = self.ctx.LINEAR, self.ctx.LINEAR

Expand Down
3 changes: 2 additions & 1 deletion arcade/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
Sequence,
Tuple,
Union,
TYPE_CHECKING, TypeVar
TYPE_CHECKING,
TypeVar
)
from typing_extensions import Self

Expand Down