From 3ca97db4fc5668005ec618f03f49a4465990733d Mon Sep 17 00:00:00 2001 From: Thomas S Date: Fri, 24 Jan 2025 17:30:00 +0100 Subject: [PATCH] fix: Allow sending primitive item with `float("nan")` to frontend --- skore/pyproject.toml | 1 + skore/src/skore/ui/project_routes.py | 5 +++-- skore/tests/integration/ui/test_ui.py | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/skore/pyproject.toml b/skore/pyproject.toml index 425416249..7ce75b8b5 100644 --- a/skore/pyproject.toml +++ b/skore/pyproject.toml @@ -10,6 +10,7 @@ dependencies = [ "joblib", "matplotlib", "numpy", + "orjson", "pandas", "plotly>=5,<6", "pyarrow", diff --git a/skore/src/skore/ui/project_routes.py b/skore/src/skore/ui/project_routes.py index 8c455ad37..bf104a80f 100644 --- a/skore/src/skore/ui/project_routes.py +++ b/skore/src/skore/ui/project_routes.py @@ -8,6 +8,7 @@ from typing import Any from fastapi import APIRouter, HTTPException, Request, status +from fastapi.responses import ORJSONResponse from skore.persistence.item import Item from skore.persistence.view.view import Layout, View @@ -68,7 +69,7 @@ def __project_as_serializable(project: Project) -> SerializableProject: ) -@router.get("/items") +@router.get("/items", response_class=ORJSONResponse) async def get_items(request: Request): """Serialize a project and send it.""" project = request.app.state.project @@ -104,7 +105,7 @@ async def delete_view(request: Request, key: str): return __project_as_serializable(project) -@router.get("/activity") +@router.get("/activity", response_class=ORJSONResponse) async def get_activity( request: Request, after: datetime = datetime(1, 1, 1, 0, 0, 0, 0, timezone.utc), diff --git a/skore/tests/integration/ui/test_ui.py b/skore/tests/integration/ui/test_ui.py index a7f7c9b41..07b766f94 100644 --- a/skore/tests/integration/ui/test_ui.py +++ b/skore/tests/integration/ui/test_ui.py @@ -308,6 +308,33 @@ def test_serialize_primitive_item( } +def test_serialize_primitive_item_with_nan( + client, + in_memory_project, + monkeypatch_datetime, + mock_nowstr, +): + in_memory_project.put("primitive", float("nan")) + response = client.get("/api/project/items") + + assert response.status_code == 200 + assert response.json() == { + "views": {}, + "items": { + "primitive": [ + { + "name": "primitive", + "media_type": "text/markdown", + "value": None, + "updated_at": mock_nowstr, + "created_at": mock_nowstr, + "note": None, + } + ] + }, + } + + def test_serialize_media_item( client, in_memory_project,