Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed value check in property assignment #234

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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