Merge pull request #753 from c-bata/dashboard-artifact-exception

Convert Dashboard's ArtifactNotFound exception to Optuna's one
This commit is contained in:
c-bata authored and GitHub committed 2024-01-16 12:23:01 +09:00
commit 7aa7bf9361
2 files changed
+45 -2

No files matched your search

+14 -2
View File
@@ -2,6 +2,8 @@ from __future__ import annotations
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 @@ class ArtifactBackendToStore:
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
+31
View File
@@ -9,6 +9,9 @@ from optuna.artifacts import upload_artifact
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