Skip to content

Commit

Permalink
Merge pull request #753 from c-bata/dashboard-artifact-exception
Browse files Browse the repository at this point in the history
Convert Dashboard's ArtifactNotFound exception to Optuna's one
  • Loading branch information
c-bata authored Jan 16, 2024
2 parents 79f4665 + fd4ef3d commit 7aa7bf9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
16 changes: 14 additions & 2 deletions optuna_dashboard/artifact/_backend_to_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
31 changes: 31 additions & 0 deletions python_tests/artifact/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 7aa7bf9

Please sign in to comment.