Skip to content

Commit

Permalink
Merge pull request #689 from pathunstrom/gomlib-small-fixes
Browse files Browse the repository at this point in the history
Gomlib small fixes
  • Loading branch information
AstraLuma committed May 20, 2024
2 parents 2b9b835 + 132d50e commit ef81cb4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/ppb/gomlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self):
self._kinds = defaultdict(set)
self._tags = defaultdict(set)

def __contains__(self, item: 'GameObject') -> bool:
def __contains__(self, item: any) -> bool:
return item in self._all

def __iter__(self) -> Iterator['GameObject']:
Expand Down Expand Up @@ -52,7 +52,11 @@ def add(self, child: 'GameObject', tags: Iterable[Hashable] = ()) -> 'GameObject
raise BadChildException(child)

if isinstance(tags, (str, bytes)):
raise TypeError("You passed a string instead of an iterable, this probably isn't what you intended.\n\nTry making it a tuple.")
raise TypeError(
"""You passed a string instead of an iterable, this probably isn't what you intended.
Try making it a tuple."""
)

self._all.add(child)

Expand Down Expand Up @@ -85,7 +89,7 @@ def remove(self, child: 'GameObject') -> 'GameObject':

return child

def get(self, *, kind: Type = None, tag: 'GameObject' = None, **_) -> Iterator:
def get(self, *, kind: Type = None, tag: Hashable = None, **_) -> Iterator:
"""
Iterate over the objects by kind or tag.
Expand Down Expand Up @@ -121,12 +125,12 @@ def walk(self):
"""
for child in self._all:
yield child
if hasattr(child, 'children') and hasattr(child.children.walk):
if hasattr(child, 'children') and hasattr(child.children, 'walk'):
yield from child.children.walk()

def tags(self):
"""
Generates all of the tags currently in the collections
Generates all the tags currently in the collections
"""
yield from self._tags

Expand Down Expand Up @@ -160,7 +164,7 @@ def __iter__(self) -> Iterator:
"""
yield from self.children

def add(self, child: 'GameObject', tags: Iterable = ()) -> None:
def add(self, child: 'GameObject', tags: Iterable[Hashable] = ()) -> 'GameObject':
"""
Shorthand for :meth:`Children.add()`
"""
Expand Down
13 changes: 12 additions & 1 deletion tests/test_gom.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import pytest

import ppb
from ppb.errors import BadChildException
from ppb.gomlib import GameObject, Children



class TestEnemy:
pass

Expand Down Expand Up @@ -123,3 +123,14 @@ def test_collection_methods(player, enemies):
# Test __iter__
for game_object in container:
assert game_object is player or game_object is enemies[0]


def test_walk_doesnt_explode():
scene = ppb.Scene()
containing_sprite = ppb.Sprite()
scene.add(containing_sprite)
for x in range(2):
containing_sprite.add(ppb.Sprite())

for o in scene.children.walk():
assert o

0 comments on commit ef81cb4

Please sign in to comment.