Skip to content

Commit c597abe

Browse files
committed
Added __repr__
1 parent 65058c9 commit c597abe

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

core/src/toga/style/pack.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ def warn_if_deprecated(self):
179179
######################################################################
180180

181181
if sys.version_info < (3, 10):
182-
_DATACLASS_KWARGS = {"init": False}
182+
_DATACLASS_KWARGS = {"init": False, "repr": False}
183183
else:
184-
_DATACLASS_KWARGS = {"kw_only": True}
184+
_DATACLASS_KWARGS = {"kw_only": True, "repr": False}
185185

186186

187187
@dataclass(**_DATACLASS_KWARGS)

travertino/src/travertino/style.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,18 @@ def __ior__(self, other):
207207

208208
def __str__(self):
209209
return "; ".join(
210-
f"{name.replace('_', '-')}: {value}" for name, value in sorted(self.items())
210+
[
211+
f"{name.replace('_', '-')}: {value}"
212+
for name, value in sorted(self.items())
213+
]
211214
)
212215

216+
def __repr__(self):
217+
properties = ", ".join(
218+
[f"{name}={repr(value)}" for name, value in sorted(self.items())]
219+
)
220+
return f"{type(self).__name__}({properties})"
221+
213222
######################################################################
214223
# Backwards compatibility
215224
######################################################################

travertino/tests/test_style.py

+16
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,22 @@ def test_str(StyleClass):
704704
)
705705

706706

707+
def test_repr():
708+
# Doesn't need to be tested with deprecated API.
709+
style = Style(explicit_const=VALUE2, explicit_value=20, thing=(30, 40, 50, 60))
710+
711+
assert repr(style) == (
712+
"Style("
713+
"explicit_const='value2', "
714+
"explicit_value=20, "
715+
"thing_bottom=50, "
716+
"thing_left=60, "
717+
"thing_right=40, "
718+
"thing_top=30"
719+
")"
720+
)
721+
722+
707723
@pytest.mark.parametrize("StyleClass", [Style, DeprecatedStyle])
708724
def test_dict(StyleClass):
709725
"Style declarations expose a dict-like interface"

travertino/tests/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from travertino.colors import hsl, hsla, rgb, rgba
88

99
if sys.version_info < (3, 10):
10-
_DATACLASS_KWARGS = {"init": False}
10+
_DATACLASS_KWARGS = {"init": False, "repr": False}
1111
else:
12-
_DATACLASS_KWARGS = {"kw_only": True}
12+
_DATACLASS_KWARGS = {"kw_only": True, "repr": False}
1313

1414

1515
def apply_dataclass(cls):

0 commit comments

Comments
 (0)