diff --git a/changes/3048.misc.rst b/changes/3048.misc.rst new file mode 100644 index 0000000000..7216e595c8 --- /dev/null +++ b/changes/3048.misc.rst @@ -0,0 +1 @@ +An issue with PR #3033 that failed when providing ``padding`` to ``Pack()`` and ``Pack.update()`` was fixed. 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..6716a2b200 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 -): - """Deprecated names alias to new names, and issue deprecation warnings.""" +def test_padding_margin(old_name, new_name, value, default, style_with, get_fn, del_fn): + """Padding (with deprecation warning) and margin map to each other.""" # 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