Skip to content

Commit 700ff10

Browse files
author
Datata1
committed
adress pr feedback
1 parent e23dd98 commit 700ff10

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

src/codesphere/core/base.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from typing import Any, Generic, List, TypeVar
22

3+
import yaml
34
from pydantic import BaseModel, ConfigDict, RootModel
45
from pydantic.alias_generators import to_camel
56

67
from ..http_client import APIHttpClient
78
from .handler import _APIOperationExecutor
89

9-
ModelT = TypeVar("ModelT")
10+
ModelT = TypeVar("ModelT", bound=BaseModel)
1011

1112

1213
class ResourceBase(_APIOperationExecutor):
@@ -59,25 +60,13 @@ def to_json(
5960
def to_yaml(self, *, by_alias: bool = True, exclude_none: bool = False) -> str:
6061
"""Export model as a YAML string.
6162
62-
Requires PyYAML to be installed (optional dependency).
63-
6463
Args:
6564
by_alias: Use camelCase keys (API format) if True, snake_case if False.
6665
exclude_none: Exclude fields with None values if True.
6766
6867
Returns:
6968
YAML string representation of the model.
70-
71-
Raises:
72-
ImportError: If PyYAML is not installed.
7369
"""
74-
try:
75-
import yaml
76-
except ImportError:
77-
raise ImportError(
78-
"PyYAML is required for YAML export. "
79-
"Install it with: pip install pyyaml"
80-
)
8170
data = self.to_dict(by_alias=by_alias, exclude_none=exclude_none)
8271
return yaml.dump(
8372
data, default_flow_style=False, allow_unicode=True, sort_keys=False
@@ -110,8 +99,6 @@ def to_list(
11099
"""
111100
return [
112101
item.model_dump(by_alias=by_alias, exclude_none=exclude_none)
113-
if hasattr(item, "model_dump")
114-
else item
115102
for item in self.root
116103
]
117104

@@ -141,25 +128,13 @@ def to_json(
141128
def to_yaml(self, *, by_alias: bool = True, exclude_none: bool = False) -> str:
142129
"""Export all items as a YAML string.
143130
144-
Requires PyYAML to be installed (optional dependency).
145-
146131
Args:
147132
by_alias: Use camelCase keys (API format) if True, snake_case if False.
148133
exclude_none: Exclude fields with None values if True.
149134
150135
Returns:
151136
YAML string representation.
152-
153-
Raises:
154-
ImportError: If PyYAML is not installed.
155137
"""
156-
try:
157-
import yaml
158-
except ImportError:
159-
raise ImportError(
160-
"PyYAML is required for YAML export. "
161-
"Install it with: pip install pyyaml"
162-
)
163138
return yaml.dump(
164139
self.to_list(by_alias=by_alias, exclude_none=exclude_none),
165140
default_flow_style=False,

tests/core/test_base.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from dataclasses import dataclass
23
from unittest.mock import MagicMock
34

@@ -140,7 +141,7 @@ class SampleModel(CamelModel):
140141
model = SampleModel(team_id=42)
141142
result = model.to_json()
142143

143-
assert result == '{"teamId":42}'
144+
assert json.loads(result) == {"teamId": 42}
144145

145146
def test_to_json_with_indent(self):
146147
"""to_json with indent should format output."""
@@ -151,22 +152,34 @@ class SampleModel(CamelModel):
151152
model = SampleModel(team_id=42)
152153
result = model.to_json(indent=2)
153154

154-
assert '"teamId": 42' in result
155+
assert json.loads(result) == {"teamId": 42}
155156
assert "\n" in result
156157

157-
def test_to_yaml_import_error(self):
158-
"""to_yaml should raise ImportError if PyYAML is not installed."""
159-
import sys
160-
from unittest.mock import patch
158+
def test_to_yaml_default(self):
159+
"""to_yaml should export as YAML string with camelCase keys."""
160+
161+
class SampleModel(CamelModel):
162+
team_id: int
163+
user_name: str
164+
165+
model = SampleModel(team_id=1, user_name="test")
166+
result = model.to_yaml()
167+
168+
assert "teamId: 1" in result
169+
assert "userName: test" in result
170+
171+
def test_to_yaml_snake_case(self):
172+
"""to_yaml with by_alias=False should use snake_case keys."""
161173

162174
class SampleModel(CamelModel):
163175
team_id: int
164176

165177
model = SampleModel(team_id=1)
178+
result = model.to_yaml(by_alias=False)
179+
180+
assert "team_id: 1" in result
181+
166182

167-
with patch.dict(sys.modules, {"yaml": None}):
168-
with pytest.raises(ImportError, match="PyYAML is required"):
169-
model.to_yaml()
170183
class TestResourceList:
171184
def test_create_with_list(self):
172185
"""ResourceList should be created with a list of items."""
@@ -267,7 +280,7 @@ class Item(CamelModel):
267280
resource_list = ResourceList[Item](root=items)
268281
result = resource_list.to_json()
269282

270-
assert result == '[{"itemId": 1}, {"itemId": 2}]'
283+
assert json.loads(result) == [{"itemId": 1}, {"itemId": 2}]
271284

272285
def test_to_json_with_indent(self):
273286
"""to_json with indent should format output."""
@@ -293,6 +306,19 @@ class Item(CamelModel):
293306

294307
assert result == []
295308

309+
def test_to_yaml_default(self):
310+
"""to_yaml should export as YAML string."""
311+
312+
class Item(CamelModel):
313+
item_id: int
314+
315+
items = [Item(item_id=1), Item(item_id=2)]
316+
resource_list = ResourceList[Item](root=items)
317+
result = resource_list.to_yaml()
318+
319+
assert "itemId: 1" in result
320+
assert "itemId: 2" in result
321+
296322

297323
class TestResourceBase:
298324
def test_initialization_with_http_client(self):

0 commit comments

Comments
 (0)