Skip to content

Commit

Permalink
fix: unicode in models
Browse files Browse the repository at this point in the history
  • Loading branch information
bvqbao committed Jan 15, 2025
1 parent cc2a56d commit b773f5b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libs/core/kiln_ai/datamodel/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def load_from_file(cls: Type[T], path: Path | str, readonly: bool = False) -> T:
cached_model = ModelCache.shared().get_model(path, cls, readonly=readonly)
if cached_model is not None:
return cached_model
with open(path, "r") as file:
with open(path, "r", encoding="utf-8") as file:
# modified time of file for cache invalidation. From file descriptor so it's atomic w read.
mtime_ns = os.fstat(file.fileno()).st_mtime_ns
file_data = file.read()
Expand Down Expand Up @@ -198,7 +198,7 @@ def save_to_file(self) -> None:
)
path.parent.mkdir(parents=True, exist_ok=True)
json_data = self.model_dump_json(indent=2, exclude={"path"})
with open(path, "w") as file:
with open(path, "w", encoding="utf-8") as file:
file.write(json_data)
# save the path so even if something like name changes, the file doesn't move
self.path = path
Expand Down
14 changes: 14 additions & 0 deletions libs/core/kiln_ai/datamodel/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ def test_save_to_file(test_project_file):
assert data["description"] == "Test Description"


def test_save_to_file_non_ascii(test_project_file):
project = Project(
name="Test Project", description="Chúc mừng!", path=test_project_file
)
project.save_to_file()

with open(test_project_file, "r", encoding="utf-8") as file:
data = json.load(file)

assert data["v"] == 1
assert data["name"] == "Test Project"
assert data["description"] == "Chúc mừng!"


def test_task_defaults():
task = Task(name="Test Task", instruction="Test Instruction")
assert task.description is None
Expand Down

0 comments on commit b773f5b

Please sign in to comment.