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

Use typing.get_type_hints instead of .__annotations__ #251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -212,8 +212,9 @@ def validate(
try:
self.request_validation(_req, query, json, headers, cookies)
if self.config.annotations:
annotations = get_type_hints(func)
for name in ("query", "json", "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 @@ -293,8 +294,9 @@ async def validate(
try:
await self.request_validation(_req, query, json, headers, cookies)
if self.config.annotations:
annotations = get_type_hints(func)
for name in ("query", "json", "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, Optional
from typing import Any, Callable, Optional, get_type_hints

from pydantic import BaseModel, ValidationError

Expand Down Expand Up @@ -184,8 +184,9 @@ def validate(
try:
self.request_validation(request, query, json, headers, cookies)
if self.config.annotations:
annotations = get_type_hints(func)
for name in ("query", "json", "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 @@ -93,8 +93,9 @@ async def validate(
try:
await self.request_validation(request, query, json, headers, cookies)
if self.config.annotations:
annotations = get_type_hints(func)
for name in ("query", "json", "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
20 changes: 15 additions & 5 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 @@ -188,14 +197,15 @@ async def async_validate(*args: Any, **kwargs: Any):
)

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

# register
for name, model in zip(
Expand Down