Skip to content
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

Add runtime support for pygame.sprite.AbstractGroup subscripts #3053

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 12 additions & 8 deletions buildconfig/stubs/pygame/sprite.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
from typing import (
Any,
Callable,
Expand All @@ -13,7 +14,7 @@ from typing import (
TypeVar,
Union,
)
from typing_extensions import deprecated # added in 3.13
from typing_extensions import deprecated # added in 3.13

from pygame.rect import FRect, Rect
from pygame.surface import Surface
Expand Down Expand Up @@ -149,6 +150,7 @@ _TDirtySprite = TypeVar("_TDirtySprite", bound=_DirtySpriteSupportsGroup)
class AbstractGroup(Generic[_TSprite]):
spritedict: Dict[_TSprite, Optional[Union[FRect, Rect]]]
lostsprites: List[Union[FRect, Rect]]
def __class_getitem__(cls, generic: Any) -> typing._GenericAlias: ... # type: ignore[name-defined]
def __init__(self) -> None: ...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[_TSprite]: ...
Expand Down Expand Up @@ -181,14 +183,16 @@ class Group(AbstractGroup[_TSprite]):
def __init__(
self, *sprites: Union[_TSprite, AbstractGroup[_TSprite], Iterable[_TSprite]]
) -> None: ...

# these are aliased in the code too
@deprecated("Use `pygame.sprite.Group` instead")
class RenderPlain(Group): ...

@deprecated("Use `pygame.sprite.Group` instead")
class RenderClear(Group): ...

class RenderUpdates(Group[_TSprite]): ...

@deprecated("Use `pygame.sprite.RenderUpdates` instead")
class OrderedUpdates(RenderUpdates[_TSprite]): ...

Expand All @@ -200,7 +204,7 @@ class LayeredUpdates(AbstractGroup[_TSprite]):
AbstractGroup[_TSprite],
Iterable[Union[_TSprite, AbstractGroup[_TSprite]]],
],
**kwargs: Any
**kwargs: Any,
) -> None: ...
def add(
self,
Expand All @@ -209,7 +213,7 @@ class LayeredUpdates(AbstractGroup[_TSprite]):
AbstractGroup[_TSprite],
Iterable[Union[_TSprite, AbstractGroup[_TSprite]]],
],
**kwargs: Any
**kwargs: Any,
) -> None: ...
def get_sprites_at(self, pos: Coordinate) -> List[_TSprite]: ...
def get_sprite(self, idx: int) -> _TSprite: ...
Expand Down Expand Up @@ -238,10 +242,10 @@ class LayeredDirty(LayeredUpdates[_TDirtySprite]):
def set_timing_threshold(
self, time_ms: SupportsFloat
) -> None: ... # This actually accept any value
@deprecated("since 2.1.1. Use `pygame.sprite.LayeredDirty.set_timing_threshold` instead")
def set_timing_treshold(
self, time_ms: SupportsFloat
) -> None: ...
@deprecated(
"since 2.1.1. Use `pygame.sprite.LayeredDirty.set_timing_threshold` instead"
)
def set_timing_treshold(self, time_ms: SupportsFloat) -> None: ...

class GroupSingle(AbstractGroup[_TSprite]):
sprite: _TSprite
Expand Down
14 changes: 11 additions & 3 deletions src_py/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,19 @@
# specific ones that aren't quite so general but fit into common
# specialized cases.

from warnings import warn
import sys
from typing import Optional
from warnings import warn

import pygame
if sys.version_info[:3] >= (3, 9, 0):
from types import GenericAlias
else:
from typing import _GenericAlias as GenericAlias # type: ignore[name-defined]

import pygame
from pygame.mask import from_surface
from pygame.rect import Rect
from pygame.time import get_ticks
from pygame.mask import from_surface


class Sprite:
Expand Down Expand Up @@ -371,6 +376,9 @@ class AbstractGroup:

"""

def __class_getitem__(cls, generic):
return GenericAlias(cls, generic)

# protected identifier value to identify sprite groups, and avoid infinite recursion
_spritegroup = True

Expand Down
21 changes: 18 additions & 3 deletions test/sprite_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#################################### IMPORTS ###################################


import sys
import typing
import unittest

if sys.version_info[:3] >= (3, 9, 0):
from types import GenericAlias
else:
from typing import _GenericAlias as GenericAlias # type: ignore[name-defined]

import pygame
from pygame import sprite


################################# MODULE LEVEL #################################


Expand Down Expand Up @@ -660,6 +665,16 @@ def update(self, *args, **kwargs):
self.assertEqual(test_sprite.sink, [1, 2, 3])
self.assertEqual(test_sprite.sink_kwargs, {"foo": 4, "bar": 5})

def test_type_subscript(self):
try:
group_generic_alias = sprite.Group[sprite.Sprite]
except TypeError as e:
self.fail(e)

self.assertIsInstance(group_generic_alias, GenericAlias)
self.assertIs(typing.get_origin(group_generic_alias), sprite.Group)
self.assertEqual(typing.get_args(group_generic_alias), (sprite.Sprite,))


################################################################################

Expand Down Expand Up @@ -1362,8 +1377,8 @@ def test_memoryleak_bug(self):
# For memory leak bug posted to mailing list by Tobias Steinrücken on 16/11/10.
# Fixed in revision 2953.

import weakref
import gc
import weakref

class MySprite(sprite.Sprite):
def __init__(self, *args, **kwargs):
Expand Down
Loading