From 57b338a71740cb9b957476701def60de775b07c6 Mon Sep 17 00:00:00 2001 From: Charles Whittington Date: Wed, 18 Dec 2024 00:57:53 -0500 Subject: [PATCH 1/4] Fixed update/init with padding --- core/src/toga/style/pack.py | 7 ++ .../style/pack/test_deprecated_properties.py | 66 ++++++++++++------- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/core/src/toga/style/pack.py b/core/src/toga/style/pack.py index 7d9d7515a0..1b353fd21b 100644 --- a/core/src/toga/style/pack.py +++ b/core/src/toga/style/pack.py @@ -111,6 +111,13 @@ def _hidden(self) -> bool: # Backwards compatibility for Toga <= 0.4.8 ####################################################### + def update(self, **properties): + properties = { + self._update_property_name(name.replace("-", "_")): value + for name, value in properties.items() + } + super().update(**properties) + # Pack.alignment is still an actual property, despite being deprecated, so we need # to suppress deprecation warnings when reapply is called. def reapply(self, *args, **kwargs): diff --git a/core/tests/style/pack/test_deprecated_properties.py b/core/tests/style/pack/test_deprecated_properties.py index 45ff4bab34..d2f1cc6943 100644 --- a/core/tests/style/pack/test_deprecated_properties.py +++ b/core/tests/style/pack/test_deprecated_properties.py @@ -16,12 +16,32 @@ ) -def setitem(obj, name, value): - obj[name] = value +def with_init(name, value): + return Pack(**{name: value}) -def setitem_hyphen(obj, name, value): - obj[name.replace("_", "-")] = value +def with_update(name, value): + style = Pack() + style.update(**{name: value}) + return style + + +def with_setattr(name, value): + style = Pack() + setattr(style, name, value) + return style + + +def with_setitem(name, value): + style = Pack() + style[name] = value + return style + + +def with_setitem_hyphen(name, value): + style = Pack() + style[name.replace("_", "-")] = value + return style def getitem(obj, name): @@ -50,17 +70,17 @@ def delitem_hyphen(obj, name): ("padding_left", "margin_left", 5, 0), ], ) -@pytest.mark.parametrize("set_fn", (setattr, setitem, setitem_hyphen)) +@pytest.mark.parametrize( + "style_with", + (with_init, with_update, with_setattr, with_setitem, with_setitem_hyphen), +) @pytest.mark.parametrize("get_fn", (getattr, getitem, getitem_hyphen)) @pytest.mark.parametrize("del_fn", (delattr, delitem, delitem_hyphen)) -def test_deprecated_properties( - old_name, new_name, value, default, set_fn, get_fn, del_fn -): +def test_padding_margin(old_name, new_name, value, default, style_with, get_fn, del_fn): """Deprecated names alias to new names, and issue deprecation warnings.""" # Set the old name, then check the new name - style = Pack() with pytest.warns(DeprecationWarning): - set_fn(style, old_name, value) + style = style_with(old_name, value) assert get_fn(style, new_name) == value # Delete the old name, check new name @@ -69,8 +89,7 @@ def test_deprecated_properties( assert get_fn(style, new_name) == default # Set the new name, then check the old name - style = Pack() - set_fn(style, new_name, value) + style = style_with(new_name, value) with pytest.warns(DeprecationWarning): assert get_fn(style, old_name) == value @@ -97,20 +116,21 @@ def test_deprecated_properties( (COLUMN, LTR, CENTER, CENTER), ], ) -@pytest.mark.parametrize("set_fn", (setattr, setitem, setitem_hyphen)) +@pytest.mark.parametrize( + "style_with", + (with_init, with_update, with_setattr, with_setitem, with_setitem_hyphen), +) @pytest.mark.parametrize("get_fn", (getattr, getitem, getitem_hyphen)) @pytest.mark.parametrize("del_fn", (delattr, delitem, delitem_hyphen)) def test_alignment_align_items( - direction, text_direction, alignment, align_items, set_fn, get_fn, del_fn + direction, text_direction, alignment, align_items, style_with, get_fn, del_fn ): """Alignment (with deprecation warning) and align_items map to each other.""" # Set alignment, check align_items with pytest.warns(DeprecationWarning): - style = Pack( - direction=direction, - text_direction=text_direction, - ) - set_fn(style, "alignment", alignment) + style = style_with("alignment", alignment) + style.update(direction=direction, text_direction=text_direction) + assert get_fn(style, "align_items") == align_items # Delete alignment, check align_items @@ -119,11 +139,9 @@ def test_alignment_align_items( assert get_fn(style, "align_items") is None # Set align_items, check alignment - style = Pack( - direction=direction, - text_direction=text_direction, - ) - set_fn(style, "align_items", align_items) + style = style_with("align_items", align_items) + style.update(direction=direction, text_direction=text_direction) + with pytest.warns(DeprecationWarning): assert get_fn(style, "alignment") == alignment From f443700322a43d22aa8edbcb13d45fcfdbed549d Mon Sep 17 00:00:00 2001 From: Charles Whittington Date: Wed, 18 Dec 2024 01:05:43 -0500 Subject: [PATCH 2/4] Added changenote --- core/tests/style/pack/3048.misc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 core/tests/style/pack/3048.misc.rst diff --git a/core/tests/style/pack/3048.misc.rst b/core/tests/style/pack/3048.misc.rst new file mode 100644 index 0000000000..7216e595c8 --- /dev/null +++ b/core/tests/style/pack/3048.misc.rst @@ -0,0 +1 @@ +An issue with PR #3033 that failed when providing ``padding`` to ``Pack()`` and ``Pack.update()`` was fixed. From b93900193eb3159f9ca7963a88c9cdea2e4d5a40 Mon Sep 17 00:00:00 2001 From: Charles Whittington Date: Wed, 18 Dec 2024 01:08:08 -0500 Subject: [PATCH 3/4] ...Moved changenote to the right place --- {core/tests/style/pack => changes}/3048.misc.rst | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {core/tests/style/pack => changes}/3048.misc.rst (100%) diff --git a/core/tests/style/pack/3048.misc.rst b/changes/3048.misc.rst similarity index 100% rename from core/tests/style/pack/3048.misc.rst rename to changes/3048.misc.rst From 33fa916727e5c3f8514c0244998e974a62aefde3 Mon Sep 17 00:00:00 2001 From: Charles Whittington Date: Wed, 18 Dec 2024 01:14:55 -0500 Subject: [PATCH 4/4] More specific docstring for test --- core/tests/style/pack/test_deprecated_properties.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/tests/style/pack/test_deprecated_properties.py b/core/tests/style/pack/test_deprecated_properties.py index d2f1cc6943..6716a2b200 100644 --- a/core/tests/style/pack/test_deprecated_properties.py +++ b/core/tests/style/pack/test_deprecated_properties.py @@ -77,7 +77,7 @@ def delitem_hyphen(obj, name): @pytest.mark.parametrize("get_fn", (getattr, getitem, getitem_hyphen)) @pytest.mark.parametrize("del_fn", (delattr, delitem, delitem_hyphen)) def test_padding_margin(old_name, new_name, value, default, style_with, get_fn, del_fn): - """Deprecated names alias to new names, and issue deprecation warnings.""" + """Padding (with deprecation warning) and margin map to each other.""" # Set the old name, then check the new name with pytest.warns(DeprecationWarning): style = style_with(old_name, value)