Skip to content

Commit eabcfea

Browse files
authored
fix: annotations lazy evaluation (#538)
* fix: annotations lazy evaluation Signed-off-by: Keming <kemingy94@gmail.com> * relax dev dep version Signed-off-by: Keming <kemingy94@gmail.com> --------- Signed-off-by: Keming <kemingy94@gmail.com>
1 parent 98176e9 commit eabcfea

File tree

6 files changed

+41
-18
lines changed

6 files changed

+41
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mosec"
3-
version = "0.8.4"
3+
version = "0.8.6"
44
authors = ["Keming <kemingy94@gmail.com>", "Zichen <lkevinzc@gmail.com>"]
55
edition = "2021"
66
license = "Apache-2.0"

mosec/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def make_body(description, mime, schema):
301301
if not return_schema
302302
else {
303303
"200": make_body(
304-
"Mosec Inference Result",
304+
"Mosec Inference Response",
305305
response_worker_cls.resp_mime_type,
306306
return_schema,
307307
)

mosec/utils/types.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,32 @@
1515
"""Provide useful utils to inspect function type."""
1616

1717
import inspect
18+
import sys
1819
from enum import Enum
19-
from typing import List
20+
from typing import Any, List
21+
22+
23+
def get_annotations(func) -> dict:
24+
"""Get the annotations of a class method.
25+
26+
This will evaluation the annotations of the method and return a dict.
27+
The implementation is based on the `inspect.get_annotations` (Py>=3.10).
28+
29+
``eval_str=True`` since ``from __future__ import annotations`` will change
30+
all the annotations to string.
31+
"""
32+
if sys.version_info >= (3, 10):
33+
return inspect.get_annotations(func, eval_str=True)
34+
annotations = getattr(func, "__annotations__", None)
35+
obj_globals = getattr(func, "__globals__", None)
36+
if annotations is None:
37+
return {}
38+
if not isinstance(annotations, dict):
39+
raise TypeError(f"{func.__name__} annotations must be a dict or None")
40+
return {
41+
key: value if not isinstance(value, str) else eval(value, obj_globals)
42+
for key, value in annotations.items()
43+
}
2044

2145

2246
class ParseTarget(Enum):
@@ -32,22 +56,20 @@ def parse_func_type(func, target: ParseTarget) -> type:
3256
- single request: return the type
3357
- batch request: return the list item type
3458
"""
35-
sig = inspect.signature(func)
36-
index = 0 if inspect.ismethod(func) else 1
59+
annotations = get_annotations(func)
3760
name = func.__name__
61+
typ = Any
3862
if target == ParseTarget.INPUT:
39-
params = list(sig.parameters.values())
40-
if len(params) < index + 1:
41-
raise TypeError(
42-
f"`{name}` method doesn't have enough({index + 1}) parameters"
43-
)
44-
typ = params[index].annotation
63+
for key in annotations:
64+
if key != "return":
65+
typ = annotations[key]
66+
break
4567
else:
46-
typ = sig.return_annotation
68+
typ = annotations.get("return", Any)
4769

4870
origin = getattr(typ, "__origin__", None)
4971
if origin is None:
50-
return typ
72+
return typ # type: ignore
5173
# GenericAlias, `func` could be batch inference
5274
if origin is list or origin is List:
5375
if not hasattr(typ, "__args__") or len(typ.__args__) != 1: # type: ignore

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ changelog = "https://github.com/mosecorg/mosec/releases"
3737

3838
[tool.cibuildwheel]
3939
build-frontend = "build"
40-
skip = ["cp36-*", "*-musllinux_*", "pp*"]
40+
skip = ["cp36-*", "cp37-*", "*-musllinux_*", "pp*"]
4141
archs = ["auto64"]
4242
before-all = "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y"
4343
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"]
5454
write_to = "mosec/_version.py"
5555

5656
[tool.mypy]
57+
python_version = "3.8"
5758
warn_redundant_casts = true
5859
warn_unreachable = true
5960
pretty = true

requirements/dev.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
setuptools_scm>=7
22
pytest>=6
33
pytest-mock>=3.5
4-
mypy>=0.910
5-
pyright>=1.1.290
6-
ruff~=0.4.7
4+
mypy~=1.10
5+
pyright~=1.1
6+
ruff~=0.4
77
pre-commit>=2.15.0
88
msgpack>=1.0.5
99
numpy>=1.24

0 commit comments

Comments
 (0)