Skip to content

Commit 14bdaae

Browse files
committed
Address review comments.
Notably, make ReadOnlySpriteHash inherit Protocol.
1 parent b11fb9a commit 14bdaae

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

arcade/sprite_list/spatial_hash.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
from abc import abstractmethod
2+
from collections.abc import Set
23
from math import trunc
3-
from typing import Generic
4+
from typing import Protocol
45

56
from arcade.sprite import SpriteType, SpriteType_co
67
from arcade.sprite.base import BasicSprite
78
from arcade.types import IPoint, Point
89
from arcade.types.rect import Rect
910

1011

11-
class ReadOnlySpatialHash(Generic[SpriteType_co]):
12-
"""
13-
Read-only view of a SpatialHash.
12+
class ReadOnlySpatialHash(Protocol[SpriteType_co]):
13+
"""A read-only view of a :py:class:`.SpatialHash` which helps preserve safety.
14+
15+
This works like the read-only views of Python's built-in :py:class:`dict`
16+
and other types. As an every-day user, it means that the underlying
17+
`SpatialHash` may contain subclasses of the annotated type, but not
18+
superclasses.
1419
15-
This is useful when the SpriteType is in covariant position.
20+
This ensures predicable behavior via type safety in cases where:
1621
17-
See SpatialHash for more details.
22+
#. A spatial hash is annotated with a specific type
23+
#. It is then manipulated outside the original context with a broader type
24+
25+
Advanced users who want more information on the specifics should see the
26+
comments of :py:class:`~arcade.sprite_list.SpriteList`.
1827
"""
1928

2029
@abstractmethod
21-
def get_sprites_near_sprite(self, sprite: BasicSprite) -> set[SpriteType_co]:
30+
def get_sprites_near_sprite(self, sprite: BasicSprite) -> Set[SpriteType_co]:
2231
"""
2332
Get all the sprites that are in the same buckets as the given sprite.
2433
@@ -28,7 +37,7 @@ def get_sprites_near_sprite(self, sprite: BasicSprite) -> set[SpriteType_co]:
2837
...
2938

3039
@abstractmethod
31-
def get_sprites_near_point(self, point: Point) -> set[SpriteType_co]:
40+
def get_sprites_near_point(self, point: Point) -> Set[SpriteType_co]:
3241
"""
3342
Return sprites in the same bucket as the given point.
3443
@@ -38,12 +47,17 @@ def get_sprites_near_point(self, point: Point) -> set[SpriteType_co]:
3847
...
3948

4049
@abstractmethod
41-
def get_sprites_near_rect(self, rect: Rect) -> set[SpriteType_co]:
50+
def get_sprites_near_rect(self, rect: Rect) -> Set[SpriteType_co]:
4251
"""
4352
Return sprites in the same buckets as the given rectangle.
4453
54+
.. tip:: Use :py:mod:`arcade.types.rect`'s helper functions to create
55+
rectangle objects!
56+
4557
Args:
46-
rect: The rectangle to check (left, right, bottom, top)
58+
rect:
59+
The rectangle to check as a :py:class:`~arcade.types.rect.Rect`
60+
object.
4761
"""
4862
...
4963

arcade/sprite_list/sprite_list.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@
4141

4242

4343
class SpriteSequence(Collection[SpriteType_co]):
44-
"""
45-
A read-only view of a SpriteList.
44+
"""A read-only view of a :py:class:`.SpriteList`.
4645
47-
Like collections.abc.Sequence, a SpriteSequence is covariant in its sprite
48-
type. This is useful when you want to manipulate a collection of
49-
SpriteLists of different sprite types.
46+
Like other read-only generics such as :py:class:`collections.abc.Sequence`,
47+
a `SpriteSequence` requires sprites be of a covariant type relative to their
48+
annotated type.
5049
51-
See SpriteList for more details.
50+
See :py:class:`.SpriteList` for more details.
5251
"""
5352

5453
from ..sprite_list import spatial_hash as sh

0 commit comments

Comments
 (0)