Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(annotations): cleanup compat and fix ModelSignatureDict type #4162

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 16 additions & 68 deletions src/bentoml/_internal/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,8 @@
from datetime import time
from datetime import timedelta
from types import TracebackType
from typing import TYPE_CHECKING

if sys.version_info < (3, 8):
import collections

GenericClass = type(t.List)

BUILTINS_MAPPING = {
t.List: list,
t.Set: set,
t.Dict: dict,
t.Tuple: tuple,
t.ByteString: bytes, # https://docs.python.org/3/library/typing.html#typing.ByteString
t.Callable: collections.abc.Callable,
t.Sequence: collections.abc.Sequence,
type(None): None,
}

def _normalize_aliases(type_: t.Type) -> t.Type:
if isinstance(type_, t.TypeVar):
return type_

if type_ in BUILTINS_MAPPING:
return BUILTINS_MAPPING[type_]
return type_

def get_args(type_: t.Type) -> tuple[t.Type]:
if isinstance(type_, GenericClass) and not type_._special:
res = type_.__args__
if get_origin(type_) is collections.abc.Callable and res[0] is not Ellipsis:
res = (list(res[:-1]), res[-1])
else:
res = ()

return res

def get_origin(type_: t.Type) -> t.Type:
if isinstance(type_, GenericClass) and not type_._special:
ori = type_.__origin__
elif hasattr(type_, "_special") and type_._special:
ori = type_
elif type_ is t.Generic:
ori = t.Generic
else:
ori = None
return ori

else:
from typing import get_args
from typing import get_origin
from typing import get_args
from typing import get_origin

__all__ = [
"MetadataType",
Expand All @@ -81,11 +33,6 @@ def get_origin(type_: t.Type) -> t.Type:

JSON_CHARSET = "utf-8"

if TYPE_CHECKING:
PathType: t.TypeAlias = str | os.PathLike[str]
else:
PathType = t.Union[str, os.PathLike]

MetadataType: t.TypeAlias = t.Union[
str,
bytes,
Expand All @@ -102,8 +49,16 @@ def get_origin(type_: t.Type) -> t.Type:
t.Dict[str, "MetadataType"],
]

if TYPE_CHECKING:
MetadataDict = t.Dict[str, MetadataType]

class ModelSignatureDict(t.TypedDict, total=False):
batchable: bool
batch_dim: t.Union[t.Tuple[int, int], int]
input_spec: t.Optional[t.Union[t.Tuple[AnyType], AnyType]]
output_spec: t.Optional[AnyType]


if t.TYPE_CHECKING:
PathType: t.TypeAlias = str | os.PathLike[str]
JSONSerializable: t.TypeAlias = (
str
| int
Expand All @@ -113,19 +68,12 @@ def get_origin(type_: t.Type) -> t.Type:
| list["JSONSerializable"]
| dict[str, "JSONSerializable"]
)

class ModelSignatureDict(t.TypedDict, total=False):
batchable: bool
batch_dim: tuple[int, int] | int | None
input_spec: tuple[AnyType] | AnyType | None
output_spec: AnyType | None

MetadataDict = t.Dict[str, MetadataType]
else:
PathType = t.Union[str, os.PathLike]
JSONSerializable = t.NewType("JSONSerializable", object)
# NOTE: remove this when registering hook for MetadataType
MetadataDict = dict
ModelSignatureDict = dict

JSONSerializable = t.NewType("JSONSerializable", object)

LifecycleHook = t.Callable[[], t.Union[None, t.Coroutine[t.Any, t.Any, None]]]

Expand Down Expand Up @@ -262,7 +210,7 @@ def issubclass(self, klass: type) -> bool:
return False


if TYPE_CHECKING:
if t.TYPE_CHECKING:
from types import UnionType

AnyType: t.TypeAlias = t.Type[t.Any] | UnionType | LazyType[t.Any]
Expand Down