Skip to content

Commit

Permalink
Merge pull request #250 from DisruptiveLabs/use_get_type_hints
Browse files Browse the repository at this point in the history
Use typing.get_type_hints instead of .__annotations__
  • Loading branch information
kemingy committed Aug 7, 2022
2 parents 75b92d4 + 275bcf9 commit 25a12f9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
8 changes: 5 additions & 3 deletions spectree/plugins/falcon_plugin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
import re
from functools import partial
from typing import Any, Callable, Dict, List, Mapping, Optional
from typing import Any, Callable, Dict, List, Mapping, Optional, get_type_hints

from pydantic import ValidationError

Expand Down Expand Up @@ -213,8 +213,9 @@ def validate(
try:
self.request_validation(_req, query, json, form, headers, cookies)
if self.config.annotations:
annotations = get_type_hints(func)
for name in ("query", "json", "form", "headers", "cookies"):
if func.__annotations__.get(name):
if annotations.get(name):
kwargs[name] = getattr(_req.context, name)

except ValidationError as err:
Expand Down Expand Up @@ -308,8 +309,9 @@ async def validate(
try:
await self.request_validation(_req, query, json, form, headers, cookies)
if self.config.annotations:
annotations = get_type_hints(func)
for name in ("query", "json", "form", "headers", "cookies"):
if func.__annotations__.get(name):
if annotations.get(name):
kwargs[name] = getattr(_req.context, name)

except ValidationError as err:
Expand Down
5 changes: 3 additions & 2 deletions spectree/plugins/flask_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Mapping, Optional, Tuple
from typing import Any, Callable, Mapping, Optional, Tuple, get_type_hints

from pydantic import BaseModel, ValidationError

Expand Down Expand Up @@ -189,8 +189,9 @@ def validate(
try:
self.request_validation(request, query, json, form, headers, cookies)
if self.config.annotations:
annotations = get_type_hints(func)
for name in ("query", "json", "form", "headers", "cookies"):
if func.__annotations__.get(name):
if annotations.get(name):
kwargs[name] = getattr(request.context, name)
except ValidationError as err:
req_validation_error = err
Expand Down
5 changes: 3 additions & 2 deletions spectree/plugins/starlette_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import namedtuple
from functools import partial
from json import JSONDecodeError
from typing import Any, Callable, Optional
from typing import Any, Callable, Optional, get_type_hints

from pydantic import ValidationError

Expand Down Expand Up @@ -100,8 +100,9 @@ async def validate(
try:
await self.request_validation(request, query, json, form, headers, cookies)
if self.config.annotations:
annotations = get_type_hints(func)
for name in ("query", "json", "form", "headers", "cookies"):
if func.__annotations__.get(name):
if annotations.get(name):
kwargs[name] = getattr(request.context, name)
except ValidationError as err:
req_validation_error = err
Expand Down
22 changes: 16 additions & 6 deletions spectree/spec.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from collections import defaultdict
from copy import deepcopy
from functools import wraps
from typing import Any, Callable, Dict, Mapping, Optional, Sequence, Type
from typing import (
Any,
Callable,
Dict,
Mapping,
Optional,
Sequence,
Type,
get_type_hints,
)

from ._types import FunctionDecorator, ModelType
from .config import Configuration, ModeEnum
Expand Down Expand Up @@ -193,11 +202,12 @@ async def async_validate(*args: Any, **kwargs: Any):

if self.config.annotations:
nonlocal query, json, form, headers, cookies
query = func.__annotations__.get("query", query)
json = func.__annotations__.get("json", json)
form = func.__annotations__.get("form", form)
headers = func.__annotations__.get("headers", headers)
cookies = func.__annotations__.get("cookies", cookies)
annotations = get_type_hints(func)
query = annotations.get("query", query)
json = annotations.get("json", json)
form = annotations.get("form", form)
headers = annotations.get("headers", headers)
cookies = annotations.get("cookies", cookies)

# register
for name, model in zip(
Expand Down

0 comments on commit 25a12f9

Please sign in to comment.