Skip to content

Commit

Permalink
Merge pull request #234 from HalfWhitt/fix-apply-check
Browse files Browse the repository at this point in the history
Fixed value check in property assignment
  • Loading branch information
freakboy3742 authored Nov 13, 2024
2 parents ccd7063 + 236961f commit 6be60da
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/234.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed an unnecessary reapply when assigning to a previously unassigned property, if the value being assigned is the same as the property's initial value.
2 changes: 1 addition & 1 deletion src/travertino/declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def __set__(self, obj, value):

value = self.validate(value)

if value != getattr(obj, f"_{self.name}", None):
if value != getattr(obj, f"_{self.name}", self.initial):
setattr(obj, f"_{self.name}", value)
obj.apply(self.name, value)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@ def test_property_with_implicit_default(StyleClass):
style.apply.assert_called_once_with("implicit", None)


@pytest.mark.parametrize("StyleClass", [Style, DeprecatedStyle])
def test_set_initial_no_apply(StyleClass):
"""If a property hasn't been set, assigning it its initial value shouldn't apply."""
style = StyleClass()

# 0 is the initial value
style.explicit_value = 0

style.apply.assert_not_called()


@pytest.mark.parametrize("StyleClass", [Style, DeprecatedStyle])
def test_directional_property(StyleClass):
style = StyleClass()
Expand Down

0 comments on commit 6be60da

Please sign in to comment.