From eabcfea712cb399fb7fff4061a0adf300790938a Mon Sep 17 00:00:00 2001 From: Keming Date: Sun, 16 Jun 2024 13:06:32 +0800 Subject: [PATCH] fix: annotations lazy evaluation (#538) * fix: annotations lazy evaluation Signed-off-by: Keming * relax dev dep version Signed-off-by: Keming --------- Signed-off-by: Keming --- Cargo.lock | 2 +- Cargo.toml | 2 +- mosec/server.py | 2 +- mosec/utils/types.py | 44 +++++++++++++++++++++++++++++++++----------- pyproject.toml | 3 ++- requirements/dev.txt | 6 +++--- 6 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 38f00306..80b3fa22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -674,7 +674,7 @@ dependencies = [ [[package]] name = "mosec" -version = "0.8.4" +version = "0.8.6" dependencies = [ "async-channel", "async-stream", diff --git a/Cargo.toml b/Cargo.toml index e099d524..a169e56e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mosec" -version = "0.8.4" +version = "0.8.6" authors = ["Keming ", "Zichen "] edition = "2021" license = "Apache-2.0" diff --git a/mosec/server.py b/mosec/server.py index 07303bb3..042bbc2d 100644 --- a/mosec/server.py +++ b/mosec/server.py @@ -301,7 +301,7 @@ def make_body(description, mime, schema): if not return_schema else { "200": make_body( - "Mosec Inference Result", + "Mosec Inference Response", response_worker_cls.resp_mime_type, return_schema, ) diff --git a/mosec/utils/types.py b/mosec/utils/types.py index 672e57f8..5abc81aa 100644 --- a/mosec/utils/types.py +++ b/mosec/utils/types.py @@ -15,8 +15,32 @@ """Provide useful utils to inspect function type.""" import inspect +import sys from enum import Enum -from typing import List +from typing import Any, List + + +def get_annotations(func) -> dict: + """Get the annotations of a class method. + + This will evaluation the annotations of the method and return a dict. + The implementation is based on the `inspect.get_annotations` (Py>=3.10). + + ``eval_str=True`` since ``from __future__ import annotations`` will change + all the annotations to string. + """ + if sys.version_info >= (3, 10): + return inspect.get_annotations(func, eval_str=True) + annotations = getattr(func, "__annotations__", None) + obj_globals = getattr(func, "__globals__", None) + if annotations is None: + return {} + if not isinstance(annotations, dict): + raise TypeError(f"{func.__name__} annotations must be a dict or None") + return { + key: value if not isinstance(value, str) else eval(value, obj_globals) + for key, value in annotations.items() + } class ParseTarget(Enum): @@ -32,22 +56,20 @@ def parse_func_type(func, target: ParseTarget) -> type: - single request: return the type - batch request: return the list item type """ - sig = inspect.signature(func) - index = 0 if inspect.ismethod(func) else 1 + annotations = get_annotations(func) name = func.__name__ + typ = Any if target == ParseTarget.INPUT: - params = list(sig.parameters.values()) - if len(params) < index + 1: - raise TypeError( - f"`{name}` method doesn't have enough({index + 1}) parameters" - ) - typ = params[index].annotation + for key in annotations: + if key != "return": + typ = annotations[key] + break else: - typ = sig.return_annotation + typ = annotations.get("return", Any) origin = getattr(typ, "__origin__", None) if origin is None: - return typ + return typ # type: ignore # GenericAlias, `func` could be batch inference if origin is list or origin is List: if not hasattr(typ, "__args__") or len(typ.__args__) != 1: # type: ignore diff --git a/pyproject.toml b/pyproject.toml index 064d2b9b..a68f0177 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,7 +37,7 @@ changelog = "https://github.com/mosecorg/mosec/releases" [tool.cibuildwheel] build-frontend = "build" -skip = ["cp36-*", "*-musllinux_*", "pp*"] +skip = ["cp36-*", "cp37-*", "*-musllinux_*", "pp*"] archs = ["auto64"] before-all = "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y" environment = { PRODUCTION_MODE="yes", PATH="$PATH:$HOME/.cargo/bin", PIP_NO_CLEAN="yes" } @@ -54,6 +54,7 @@ requires = ["setuptools>=45", "wheel", "setuptools_scm>=7.0"] write_to = "mosec/_version.py" [tool.mypy] +python_version = "3.8" warn_redundant_casts = true warn_unreachable = true pretty = true diff --git a/requirements/dev.txt b/requirements/dev.txt index 9e77e944..9494dc98 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -1,9 +1,9 @@ setuptools_scm>=7 pytest>=6 pytest-mock>=3.5 -mypy>=0.910 -pyright>=1.1.290 -ruff~=0.4.7 +mypy~=1.10 +pyright~=1.1 +ruff~=0.4 pre-commit>=2.15.0 msgpack>=1.0.5 numpy>=1.24