From df0b34b38b7b966cb10d987295f07ff838ba015c Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Tue, 8 Aug 2023 10:38:01 +0200 Subject: [PATCH 1/7] enhance typings on Sprite and SpriteList --- arcade/scene.py | 8 ++++---- arcade/sprite/base.py | 4 ++-- arcade/sprite/sprite.py | 16 +++++++++++----- arcade/sprite_list/sprite_list.py | 10 ++++++++-- arcade/types.py | 21 ++++++++++++++++++++- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/arcade/scene.py b/arcade/scene.py index d84140da89..1a5eb0fd23 100644 --- a/arcade/scene.py +++ b/arcade/scene.py @@ -10,10 +10,10 @@ * Control sprite list draw order within the group """ -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.types import Color, RGBA255, OpenGlFilters, BlendFunctions from arcade.tilemap import TileMap from warnings import warn @@ -432,9 +432,9 @@ def update_animation( def draw( self, names: Optional[Iterable[str]] = None, - filter: Optional[int] = None, + filter: Optional[OpenGlFilters] = None, pixelated: bool = False, - blend_function: Optional[Union[Tuple[int, int], Tuple[int, int, int, int]]] = None, + blend_function: Optional[BlendFunctions] = None, **kwargs ) -> None: """ diff --git a/arcade/sprite/base.py b/arcade/sprite/base.py index ecbda85f53..174102bac6 100644 --- a/arcade/sprite/base.py +++ b/arcade/sprite/base.py @@ -1,4 +1,4 @@ -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 @@ -47,7 +47,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 diff --git a/arcade/sprite/sprite.py b/arcade/sprite/sprite.py index 7a62d7f3d2..37553c713f 100644 --- a/arcade/sprite/sprite.py +++ b/arcade/sprite/sprite.py @@ -5,7 +5,7 @@ from arcade import Texture, load_texture from arcade.hitbox import HitBox, RotatableHitBox from arcade.texture import get_default_texture -from arcade.types import PathOrTexture, Point +from arcade.types import PathOrTexture, Point, OpenGlFilters, BlendFunctions from .base import BasicSprite from .mixins import PymunkMixin @@ -66,7 +66,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 @@ -255,7 +255,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 ----- @@ -313,7 +313,13 @@ def stop(self) -> None: # ---- Draw Methods ---- - def draw(self, *, filter=None, pixelated=None, blend_function=None) -> None: + def draw( + self, + *, + filter: Optional[OpenGlFilters] | None = None, + pixelated: Optional[bool] = None, + blend_function: Optional[BlendFunctions] = None + ) -> None: """ A debug method which draws the sprite into the current OpenGL context. @@ -399,7 +405,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 diff --git a/arcade/sprite_list/sprite_list.py b/arcade/sprite_list/sprite_list.py index af33e53c4a..2e35f4f5a8 100644 --- a/arcade/sprite_list/sprite_list.py +++ b/arcade/sprite_list/sprite_list.py @@ -31,7 +31,7 @@ get_window, gl, ) -from arcade.types import Color, RGBA255 +from arcade.types import Color, RGBA255, OpenGlFilters, BlendFunctions from arcade.gl.buffer import Buffer from arcade.gl.vertex_array import Geometry @@ -960,7 +960,13 @@ def initialize(self): """ self._init_deferred() - def draw(self, *, filter=None, pixelated=None, blend_function=None): + def draw( + self, + *, + filter: Optional[OpenGlFilters] = None, + pixelated: Optional[bool] = None, + blend_function: Optional[BlendFunctions] = None + ) -> None: """ Draw this list of sprites. diff --git a/arcade/types.py b/arcade/types.py index e6be602db8..9cdb2ca88e 100644 --- a/arcade/types.py +++ b/arcade/types.py @@ -17,7 +17,10 @@ Sequence, Tuple, Union, - TYPE_CHECKING, TypeVar + TYPE_CHECKING, + TypeVar, + TypeAlias, + Literal, ) from typing_extensions import Self @@ -49,6 +52,22 @@ RGBA255OrNormalized = Union[RGBA255, RGBANormalized] +# equivalent to the following filters (under arcade.gl.enums): +# NEAREST, LINEAR, NEAREST_MIPMAP_NEAREST, LINEAR_MIPMAP_NEAREST, +# NEAREST_MIPMAP_LINEAR, LINEAR_MIPMAP_LINEAR +OpenGlFilters: TypeAlias = Union[Literal[9728], Literal[9729], Literal[9984], + Literal[9985], Literal[9986], Literal[9987]] + +# equivalent to the following blend_functions (under arcade.gl.enums): +# ZERO, ONE, SRC_COLOR, ONE_MINUS_SRC_COLOR, +# SRC_ALPHA, ONE_MINUS_SRC_ALPHA, DST_ALPHA, +# ONE_MINUS_DST_ALPHA, DST_COLOR, ONE_MINUS_DST_COLOR +BlendFunctions: TypeAlias = Union[Literal[0], Literal[1], Literal[768], + Literal[769], Literal[770], Literal[771], + Literal[772], Literal[773], Literal[774], + Literal[775]] + + __all__ = [ "BufferProtocol", From 59ce9d97141ce3c78fc69d9c3859887bd69f154c Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 6 Sep 2023 06:09:55 -0400 Subject: [PATCH 2/7] Make annotations singular --- arcade/scene.py | 6 +++--- arcade/sprite/sprite.py | 6 +++--- arcade/sprite_list/sprite_list.py | 6 +++--- arcade/types.py | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/arcade/scene.py b/arcade/scene.py index 1a5eb0fd23..88d049ceb3 100644 --- a/arcade/scene.py +++ b/arcade/scene.py @@ -13,7 +13,7 @@ from typing import Dict, List, Optional, Union, Iterable from arcade import Sprite, SpriteList -from arcade.types import Color, RGBA255, OpenGlFilters, BlendFunctions +from arcade.types import Color, RGBA255, OpenGlFilter, BlendFunction from arcade.tilemap import TileMap from warnings import warn @@ -432,9 +432,9 @@ def update_animation( def draw( self, names: Optional[Iterable[str]] = None, - filter: Optional[OpenGlFilters] = None, + filter: Optional[OpenGlFilter] = None, pixelated: bool = False, - blend_function: Optional[BlendFunctions] = None, + blend_function: Optional[BlendFunction] = None, **kwargs ) -> None: """ diff --git a/arcade/sprite/sprite.py b/arcade/sprite/sprite.py index 37553c713f..5766f2acef 100644 --- a/arcade/sprite/sprite.py +++ b/arcade/sprite/sprite.py @@ -5,7 +5,7 @@ from arcade import Texture, load_texture from arcade.hitbox import HitBox, RotatableHitBox from arcade.texture import get_default_texture -from arcade.types import PathOrTexture, Point, OpenGlFilters, BlendFunctions +from arcade.types import PathOrTexture, Point, OpenGlFilter, BlendFunction from .base import BasicSprite from .mixins import PymunkMixin @@ -316,9 +316,9 @@ def stop(self) -> None: def draw( self, *, - filter: Optional[OpenGlFilters] | None = None, + filter: Optional[OpenGlFilter] | None = None, pixelated: Optional[bool] = None, - blend_function: Optional[BlendFunctions] = None + blend_function: Optional[BlendFunction] = None ) -> None: """ A debug method which draws the sprite into the current OpenGL context. diff --git a/arcade/sprite_list/sprite_list.py b/arcade/sprite_list/sprite_list.py index 2e35f4f5a8..082c656e46 100644 --- a/arcade/sprite_list/sprite_list.py +++ b/arcade/sprite_list/sprite_list.py @@ -31,7 +31,7 @@ get_window, gl, ) -from arcade.types import Color, RGBA255, OpenGlFilters, BlendFunctions +from arcade.types import Color, RGBA255, OpenGlFilter, BlendFunction from arcade.gl.buffer import Buffer from arcade.gl.vertex_array import Geometry @@ -963,9 +963,9 @@ def initialize(self): def draw( self, *, - filter: Optional[OpenGlFilters] = None, + filter: Optional[OpenGlFilter] = None, pixelated: Optional[bool] = None, - blend_function: Optional[BlendFunctions] = None + blend_function: Optional[BlendFunction] = None ) -> None: """ Draw this list of sprites. diff --git a/arcade/types.py b/arcade/types.py index 9cdb2ca88e..cd94ccf5a8 100644 --- a/arcade/types.py +++ b/arcade/types.py @@ -55,14 +55,14 @@ # equivalent to the following filters (under arcade.gl.enums): # NEAREST, LINEAR, NEAREST_MIPMAP_NEAREST, LINEAR_MIPMAP_NEAREST, # NEAREST_MIPMAP_LINEAR, LINEAR_MIPMAP_LINEAR -OpenGlFilters: TypeAlias = Union[Literal[9728], Literal[9729], Literal[9984], +OpenGlFilter: TypeAlias = Union[Literal[9728], Literal[9729], Literal[9984], Literal[9985], Literal[9986], Literal[9987]] # equivalent to the following blend_functions (under arcade.gl.enums): # ZERO, ONE, SRC_COLOR, ONE_MINUS_SRC_COLOR, # SRC_ALPHA, ONE_MINUS_SRC_ALPHA, DST_ALPHA, # ONE_MINUS_DST_ALPHA, DST_COLOR, ONE_MINUS_DST_COLOR -BlendFunctions: TypeAlias = Union[Literal[0], Literal[1], Literal[768], +BlendFunction: TypeAlias = Union[Literal[0], Literal[1], Literal[768], Literal[769], Literal[770], Literal[771], Literal[772], Literal[773], Literal[774], Literal[775]] From 68dbe0d8dc3b3026e223621024f2479299d80277 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 6 Sep 2023 06:38:11 -0400 Subject: [PATCH 3/7] Move filter & blend aliases to arcade.gl.types * Use PyGLenum per @cspotcode's suggestion * Remove use of literals * Move to arcade.gl.types * Import TypeAlias from typing_extensions since it's from 3.10 --- arcade/gl/types.py | 8 ++++++++ arcade/scene.py | 3 ++- arcade/sprite/sprite.py | 3 ++- arcade/sprite_list/sprite_list.py | 3 ++- arcade/types.py | 20 +------------------- 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/arcade/gl/types.py b/arcade/gl/types.py index fe81e27d03..32f7c75cce 100644 --- a/arcade/gl/types.py +++ b/arcade/gl/types.py @@ -1,5 +1,6 @@ import re from typing import Dict, Optional, Iterable, List, Sequence, Tuple, Union +from typing_extensions import TypeAlias from pyglet import gl @@ -14,6 +15,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, diff --git a/arcade/scene.py b/arcade/scene.py index 88d049ceb3..0a9fb77ac2 100644 --- a/arcade/scene.py +++ b/arcade/scene.py @@ -13,7 +13,8 @@ from typing import Dict, List, Optional, Union, Iterable from arcade import Sprite, SpriteList -from arcade.types import Color, RGBA255, OpenGlFilter, BlendFunction +from arcade.types import Color, RGBA255 +from arcade.gl.types import OpenGlFilter, BlendFunction from arcade.tilemap import TileMap from warnings import warn diff --git a/arcade/sprite/sprite.py b/arcade/sprite/sprite.py index 5766f2acef..2a942d974a 100644 --- a/arcade/sprite/sprite.py +++ b/arcade/sprite/sprite.py @@ -5,7 +5,8 @@ from arcade import Texture, load_texture from arcade.hitbox import HitBox, RotatableHitBox from arcade.texture import get_default_texture -from arcade.types import PathOrTexture, Point, OpenGlFilter, BlendFunction +from arcade.types import PathOrTexture, Point +from arcade.gl.types import OpenGlFilter, BlendFunction from .base import BasicSprite from .mixins import PymunkMixin diff --git a/arcade/sprite_list/sprite_list.py b/arcade/sprite_list/sprite_list.py index 082c656e46..266310ef13 100644 --- a/arcade/sprite_list/sprite_list.py +++ b/arcade/sprite_list/sprite_list.py @@ -31,7 +31,8 @@ get_window, gl, ) -from arcade.types import Color, RGBA255, OpenGlFilter, BlendFunction +from arcade.types import Color, RGBA255 +from arcade.gl.types import OpenGlFilter, BlendFunction from arcade.gl.buffer import Buffer from arcade.gl.vertex_array import Geometry diff --git a/arcade/types.py b/arcade/types.py index cd94ccf5a8..9d9921bbf1 100644 --- a/arcade/types.py +++ b/arcade/types.py @@ -18,9 +18,7 @@ Tuple, Union, TYPE_CHECKING, - TypeVar, - TypeAlias, - Literal, + TypeVar ) from typing_extensions import Self @@ -52,22 +50,6 @@ RGBA255OrNormalized = Union[RGBA255, RGBANormalized] -# equivalent to the following filters (under arcade.gl.enums): -# NEAREST, LINEAR, NEAREST_MIPMAP_NEAREST, LINEAR_MIPMAP_NEAREST, -# NEAREST_MIPMAP_LINEAR, LINEAR_MIPMAP_LINEAR -OpenGlFilter: TypeAlias = Union[Literal[9728], Literal[9729], Literal[9984], - Literal[9985], Literal[9986], Literal[9987]] - -# equivalent to the following blend_functions (under arcade.gl.enums): -# ZERO, ONE, SRC_COLOR, ONE_MINUS_SRC_COLOR, -# SRC_ALPHA, ONE_MINUS_SRC_ALPHA, DST_ALPHA, -# ONE_MINUS_DST_ALPHA, DST_COLOR, ONE_MINUS_DST_COLOR -BlendFunction: TypeAlias = Union[Literal[0], Literal[1], Literal[768], - Literal[769], Literal[770], Literal[771], - Literal[772], Literal[773], Literal[774], - Literal[775]] - - __all__ = [ "BufferProtocol", From f8477ad91133e550fba249919fb4efdba7367bda Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 6 Sep 2023 06:42:10 -0400 Subject: [PATCH 4/7] Remove | assignment incompatible with 3.8 --- arcade/sprite/sprite.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/sprite/sprite.py b/arcade/sprite/sprite.py index 2a942d974a..1af36580e7 100644 --- a/arcade/sprite/sprite.py +++ b/arcade/sprite/sprite.py @@ -317,7 +317,7 @@ def stop(self) -> None: def draw( self, *, - filter: Optional[OpenGlFilter] | None = None, + filter: Optional[OpenGlFilter] = None, pixelated: Optional[bool] = None, blend_function: Optional[BlendFunction] = None ) -> None: From 88f97e2c65f83760b23b218b8c941ded6b14ca05 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 6 Sep 2023 06:58:10 -0400 Subject: [PATCH 5/7] Allow using either int or Tuple[int, int] for filter in SpriteList.draw --- arcade/sprite_list/sprite_list.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/arcade/sprite_list/sprite_list.py b/arcade/sprite_list/sprite_list.py index 266310ef13..7ab9072fcc 100644 --- a/arcade/sprite_list/sprite_list.py +++ b/arcade/sprite_list/sprite_list.py @@ -32,7 +32,7 @@ gl, ) from arcade.types import Color, RGBA255 -from arcade.gl.types import OpenGlFilter, BlendFunction +from arcade.gl.types import OpenGlFilter, BlendFunction, PyGLenum from arcade.gl.buffer import Buffer from arcade.gl.vertex_array import Geometry @@ -964,7 +964,7 @@ def initialize(self): def draw( self, *, - filter: Optional[OpenGlFilter] = None, + filter: Optional[Union[PyGLenum, OpenGlFilter]] = None, pixelated: Optional[bool] = None, blend_function: Optional[BlendFunction] = None ) -> None: @@ -993,7 +993,12 @@ def draw( # 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(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 = filter, filter else: self.atlas.texture.filter = self.ctx.LINEAR, self.ctx.LINEAR From 60a37770fbcda4f1394eb8cb16e31c3a6a4a2aa6 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 6 Sep 2023 08:07:06 -0400 Subject: [PATCH 6/7] Add casts to make pyright happy --- arcade/sprite_list/sprite_list.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arcade/sprite_list/sprite_list.py b/arcade/sprite_list/sprite_list.py index 7ab9072fcc..90f6cdd319 100644 --- a/arcade/sprite_list/sprite_list.py +++ b/arcade/sprite_list/sprite_list.py @@ -22,7 +22,7 @@ Union, Generic, Callable, - cast, + cast, Sized, ) from arcade import ( @@ -994,11 +994,11 @@ def draw( # Set custom filter or reset to default if filter: if hasattr(filter, '__len__', ): # assume it's a collection - if len(filter) != 2: + 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 = filter, filter + self.atlas.texture.filter = cast(OpenGlFilter, (filter, filter)) else: self.atlas.texture.filter = self.ctx.LINEAR, self.ctx.LINEAR From fdcedf4312b589fbbeb64bedd93f33584ad9fb51 Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Mon, 11 Sep 2023 11:29:54 +0200 Subject: [PATCH 7/7] remove unused imports and import Any --- arcade/scene.py | 2 +- arcade/sprite/base.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arcade/scene.py b/arcade/scene.py index 8b0c83c077..033ccec06a 100644 --- a/arcade/scene.py +++ b/arcade/scene.py @@ -12,7 +12,7 @@ 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 diff --git a/arcade/sprite/base.py b/arcade/sprite/base.py index d1bd428b59..68e0d57b30 100644 --- a/arcade/sprite/base.py +++ b/arcade/sprite/base.py @@ -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