From 2eb1cc9d5b41d382fb55c429d33968b0ff6d76bf Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:07:49 -0400 Subject: [PATCH 1/8] Use | None in arcade.text.Text.__init__ --- arcade/text.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arcade/text.py b/arcade/text.py index a59d082c7e..365bc942d4 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 = 0, 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 From ebe0b6fbcc29285b016ad1140d2da323dfc150f6 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:09:27 -0400 Subject: [PATCH 2/8] Use | None in arcade.text.create_text_sprite --- arcade/text.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arcade/text.py b/arcade/text.py index 365bc942d4..fa53b5e5bf 100644 --- a/arcade/text.py +++ b/arcade/text.py @@ -581,15 +581,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`. From c83fd75cd657807af4cc36f8685ab5425f98e7e7 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:12:43 -0400 Subject: [PATCH 3/8] Handle Text width=None, multiline=True edge case * Add edge case handling by using not instead of == 0 * Update arcade.text.Text instance tests --- arcade/text.py | 6 ++++-- tests/unit/text/test_text_error_handling.py | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/arcade/text.py b/arcade/text.py index fa53b5e5bf..42489c5d1e 100644 --- a/arcade/text.py +++ b/arcade/text.py @@ -196,8 +196,10 @@ 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( diff --git a/tests/unit/text/test_text_error_handling.py b/tests/unit/text/test_text_error_handling.py index cb88f998fc..f4ecc48b27 100644 --- a/tests/unit/text/test_text_error_handling.py +++ b/tests/unit/text/test_text_error_handling.py @@ -7,7 +7,13 @@ def test_text_instance_raise_multiline_error(window): with pytest.raises(ValueError) as e: _ = arcade.Text("Initial text", 0, 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): From 1779fe038707f7b4b892a8dc931bc41546355e7c Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:38:06 -0400 Subject: [PATCH 4/8] Add edge case handling to draw_text following Text's --- arcade/text.py | 9 ++++++--- tests/unit/text/test_text_error_handling.py | 8 +++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/arcade/text.py b/arcade/text.py index 42489c5d1e..375945f060 100644 --- a/arcade/text.py +++ b/arcade/text.py @@ -671,7 +671,7 @@ def draw_text( y: int, color: RGBA255 = arcade.color.WHITE, font_size: float = 12, - width: int = 0, + width: int | None = 0, align: str = "left", font_name: FontNameOrNames = ("calibri", "arial"), bold: bool = False, @@ -849,8 +849,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 f4ecc48b27..96db10f250 100644 --- a/tests/unit/text/test_text_error_handling.py +++ b/tests/unit/text/test_text_error_handling.py @@ -20,4 +20,10 @@ def test_text_function_raise_multiline_error(window): with pytest.raises(ValueError) as e: _ = arcade.draw_text("Initial text", 0, 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.draw_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." From 11de06c46de1d6c1427b84828314526e1fe818e6 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:39:19 -0400 Subject: [PATCH 5/8] Whitespace in test file --- tests/unit/text/test_text_error_handling.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/unit/text/test_text_error_handling.py b/tests/unit/text/test_text_error_handling.py index 96db10f250..08ca43cced 100644 --- a/tests/unit/text/test_text_error_handling.py +++ b/tests/unit/text/test_text_error_handling.py @@ -9,7 +9,6 @@ def test_text_instance_raise_multiline_error(window): 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) @@ -20,7 +19,6 @@ def test_text_function_raise_multiline_error(window): with pytest.raises(ValueError) as e: _ = arcade.draw_text("Initial text", 0, 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: From c39cd68bbbfcf410bb5684d9315eba967a1da35b Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:41:29 -0400 Subject: [PATCH 6/8] Use width = None default to follow upstream pyglet.Label --- arcade/text.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arcade/text.py b/arcade/text.py index 375945f060..8c3a2191a1 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: int | None = 0, + width: int | None = None, align: str = "left", font_name: FontNameOrNames = ("calibri", "arial"), bold: bool = False, @@ -671,7 +671,7 @@ def draw_text( y: int, color: RGBA255 = arcade.color.WHITE, font_size: float = 12, - width: int | None = 0, + width: int | None = None, align: str = "left", font_name: FontNameOrNames = ("calibri", "arial"), bold: bool = False, From 2c7ae98efed1580c2848982ada417e4e6c6ff4ab Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:55:55 -0400 Subject: [PATCH 7/8] ./make.py format --- arcade/text.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arcade/text.py b/arcade/text.py index 8c3a2191a1..6238dc1044 100644 --- a/arcade/text.py +++ b/arcade/text.py @@ -199,7 +199,8 @@ def __init__( 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}.") + f"but got {width!r}." + ) adjusted_font = _attempt_font_name_resolution(font_name) self._label = pyglet.text.Label( From 234d207dcf09ba81105c76acf38ce611712558f9 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Mon, 8 Jul 2024 11:55:05 -0400 Subject: [PATCH 8/8] Make tests more specific --- tests/unit/text/test_text_error_handling.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/text/test_text_error_handling.py b/tests/unit/text/test_text_error_handling.py index 08ca43cced..4a46dd29e5 100644 --- a/tests/unit/text/test_text_error_handling.py +++ b/tests/unit/text/test_text_error_handling.py @@ -5,7 +5,7 @@ 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 to a non-zero value when 'multiline' is True, but got 0." @@ -17,7 +17,7 @@ def test_text_instance_raise_multiline_error(window): 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."