1
- from typing import Callable , Dict , Optional , Tuple
2
-
3
- from quart import ResponseReturnValue as QuartResponseReturnValue
1
+ from typing import Callable , Dict , Optional , Tuple , TypeVar
4
2
5
3
from .typing import Model
6
4
from .validation import (
11
9
QUART_SCHEMA_RESPONSE_ATTRIBUTE ,
12
10
)
13
11
12
+ T = TypeVar ("T" , bound = Callable )
13
+
14
14
15
- def document_querystring (model_class : Model ) -> Callable :
15
+ def document_querystring (model_class : Model ) -> Callable [[ T ], T ] :
16
16
"""Document the request querystring arguments.
17
17
18
18
Add the request querystring **model_class** to the openapi
@@ -25,15 +25,15 @@ def document_querystring(model_class: Model) -> Callable:
25
25
26
26
"""
27
27
28
- def decorator (func : Callable ) -> Callable :
28
+ def decorator (func : T ) -> T :
29
29
setattr (func , QUART_SCHEMA_QUERYSTRING_ATTRIBUTE , model_class )
30
30
31
31
return func
32
32
33
33
return decorator
34
34
35
35
36
- def document_headers (model_class : Model ) -> Callable :
36
+ def document_headers (model_class : Model ) -> Callable [[ T ], T ] :
37
37
"""Document the request headers.
38
38
39
39
Add the request **model_class** to the openapi generated
@@ -46,7 +46,7 @@ def document_headers(model_class: Model) -> Callable:
46
46
47
47
"""
48
48
49
- def decorator (func : Callable ) -> Callable :
49
+ def decorator (func : T ) -> T :
50
50
setattr (func , QUART_SCHEMA_HEADERS_ATTRIBUTE , model_class )
51
51
52
52
return func
@@ -58,7 +58,7 @@ def document_request(
58
58
model_class : Model ,
59
59
* ,
60
60
source : DataSource = DataSource .JSON ,
61
- ) -> Callable :
61
+ ) -> Callable [[ T ], T ] :
62
62
"""Document the request data.
63
63
64
64
Add the request **model_class** to the openapi generated
@@ -72,7 +72,7 @@ def document_request(
72
72
encoded).
73
73
"""
74
74
75
- def decorator (func : Callable ) -> Callable :
75
+ def decorator (func : T ) -> T :
76
76
setattr (func , QUART_SCHEMA_REQUEST_ATTRIBUTE , (model_class , source ))
77
77
78
78
return func
@@ -84,7 +84,7 @@ def document_response(
84
84
model_class : Model ,
85
85
status_code : int = 200 ,
86
86
headers_model_class : Optional [Model ] = None ,
87
- ) -> Callable :
87
+ ) -> Callable [[ T ], T ] :
88
88
"""Document the response data.
89
89
90
90
Add the response **model_class**, and its corresponding (optional)
@@ -103,9 +103,7 @@ def document_response(
103
103
104
104
"""
105
105
106
- def decorator (
107
- func : Callable [..., QuartResponseReturnValue ]
108
- ) -> Callable [..., QuartResponseReturnValue ]:
106
+ def decorator (func : T ) -> T :
109
107
schemas = getattr (func , QUART_SCHEMA_RESPONSE_ATTRIBUTE , {})
110
108
schemas [status_code ] = (model_class , headers_model_class )
111
109
setattr (func , QUART_SCHEMA_RESPONSE_ATTRIBUTE , schemas )
@@ -122,15 +120,15 @@ def document(
122
120
request_source : DataSource = DataSource .JSON ,
123
121
headers : Optional [Model ] = None ,
124
122
responses : Dict [int , Tuple [Model , Optional [Model ]]],
125
- ) -> Callable :
123
+ ) -> Callable [[ T ], T ] :
126
124
"""Document the route.
127
125
128
126
This is a shorthand combination of of the document_querystring,
129
127
document_request, document_headers, and document_response
130
128
decorators. Please see the docstrings for those decorators.
131
129
"""
132
130
133
- def decorator (func : Callable ) -> Callable :
131
+ def decorator (func : T ) -> T :
134
132
if querystring is not None :
135
133
func = document_querystring (querystring )(func )
136
134
if request is not None :
0 commit comments