Skip to content

Commit d13d35c

Browse files
committed
MPT-15233 Fix for snake-case to camel-case conversion
- Disabled camel_killer_box - Renamed Model._resource_data to Model._box for better naming clarity - Added models representation
1 parent 262d20c commit d13d35c

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

mpt_api_client/models/model.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
ResourceData = dict[str, Any]
99

1010

11-
class Model:
11+
class Model: # noqa: WPS214
1212
"""Provides a resource to interact with api data using fluent interfaces."""
1313

1414
_data_key: ClassVar[str | None] = None
15-
_safe_attributes: ClassVar[list[str]] = ["meta", "_resource_data"]
15+
_safe_attributes: ClassVar[list[str]] = ["meta", "_box"]
1616

1717
def __init__(self, resource_data: ResourceData | None = None, meta: Meta | None = None) -> None:
1818
self.meta = meta
19-
self._resource_data = Box(resource_data or {}, camel_killer_box=True, default_box=False)
19+
self._box = Box(resource_data or {}, camel_killer_box=False, default_box=False)
2020

2121
@classmethod
2222
def new(cls, resource_data: ResourceData | None = None, meta: Meta | None = None) -> Self:
@@ -25,15 +25,15 @@ def new(cls, resource_data: ResourceData | None = None, meta: Meta | None = None
2525

2626
def __getattr__(self, attribute: str) -> Box | Any:
2727
"""Returns the resource data."""
28-
return self._resource_data.__getattr__(attribute) # type: ignore[no-untyped-call]
28+
return self._box.__getattr__(attribute) # type: ignore[no-untyped-call]
2929

3030
@override
3131
def __setattr__(self, attribute: str, attribute_value: Any) -> None:
3232
if attribute in self._safe_attributes:
3333
object.__setattr__(self, attribute, attribute_value)
3434
return
3535

36-
self._resource_data.__setattr__(attribute, attribute_value) # type: ignore[no-untyped-call]
36+
self._box.__setattr__(attribute, attribute_value) # type: ignore[no-untyped-call]
3737

3838
@classmethod
3939
def from_response(cls, response: Response) -> Self:
@@ -55,8 +55,12 @@ def from_response(cls, response: Response) -> Self:
5555
@property
5656
def id(self) -> str:
5757
"""Returns the resource ID."""
58-
return str(self._resource_data.get("id", "")) # type: ignore[no-untyped-call]
58+
return str(self._box.get("id", "")) # type: ignore[no-untyped-call]
5959

6060
def to_dict(self) -> dict[str, Any]:
6161
"""Returns the resource as a dictionary."""
62-
return self._resource_data.to_dict()
62+
return self._box.to_dict()
63+
64+
@override
65+
def __repr__(self) -> str:
66+
return f"<{self.__class__.__name__} {self.id}>" # noqa: WPS237

tests/unit/models/resource/test_resource.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,23 @@ def test_id_property_with_numeric_id():
7070

7171
assert resource.id == "1024"
7272
assert isinstance(resource.id, str)
73+
74+
75+
def test_case_conversion():
76+
resource_data = {"id": "abc-123", "FullName": "Alice Smith"}
77+
78+
resource = Model(resource_data)
79+
80+
assert resource.FullName == "Alice Smith"
81+
assert resource.to_dict() == resource_data
82+
with pytest.raises(AttributeError):
83+
resource.full_name # noqa: B018
84+
85+
86+
def test_repr():
87+
resource_data = {"id": "abc-123", "FullName": "Alice Smith"}
88+
89+
resource = Model(resource_data)
90+
91+
assert repr(resource) == "<Model abc-123>"
92+
assert str(resource) == "<Model abc-123>"

0 commit comments

Comments
 (0)