Skip to content

Commit 8b4ad30

Browse files
authored
Fix SpriteList.pop exception type on empty (#2103)
* Use IndexError instead of ValueError on empty SpriteList * Docstring update for SpriteList.pop * Add tests for pop exceptions * Fix doc build * Fix typos and use inv file inspection target * Use python -m sphinx.ext.intersphinx to discover the pop doc's actual target (Python, why?) * Fix various typos and style issues
1 parent 4c1ef70 commit 8b4ad30

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

arcade/sprite_list/sprite_list.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,13 +589,20 @@ def clear(self, deep: bool = True) -> None:
589589
self._init_deferred()
590590

591591
def pop(self, index: int = -1) -> SpriteType:
592-
"""
593-
Pop off the last sprite, or the given index, from the list
592+
"""Attempt to pop a sprite from the list.
593+
594+
This works like :external:ref:`popping from <tut-morelists>` a
595+
standard Python :py:class:`list`:
596+
597+
#. If the list is empty, raise an :py:class:`IndexError`
598+
#. If no ``index`` is passed, try to pop the last
599+
:py:class:`Sprite` in the list
594600
595-
:param index: Index of sprite to remove, defaults to -1 for the last item.
601+
:param index: Index of sprite to remove (defaults to ``-1`` for
602+
the last item)
596603
"""
597604
if len(self.sprite_list) == 0:
598-
raise (ValueError("pop from empty list"))
605+
raise IndexError("pop from empty list")
599606

600607
sprite = self.sprite_list[index]
601608
self.remove(sprite)

tests/unit/spritelist/test_spritelist.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ def test_it_can_pop_at_a_given_index():
113113
assert [spritelist.sprite_slot[s] for s in spritelist] == [0, 2]
114114

115115

116+
def test_it_raises_indexerror_when_popping_from_empty_spritelist():
117+
spritelist = make_named_sprites(0)
118+
119+
# With default index
120+
with pytest.raises(IndexError):
121+
spritelist.pop()
122+
123+
# With positional argument
124+
with pytest.raises(IndexError):
125+
spritelist.pop(0)
126+
127+
# With keyword argument
128+
with pytest.raises(IndexError):
129+
spritelist.pop(index=1)
130+
131+
116132
def test_setitem(ctx):
117133
"""Testing __setitem__"""
118134
num_sprites = 10

0 commit comments

Comments
 (0)