Skip to content

Commit be3230a

Browse files
authored
Fix type checking in Sprite.texture setter (#1409)
* Replace double texture type check with single __debug__ short circuiting check * Raise TypeError instead of ValueError on wrong type * Add a test for raising a TypeError when something other than a texture is passed to the setter
1 parent 35e9b0a commit be3230a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

arcade/sprite.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -902,10 +902,9 @@ def texture(self, texture: Texture):
902902
if texture == self._texture:
903903
return
904904

905-
if not isinstance(texture, Texture):
906-
raise ValueError(f"The 'texture' parameter must be an instance of arcade.Texture."
907-
f"It is an instance of '{type(texture)}'.")
908-
assert isinstance(texture, Texture)
905+
if __debug__ and not isinstance(texture, Texture):
906+
raise TypeError(f"The 'texture' parameter must be an instance of arcade.Texture,"
907+
f" but is an instance of '{type(texture)}'.")
909908

910909
self.clear_spatial_hashes()
911910
self._point_list_cache = None

tests/unit2/test_sprite.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest as pytest
2+
13
import arcade
24

35
frame_counter = 0
@@ -120,6 +122,15 @@ def on_draw():
120122
window.test(2)
121123

122124

125+
@pytest.mark.parametrize('not_a_texture', [
126+
1, "not_a_texture", (1, 2, 3)
127+
])
128+
def test_sprite_texture_setter_raises_type_error_when_given_non_texture(not_a_texture):
129+
sprite = arcade.Sprite(":resources:images/items/coinGold.png", 1.0)
130+
with pytest.raises(TypeError):
131+
sprite.texture = not_a_texture
132+
133+
123134
def test_sprite_sizes(window: arcade.Window):
124135
SIZE = 50
125136
SPACING = SIZE * 2

0 commit comments

Comments
 (0)