Skip to content

Commit c58d14f

Browse files
authored
py: change str repr to mitigate confusion (kubeflow#1106)
* py: change str repr to mitigate confusion Signed-off-by: Matteo Mortari <[email protected]> * linting Signed-off-by: Matteo Mortari <[email protected]> --------- Signed-off-by: Matteo Mortari <[email protected]>
1 parent 8d48d2c commit c58d14f

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

clients/python/src/model_registry/types/contexts.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ def from_basemodel(cls, source: ModelVersionBaseModel) -> ModelVersion:
8282
else None,
8383
)
8484

85+
def __repr_str__(self, join_str: str) -> str:
86+
"""Overrides pydantic Representation.__repr_str__ to bring name to the front."""
87+
result = []
88+
for a, v in self.__repr_args__():
89+
if a is None:
90+
result.append(repr(v))
91+
elif a == "name":
92+
result.insert(0, f"{a}={v!r}")
93+
else:
94+
result.append(f"{a}={v!r}")
95+
return join_str.join(result)
96+
8597

8698
class RegisteredModel(BaseResourceModel):
8799
"""Represents a registered model.
@@ -131,3 +143,15 @@ def from_basemodel(cls, source: RegisteredModelBaseModel) -> RegisteredModel:
131143
if source.custom_properties
132144
else None,
133145
)
146+
147+
def __repr_str__(self, join_str: str) -> str:
148+
"""Overrides pydantic Representation.__repr_str__ to bring name to the front."""
149+
result = []
150+
for a, v in self.__repr_args__():
151+
if a is None:
152+
result.append(repr(v))
153+
elif a == "name":
154+
result.insert(0, f"{a}={v!r}")
155+
else:
156+
result.append(f"{a}={v!r}")
157+
return join_str.join(result)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from model_registry.types.contexts import ModelVersion, RegisteredModel
2+
3+
4+
def test_model_version_repr():
5+
"""Test the repr of a ModelVersion, with name to the front, ensuring other fields are also present"""
6+
mv = ModelVersion(name="Version 1", id="123", author="test_author", description="test_description")
7+
print(mv)
8+
assert str(mv).startswith("name='Version 1'")
9+
assert "id='123'" in str(mv)
10+
assert "author='test_author'" in str(mv)
11+
assert "description='test_description'" in str(mv)
12+
print(repr(mv))
13+
assert repr(mv).startswith("ModelVersion(name='Version 1'")
14+
assert "id='123'" in str(mv)
15+
assert "author='test_author'" in str(mv)
16+
assert "description='test_description'" in str(mv)
17+
18+
# Test with empty name, not really used in practice but to increase coverage
19+
mv = ModelVersion(name="", id="123", author="test_author", description="test_description")
20+
print(mv)
21+
assert str(mv).startswith("name=''")
22+
assert "id='123'" in str(mv)
23+
assert "author='test_author'" in str(mv)
24+
assert "description='test_description'" in str(mv)
25+
print(repr(mv))
26+
assert repr(mv).startswith("ModelVersion(name=''")
27+
assert "id='123'" in str(mv)
28+
assert "author='test_author'" in str(mv)
29+
assert "description='test_description'" in str(mv)
30+
31+
32+
def test_registered_model_repr():
33+
"""Test the repr of a RegisteredModel, with name to the front, ensuring other fields are also present"""
34+
rm = RegisteredModel(name="Model 1", id="123", owner="test_owner", description="test_description")
35+
print(rm)
36+
assert str(rm).startswith("name='Model 1'")
37+
assert "id='123'" in str(rm)
38+
assert "owner='test_owner'" in str(rm)
39+
assert "description='test_description'" in str(rm)
40+
print(repr(rm))
41+
assert repr(rm).startswith("RegisteredModel(name='Model 1'")
42+
assert "id='123'" in str(rm)
43+
assert "owner='test_owner'" in str(rm)
44+
assert "description='test_description'" in str(rm)
45+
46+
# Test with empty name, not really used in practice but to increase coverage
47+
rm = RegisteredModel(name="", id="123", owner="test_owner", description="test_description")
48+
print(rm)
49+
assert str(rm).startswith("name=''")
50+
assert "id='123'" in str(rm)
51+
assert "owner='test_owner'" in str(rm)
52+
assert "description='test_description'" in str(rm)
53+
print(repr(rm))
54+
assert repr(rm).startswith("RegisteredModel(name=''")
55+
assert "id='123'" in str(rm)
56+
assert "owner='test_owner'" in str(rm)
57+
assert "description='test_description'" in str(rm)

0 commit comments

Comments
 (0)