Skip to content

Commit 6cb3857

Browse files
committed
Add runtime support for pygame.sprite.AbstractGroup subscripts
1 parent 3b850e7 commit 6cb3857

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src_py/sprite.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ class AbstractGroup:
371371
372372
"""
373373

374+
def __class_getitem__(cls, generic):
375+
# switch to `types.GenericAlias` once Python 3.8 support is dropped
376+
import typing
377+
378+
return typing._GenericAlias(cls, generic) # type: ignore[attr-defined]
379+
374380
# protected identifier value to identify sprite groups, and avoid infinite recursion
375381
_spritegroup = True
376382

test/sprite_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,19 @@ def update(self, *args, **kwargs):
660660
self.assertEqual(test_sprite.sink, [1, 2, 3])
661661
self.assertEqual(test_sprite.sink_kwargs, {"foo": 4, "bar": 5})
662662

663+
def test_type_subscript(self):
664+
try:
665+
group_generic_alias = sprite.Group[sprite.Sprite]
666+
except TypeError as e:
667+
self.fail(e)
668+
669+
# switch to `types.GenericAlias` once Python 3.8 support is dropped
670+
import typing
671+
672+
self.assertIsInstance(group_generic_alias, typing._GenericAlias) # type: ignore[attr-defined]
673+
self.assertIs(typing.get_origin(group_generic_alias), sprite.Group)
674+
self.assertEqual(typing.get_args(group_generic_alias), (sprite.Sprite,))
675+
663676

664677
################################################################################
665678

0 commit comments

Comments
 (0)