1
1
from abc import abstractmethod
2
+ from collections .abc import Set
2
3
from math import trunc
3
- from typing import Generic
4
+ from typing import Protocol
4
5
5
6
from arcade .sprite import SpriteType , SpriteType_co
6
7
from arcade .sprite .base import BasicSprite
7
8
from arcade .types import IPoint , Point
8
9
from arcade .types .rect import Rect
9
10
10
11
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.
14
19
15
- This is useful when the SpriteType is in covariant position.
20
+ This ensures predicable behavior via type safety in cases where:
16
21
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`.
18
27
"""
19
28
20
29
@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 ]:
22
31
"""
23
32
Get all the sprites that are in the same buckets as the given sprite.
24
33
@@ -28,7 +37,7 @@ def get_sprites_near_sprite(self, sprite: BasicSprite) -> set[SpriteType_co]:
28
37
...
29
38
30
39
@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 ]:
32
41
"""
33
42
Return sprites in the same bucket as the given point.
34
43
@@ -38,12 +47,17 @@ def get_sprites_near_point(self, point: Point) -> set[SpriteType_co]:
38
47
...
39
48
40
49
@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 ]:
42
51
"""
43
52
Return sprites in the same buckets as the given rectangle.
44
53
54
+ .. tip:: Use :py:mod:`arcade.types.rect`'s helper functions to create
55
+ rectangle objects!
56
+
45
57
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.
47
61
"""
48
62
...
49
63
0 commit comments