Skip to content

Commit

Permalink
Convert Dashboard's ArtifactNotFound exception to Optuna's one
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bata committed Jan 16, 2024
1 parent 99a54cb commit 29eb29f
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

Check warning on line 35 in optuna_dashboard/artifact/_backend_to_store.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/artifact/_backend_to_store.py#L35

Added line #L35 was not covered by tests

try:
return self._backend.open(artifact_id)
except DashboardArtifactNotFound as e:
raise ArtifactNotFound from e

Check warning on line 40 in optuna_dashboard/artifact/_backend_to_store.py

View check run for this annotation

Codecov / codecov/patch

optuna_dashboard/artifact/_backend_to_store.py#L37-L40

Added lines #L37 - L40 were not covered by tests

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._backend_to_store import to_artifact_store
from optuna_dashboard.artifact.file_system import FileSystemBackend
from optuna_dashboard.artifact import upload_artifact as dashboard_upload_artifact
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 29eb29f

Please sign in to comment.