diff --git a/arcade/text.py b/arcade/text.py index a59d082c7e..6238dc1044 100644 --- a/arcade/text.py +++ b/arcade/text.py @@ -177,7 +177,7 @@ def __init__( y: float, color: RGBOrA255 = arcade.color.WHITE, font_size: float = 12, - width: Optional[int] = 0, + width: int | None = None, align: str = "left", font_name: FontNameOrNames = ("calibri", "arial"), bold: bool = False, @@ -186,8 +186,8 @@ def __init__( anchor_y: str = "baseline", multiline: bool = False, rotation: float = 0, - batch: Optional[pyglet.graphics.Batch] = None, - group: Optional[pyglet.graphics.Group] = None, + batch: pyglet.graphics.Batch | None = None, + group: pyglet.graphics.Group | None = None, z: float = 0, ): # Raises a RuntimeError if no window for better user feedback @@ -196,8 +196,11 @@ def __init__( if align not in ("left", "center", "right"): raise ValueError("The 'align' parameter must be equal to 'left', 'right', or 'center'.") - if multiline and width == 0: - raise ValueError("The 'width' parameter must be set when 'multiline' is True.") + if multiline and not width: + raise ValueError( + f"The 'width' parameter must be set to a non-zero value when 'multiline' is True, " + f"but got {width!r}." + ) adjusted_font = _attempt_font_name_resolution(font_name) self._label = pyglet.text.Label( @@ -581,15 +584,15 @@ def create_text_sprite( text: str, color: RGBA255 = arcade.color.WHITE, font_size: float = 12, - width: Optional[int] = None, + width: int | None = None, align: str = "left", font_name: FontNameOrNames = ("calibri", "arial"), bold: bool = False, italic: bool = False, anchor_x: str = "left", multiline: bool = False, - texture_atlas: Optional[TextureAtlasBase] = None, - background_color: Optional[RGBA255] = None, + texture_atlas: TextureAtlasBase | None = None, + background_color: RGBA255 | None = None, ) -> arcade.Sprite: """ Creates a sprite containing text based off of :py:class:`~arcade.Text`. @@ -669,7 +672,7 @@ def draw_text( y: int, color: RGBA255 = arcade.color.WHITE, font_size: float = 12, - width: int = 0, + width: int | None = None, align: str = "left", font_name: FontNameOrNames = ("calibri", "arial"), bold: bool = False, @@ -847,8 +850,11 @@ def draw_text( if align not in ("left", "center", "right"): raise ValueError("The 'align' parameter must be equal to 'left', 'right', or 'center'.") - if multiline and width == 0: - raise ValueError("The 'width' parameter must be set when 'multiline' is True.") + if multiline and not width: + raise ValueError( + f"The 'width' parameter must be set to a non-zero value when 'multiline' is True, " + f"but got {width!r}." + ) if not label: adjusted_font = _attempt_font_name_resolution(font_name) diff --git a/tests/unit/text/test_text_error_handling.py b/tests/unit/text/test_text_error_handling.py index cb88f998fc..4a46dd29e5 100644 --- a/tests/unit/text/test_text_error_handling.py +++ b/tests/unit/text/test_text_error_handling.py @@ -5,13 +5,23 @@ def test_text_instance_raise_multiline_error(window): with pytest.raises(ValueError) as e: - _ = arcade.Text("Initial text", 0, 0, multiline=True) + _ = arcade.Text("Initial text", 0, 0, width=0, multiline=True) - assert e.value.args[0] == "The 'width' parameter must be set when 'multiline' is True." + assert e.value.args[0] == "The 'width' parameter must be set to a non-zero value when 'multiline' is True, but got 0." + + with pytest.raises(ValueError) as e: + _ = arcade.Text("Initial text", 0, 0, width=None, multiline=True) + + assert e.value.args[0] == "The 'width' parameter must be set to a non-zero value when 'multiline' is True, but got None." def test_text_function_raise_multiline_error(window): with pytest.raises(ValueError) as e: - _ = arcade.draw_text("Initial text", 0, 0, multiline=True) + _ = arcade.draw_text("Initial text", 0, 0, width=0, multiline=True) + + assert e.value.args[0] == "The 'width' parameter must be set to a non-zero value when 'multiline' is True, but got 0." + + with pytest.raises(ValueError) as e: + _ = arcade.draw_text("Initial text", 0, 0, width=None, multiline=True) - assert e.value.args[0] == "The 'width' parameter must be set when 'multiline' is True." + assert e.value.args[0] == "The 'width' parameter must be set to a non-zero value when 'multiline' is True, but got None."