From 4dc7178c1e0349f8dd57d2cead55327cafa986ce Mon Sep 17 00:00:00 2001 From: c-bata Date: Fri, 13 Jan 2023 23:41:27 +0900 Subject: [PATCH] Make typing_extensions optional again --- optuna_dashboard/_app.py | 4 ++-- optuna_dashboard/artifact/__init__.py | 1 - optuna_dashboard/artifact/_backend.py | 20 ++------------------ optuna_dashboard/artifact/boto3.py | 2 +- optuna_dashboard/artifact/file_system.py | 2 +- optuna_dashboard/artifact/protocol.py | 13 +++++++++++++ pyproject.toml | 1 - 7 files changed, 19 insertions(+), 24 deletions(-) create mode 100644 optuna_dashboard/artifact/protocol.py diff --git a/optuna_dashboard/_app.py b/optuna_dashboard/_app.py index 3ad91c15..01405406 100644 --- a/optuna_dashboard/_app.py +++ b/optuna_dashboard/_app.py @@ -29,7 +29,6 @@ from optuna.version import __version__ as optuna_ver from packaging import version from . import _note as note -from . import artifact from ._bottle_util import BottleViewReturn from ._bottle_util import json_api_view from ._cached_extra_study_property import get_cached_extra_study_property @@ -43,6 +42,7 @@ from .artifact._backend import register_artifact_route if typing.TYPE_CHECKING: from _typeshed.wsgi import WSGIApplication + from optuna_dashboard.artifact.protocol import ArtifactBackend try: from optuna.study._frozen import FrozenStudy @@ -182,7 +182,7 @@ def get_trials(storage: BaseStorage, study_id: int, ttl_seconds: int = 10) -> li def create_app( storage: BaseStorage, - artifact_backend: Optional[artifact.ArtifactBackend] = None, + artifact_backend: Optional[ArtifactBackend] = None, debug: bool = False, ) -> Bottle: app = Bottle() diff --git a/optuna_dashboard/artifact/__init__.py b/optuna_dashboard/artifact/__init__.py index 10a0c2ee..786e056a 100644 --- a/optuna_dashboard/artifact/__init__.py +++ b/optuna_dashboard/artifact/__init__.py @@ -1,2 +1 @@ -from ._backend import ArtifactBackend # noqa from ._backend import upload_artifact # noqa diff --git a/optuna_dashboard/artifact/_backend.py b/optuna_dashboard/artifact/_backend.py index 362b05a2..fd282de6 100644 --- a/optuna_dashboard/artifact/_backend.py +++ b/optuna_dashboard/artifact/_backend.py @@ -15,20 +15,15 @@ from .._bottle_util import json_api_view from .._bottle_util import parse_data_uri -try: - from typing import Protocol -except ImportError: - from typing_extensions import Protocol # type: ignore - - if TYPE_CHECKING: from typing import Any - from typing import BinaryIO from typing import Optional from typing import TypedDict from optuna.storages import BaseStorage + from .protocol import ArtifactBackend + ArtifactMeta = TypedDict( "ArtifactMeta", { @@ -44,17 +39,6 @@ ARTIFACTS_ATTR_PREFIX = "dashboard:artifacts:" DEFAULT_MIME_TYPE = "application/octet-stream" -class ArtifactBackend(Protocol): - def open(self, artifact_id: str) -> BinaryIO: - ... - - def write(self, artifact_id: str, content_body: BinaryIO) -> None: - ... - - def remove(self, artifact_id: str) -> None: - ... - - def register_artifact_route( app: Bottle, storage: BaseStorage, artifact_backend: Optional[ArtifactBackend] ) -> None: diff --git a/optuna_dashboard/artifact/boto3.py b/optuna_dashboard/artifact/boto3.py index 2eee30c4..97341e83 100644 --- a/optuna_dashboard/artifact/boto3.py +++ b/optuna_dashboard/artifact/boto3.py @@ -51,6 +51,6 @@ class Boto3Backend: if TYPE_CHECKING: # A mypy-runtime assertion to ensure that Boto3Backend # implements all abstract methods in ArtifactBackendProtocol. - from ._backend import ArtifactBackend + from .protocol import ArtifactBackend _: ArtifactBackend = Boto3Backend("") diff --git a/optuna_dashboard/artifact/file_system.py b/optuna_dashboard/artifact/file_system.py index 651c9bfd..45e530f1 100644 --- a/optuna_dashboard/artifact/file_system.py +++ b/optuna_dashboard/artifact/file_system.py @@ -26,6 +26,6 @@ class FileSystemBackend: if TYPE_CHECKING: # A mypy-runtime assertion to ensure that LocalArtifactBackend # implements all abstract methods in ArtifactBackendProtocol. - from ._backend import ArtifactBackend + from .protocol import ArtifactBackend _: ArtifactBackend = FileSystemBackend("") diff --git a/optuna_dashboard/artifact/protocol.py b/optuna_dashboard/artifact/protocol.py new file mode 100644 index 00000000..b5ef775a --- /dev/null +++ b/optuna_dashboard/artifact/protocol.py @@ -0,0 +1,13 @@ +from typing import BinaryIO +from typing import Protocol + + +class ArtifactBackend(Protocol): + def open(self, artifact_id: str) -> BinaryIO: + ... + + def write(self, artifact_id: str, content_body: BinaryIO) -> None: + ... + + def remove(self, artifact_id: str) -> None: + ... diff --git a/pyproject.toml b/pyproject.toml index 51283cb0..96d29c75 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,6 @@ dependencies = [ "optuna>=2.4.0", "packaging", "scikit-learn", - 'typing-extensions; python_version<"3.8"', ] dynamic = ["version"]