diff --git a/src_py/sprite.py b/src_py/sprite.py index 51116ebdfc..a71e868efa 100644 --- a/src_py/sprite.py +++ b/src_py/sprite.py @@ -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: @@ -372,10 +377,7 @@ class AbstractGroup: """ def __class_getitem__(cls, generic): - # switch to `types.GenericAlias` once Python 3.8 support is dropped - import typing - - return typing._GenericAlias(cls, generic) # type: ignore[name-defined] + return GenericAlias(cls, generic) # protected identifier value to identify sprite groups, and avoid infinite recursion _spritegroup = True diff --git a/test/sprite_test.py b/test/sprite_test.py index d0f95ea2d1..f1d55c285a 100644 --- a/test/sprite_test.py +++ b/test/sprite_test.py @@ -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 ################################# @@ -666,10 +671,7 @@ def test_type_subscript(self): except TypeError as e: self.fail(e) - # switch to `types.GenericAlias` once Python 3.8 support is dropped - import typing - - self.assertIsInstance(group_generic_alias, typing._GenericAlias) # type: ignore[name-defined] + 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,)) @@ -1375,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):