diff --git a/optuna_dashboard/artifact/_backend_to_store.py b/optuna_dashboard/artifact/_backend_to_store.py index 9fe41aa3c..0532e6268 100644 --- a/optuna_dashboard/artifact/_backend_to_store.py +++ b/optuna_dashboard/artifact/_backend_to_store.py @@ -2,6 +2,8 @@ from typing import TYPE_CHECKING +from optuna_dashboard.artifact.exceptions import ArtifactNotFound as DashboardArtifactNotFound + if TYPE_CHECKING: from typing import BinaryIO @@ -30,10 +32,20 @@ def __init__(self, artifact_backend: ArtifactBackend) -> None: self._backend = artifact_backend def open_reader(self, artifact_id: str) -> BinaryIO: - return self._backend.open(artifact_id) + from optuna.artifacts.exceptions import ArtifactNotFound + + try: + return self._backend.open(artifact_id) + except DashboardArtifactNotFound as e: + raise ArtifactNotFound from e def write(self, artifact_id: str, content_body: BinaryIO) -> None: self._backend.write(artifact_id, content_body) def remove(self, artifact_id: str) -> None: - self._backend.remove(artifact_id) + from optuna.artifacts.exceptions import ArtifactNotFound + + try: + self._backend.remove(artifact_id) + except DashboardArtifactNotFound as e: + raise ArtifactNotFound from e diff --git a/python_tests/artifact/test_backend.py b/python_tests/artifact/test_backend.py index 165c50299..8325343b0 100644 --- a/python_tests/artifact/test_backend.py +++ b/python_tests/artifact/test_backend.py @@ -9,6 +9,9 @@ from optuna.storages import BaseStorage from optuna_dashboard._app import create_app from optuna_dashboard.artifact import _backend +from optuna_dashboard.artifact import upload_artifact as dashboard_upload_artifact +from optuna_dashboard.artifact._backend_to_store import to_artifact_store +from optuna_dashboard.artifact.file_system import FileSystemBackend import pytest from ..wsgi_client import send_request @@ -298,3 +301,31 @@ def test_delete_trial_artifact() -> None: "DELETE", ) assert status == 404 + + +# Check the backward compatibility with the artifact backend. +def test_delete_artifact_with_dashboard_file_system_backend() -> None: + storage = optuna.storages.InMemoryStorage() + study = optuna.create_study(storage=storage) + trial = study.ask() + with tempfile.TemporaryDirectory() as tmpdir: + artifact_backend = FileSystemBackend(tmpdir) + with tempfile.NamedTemporaryFile() as f: + f.write(b"dummy_content") + f.flush() + artifact_id = dashboard_upload_artifact(artifact_backend, trial, f.name) + + artifact_store = to_artifact_store(artifact_backend) + app = create_app(storage, artifact_store) + status, _, _ = send_request( + app, + f"/api/artifacts/{study._study_id}/{trial._trial_id}/{artifact_id}", + "DELETE", + ) + assert status == 204 + status, _, _ = send_request( + app, + f"/api/artifacts/{study._study_id}/{trial._trial_id}/{artifact_id}", + "DELETE", + ) + assert status == 404