Skip to content

Commit

Permalink
Merge pull request #3048 from HalfWhitt/fix_update_init
Browse files Browse the repository at this point in the history
Fixed update/init with padding
  • Loading branch information
freakboy3742 authored Dec 18, 2024
2 parents 6d6a244 + 33fa916 commit c69f5e4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 25 deletions.
1 change: 1 addition & 0 deletions changes/3048.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
An issue with PR #3033 that failed when providing ``padding`` to ``Pack()`` and ``Pack.update()`` was fixed.
7 changes: 7 additions & 0 deletions core/src/toga/style/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
68 changes: 43 additions & 25 deletions core/tests/style/pack/test_deprecated_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit c69f5e4

Please sign in to comment.