diff --git a/buildconfig/stubs/pygame/sprite.pyi b/buildconfig/stubs/pygame/sprite.pyi index 148c54dea2..b3245324ef 100644 --- a/buildconfig/stubs/pygame/sprite.pyi +++ b/buildconfig/stubs/pygame/sprite.pyi @@ -30,12 +30,12 @@ from pygame.typing import Point, RectLike # Some sprite functions only need objects with certain attributes, not always a sprite class _HasRect(Protocol): @property - def rect(self) -> Optional[Union[FRect, Rect]]: ... + def rect(self) -> Union[FRect, Rect]: ... # image in addition to rect class _HasImageAndRect(_HasRect, Protocol): @property - def image(self) -> Optional[Surface]: ... + def image(self) -> Surface: ... # mask in addition to rect class _HasMaskAndRect(_HasRect, Protocol): @@ -43,14 +43,8 @@ class _HasMaskAndRect(_HasRect, Protocol): def mask(self) -> Mask: ... class Sprite(_HasImageAndRect): - @property - def image(self) -> Optional[Surface]: ... - @image.setter - def image(self, value: Optional[Surface]) -> None: ... - @property - def rect(self) -> Optional[Union[FRect, Rect]]: ... - @rect.setter - def rect(self, value: Optional[Union[FRect, Rect]]) -> None: ... + image: Surface # Uses attribute annotation instead of property to satisfy stubtest + rect: Union[FRect, Rect] # Same as image, see above @property def layer(self) -> int: ... @layer.setter @@ -68,7 +62,7 @@ class Sprite(_HasImageAndRect): class DirtySprite(Sprite): dirty: int blendmode: int - source_rect: Union[FRect, Rect] + source_rect: Optional[Union[FRect, Rect]] visible: int _layer: int @@ -150,7 +144,7 @@ class LayeredDirty(LayeredUpdates[_DirtySpriteT]): def clear(self, surface: Surface, bgd: Surface) -> None: ... # type: ignore[override] def repaint_rect(self, screen_rect: RectLike) -> None: ... def set_clip(self, screen_rect: Optional[RectLike] = None) -> None: ... - def get_clip(self) -> Union[FRect, Rect]: ... + def get_clip(self) -> Optional[Union[FRect, Rect]]: ... def set_timing_threshold(self, time_ms: SupportsFloat) -> None: ... @deprecated( "since 2.1.1. Use `pygame.sprite.LayeredDirty.set_timing_threshold` instead" diff --git a/src_py/sprite.py b/src_py/sprite.py index 9e72c3f2e1..f4a90a7530 100644 --- a/src_py/sprite.py +++ b/src_py/sprite.py @@ -85,12 +85,13 @@ # specialized cases. import types -from typing import Optional +from typing import Union from warnings import warn import pygame from pygame.mask import from_surface -from pygame.rect import Rect +from pygame.rect import FRect, Rect +from pygame.surface import Surface from pygame.time import get_ticks @@ -109,29 +110,14 @@ class Sprite: """ + image: Surface + rect: Union[Rect, FRect] + def __init__(self, *groups): self.__g = {} # The groups the sprite is in - self.__image: Optional[pygame.surface.Surface] = None - self.__rect: Optional[pygame.rect.Rect] = None if groups: self.add(*groups) - @property - def image(self): - return self.__image - - @image.setter - def image(self, value: Optional[pygame.surface.Surface]): - self.__image = value - - @property - def rect(self): - return self.__rect - - @rect.setter - def rect(self, value: Optional[pygame.rect.Rect]): - self.__rect = value - def add(self, *groups): """add the sprite to groups @@ -1337,7 +1323,7 @@ def set_clip(self, screen_rect=None): def get_clip(self): """get the area where drawing will occur - LayeredDirty.get_clip(): return Rect + LayeredDirty.get_clip(): return Rect or None """ return self._clip