Skip to content

Commit 054bbc4

Browse files
committed
refactor: simplify async decorator by removing dead code branch
1 parent f216979 commit 054bbc4

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/functions_framework/execution_id.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,37 +178,34 @@ def set_execution_context_async(enable_id_logging=False):
178178
stdout_redirect = contextlib.nullcontext()
179179
stderr_redirect = contextlib.nullcontext()
180180

181-
def decorator(view_function):
182-
@functools.wraps(view_function)
181+
def decorator(func):
182+
@functools.wraps(func)
183183
async def async_wrapper(request, *args, **kwargs):
184184
context = _extract_context_from_headers(request.headers)
185185
token = execution_context_var.set(context)
186186

187187
with stderr_redirect, stdout_redirect:
188-
if inspect.iscoroutinefunction(view_function):
189-
result = await view_function(request, *args, **kwargs)
190-
else:
191-
result = view_function(request, *args, **kwargs) # pragma: no cover
188+
result = await func(request, *args, **kwargs)
192189

193190
# Only reset context on successful completion
194191
# On exception, leave context available for exception handlers
195192
execution_context_var.reset(token)
196193
return result
197194

198-
@functools.wraps(view_function)
195+
@functools.wraps(func)
199196
def sync_wrapper(request, *args, **kwargs):
200197
context = _extract_context_from_headers(request.headers)
201198
token = execution_context_var.set(context)
202199

203200
with stderr_redirect, stdout_redirect:
204-
result = view_function(request, *args, **kwargs)
201+
result = func(request, *args, **kwargs)
205202

206203
# Only reset context on successful completion
207204
# On exception, leave context available for exception handlers
208205
execution_context_var.reset(token)
209206
return result
210207

211-
if inspect.iscoroutinefunction(view_function):
208+
if inspect.iscoroutinefunction(func):
212209
return async_wrapper
213210
else:
214211
return sync_wrapper

0 commit comments

Comments
 (0)