1- import importlib .metadata
2-
31import marshmallow as ma
42from flask import Flask , Response , request
53from flask import jsonify as J
1311 use_kwargs ,
1412)
1513
16- FLASK_MAJOR_VERSION = int (importlib .metadata .version ("flask" ).split ("." )[0 ])
17- FLASK_SUPPORTS_ASYNC = FLASK_MAJOR_VERSION >= 2
18-
1914
2015class TestAppConfig :
2116 TESTING = True
@@ -138,12 +133,10 @@ def echo_headers_raising(args):
138133 return J (args )
139134
140135
141- if FLASK_SUPPORTS_ASYNC :
142-
143- @app .route ("/echo_headers_raising_async" )
144- @use_args (HelloSchema (), location = "headers" , unknown = None )
145- async def echo_headers_raising_async (args ):
146- return J (args )
136+ @app .route ("/echo_headers_raising_async" )
137+ @use_args (HelloSchema (), location = "headers" , unknown = None )
138+ async def echo_headers_raising_async (args ):
139+ return J (args )
147140
148141
149142@app .route ("/echo_cookie" )
@@ -165,14 +158,12 @@ def echo_view_arg(view_arg):
165158 return J (parser .parse ({"view_arg" : fields .Int ()}, location = "view_args" ))
166159
167160
168- if FLASK_SUPPORTS_ASYNC :
169-
170- @app .route ("/echo_view_arg_async/<view_arg>" )
171- async def echo_view_arg_async (view_arg ):
172- parsed_view_arg = await parser .async_parse (
173- {"view_arg" : fields .Int ()}, location = "view_args"
174- )
175- return J (parsed_view_arg )
161+ @app .route ("/echo_view_arg_async/<view_arg>" )
162+ async def echo_view_arg_async (view_arg ):
163+ parsed_view_arg = await parser .async_parse (
164+ {"view_arg" : fields .Int ()}, location = "view_args"
165+ )
166+ return J (parsed_view_arg )
176167
177168
178169@app .route ("/echo_view_arg_use_args/<view_arg>" )
@@ -181,12 +172,10 @@ def echo_view_arg_with_use_args(args, **kwargs):
181172 return J (args )
182173
183174
184- if FLASK_SUPPORTS_ASYNC :
185-
186- @app .route ("/echo_view_arg_use_args_async/<view_arg>" )
187- @use_args ({"view_arg" : fields .Int ()}, location = "view_args" )
188- async def echo_view_arg_with_use_args_async (args , ** kwargs ):
189- return J (args )
175+ @app .route ("/echo_view_arg_use_args_async/<view_arg>" )
176+ @use_args ({"view_arg" : fields .Int ()}, location = "view_args" )
177+ async def echo_view_arg_with_use_args_async (args , ** kwargs ):
178+ return J (args )
190179
191180
192181@app .route ("/echo_nested" , methods = ["POST" ])
@@ -211,16 +200,12 @@ def echo_nested_many_with_data_key():
211200 return J (parser .parse (args ))
212201
213202
214- if FLASK_SUPPORTS_ASYNC :
215-
216- @app .route ("/echo_nested_many_data_key_async" , methods = ["POST" ])
217- async def echo_nested_many_with_data_key_async ():
218- args = {
219- "x_field" : fields .Nested (
220- {"id" : fields .Int ()}, many = True , data_key = "X-Field"
221- )
222- }
223- return J (await parser .async_parse (args ))
203+ @app .route ("/echo_nested_many_data_key_async" , methods = ["POST" ])
204+ async def echo_nested_many_with_data_key_async ():
205+ args = {
206+ "x_field" : fields .Nested ({"id" : fields .Int ()}, many = True , data_key = "X-Field" )
207+ }
208+ return J (await parser .async_parse (args ))
224209
225210
226211class EchoMethodViewUseArgs (MethodView ):
@@ -235,17 +220,16 @@ def post(self, args):
235220)
236221
237222
238- if FLASK_SUPPORTS_ASYNC :
223+ class EchoMethodViewUseArgsAsync (MethodView ):
224+ @use_args ({"val" : fields .Int ()})
225+ async def post (self , args ):
226+ return J (args )
239227
240- class EchoMethodViewUseArgsAsync (MethodView ):
241- @use_args ({"val" : fields .Int ()})
242- async def post (self , args ):
243- return J (args )
244228
245- app .add_url_rule (
246- "/echo_method_view_use_args_async" ,
247- view_func = EchoMethodViewUseArgsAsync .as_view ("echo_method_view_use_args_async" ),
248- )
229+ app .add_url_rule (
230+ "/echo_method_view_use_args_async" ,
231+ view_func = EchoMethodViewUseArgsAsync .as_view ("echo_method_view_use_args_async" ),
232+ )
249233
250234
251235class EchoMethodViewUseKwargs (MethodView ):
@@ -259,19 +243,17 @@ def post(self, val):
259243 view_func = EchoMethodViewUseKwargs .as_view ("echo_method_view_use_kwargs" ),
260244)
261245
262- if FLASK_SUPPORTS_ASYNC :
263246
264- class EchoMethodViewUseKwargsAsync (MethodView ):
265- @use_kwargs ({"val" : fields .Int ()})
266- async def post (self , val ):
267- return J ({"val" : val })
247+ class EchoMethodViewUseKwargsAsync (MethodView ):
248+ @use_kwargs ({"val" : fields .Int ()})
249+ async def post (self , val ):
250+ return J ({"val" : val })
268251
269- app .add_url_rule (
270- "/echo_method_view_use_kwargs_async" ,
271- view_func = EchoMethodViewUseKwargsAsync .as_view (
272- "echo_method_view_use_kwargs_async"
273- ),
274- )
252+
253+ app .add_url_rule (
254+ "/echo_method_view_use_kwargs_async" ,
255+ view_func = EchoMethodViewUseKwargsAsync .as_view ("echo_method_view_use_kwargs_async" ),
256+ )
275257
276258
277259@app .route ("/echo_use_kwargs_missing" , methods = ["post" ])
@@ -281,13 +263,11 @@ def echo_use_kwargs_missing(username, **kwargs):
281263 return J ({"username" : username })
282264
283265
284- if FLASK_SUPPORTS_ASYNC :
285-
286- @app .route ("/echo_use_kwargs_missing_async" , methods = ["post" ])
287- @use_kwargs ({"username" : fields .Str (required = True ), "password" : fields .Str ()})
288- async def echo_use_kwargs_missing_async (username , ** kwargs ):
289- assert "password" not in kwargs
290- return J ({"username" : username })
266+ @app .route ("/echo_use_kwargs_missing_async" , methods = ["post" ])
267+ @use_kwargs ({"username" : fields .Str (required = True ), "password" : fields .Str ()})
268+ async def echo_use_kwargs_missing_async (username , ** kwargs ):
269+ assert "password" not in kwargs
270+ return J ({"username" : username })
291271
292272
293273# Return validation errors as JSON
0 commit comments