Skip to content

Commit

Permalink
import appropriate GenericAlias based on the version
Browse files Browse the repository at this point in the history
  • Loading branch information
Matiiss committed Nov 24, 2024
1 parent f095550 commit 7f3b58f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
16 changes: 9 additions & 7 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 @@ -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
Expand Down
16 changes: 9 additions & 7 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 @@ -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,))

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 7f3b58f

Please sign in to comment.