SyntaxWarning: 'return' in a 'finally' block (Python 3.14)

未关闭 适合新手
#758 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
70/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
aws, python
领域
backend, cloud

调研方向

从 datadog_lambda/wrapper.py 中的 _LambdaDecorator.call 开始,检查 try/except/finally 控制流,尤其是第 203 行报告的 return。验证 issue 中描述的四条执行路径,并添加或更新测试,以确保在 Python 3.14 中导入时不会发出 SyntaxWarning,同时阻塞响应和正常响应仍然正确。

由索引模型根据 Issue 内容生成。

描述

Expected Behavior

No warnings emitted during Lambda cold start when importing datadog_lambda.

Actual Behavior

A SyntaxWarning is emitted at module import time (cold start) on Python 3.14:

/var/task/datadog_lambda/wrapper.py:203: SyntaxWarning: 'return' in a 'finally' block
  return self.blocking_response

This warning fires every cold start and pollutes CloudWatch logs. Python 3.14 introduced SyntaxWarning for return/break/continue inside finally blocks because such a return silently suppresses any exception that was actively propagating through the try/except.

Steps to Reproduce the Problem

  1. Deploy a Lambda function using datadog-lambda on the Python 3.14 runtime
  2. Trigger a cold start (first invocation or new sandbox)
  3. Observe the SyntaxWarning in CloudWatch logs during INIT_START

Specifications

  • Datadog Lambda Library version: 8.123.0
  • Python version: 3.14

Root Cause

In _LambdaDecorator.__call__, the finally block contains a return:

finally:
    self._after(event, context)
    if self.blocking_response:
        return self.blocking_response  # ← triggers SyntaxWarning in Python 3.14

Proposed Fix

Remove return self.response from the try block and return self.blocking_response from the finally block, deferring both to after the entire try/except/finally:

    try:
        if self.blocking_response:
            return self.blocking_response
        self.response = self.func(event, context, **kwargs)
    except BlockingException:
        self.blocking_response = get_asm_blocked_response(self.event_source)
    except Exception:
        ...
        raise
    finally:
        self._after(event, context)

    if self.blocking_response:
        return self.blocking_response
    return self.response

All four execution paths (block before, block during, block after, normal) remain correct. Happy to open a PR with this fix and updated tests.

Stacktrace

/var/task/datadog_lambda/wrapper.py:203: SyntaxWarning: 'return' in a 'finally' block
return self.blocking_response
主要语言
Python
星标
102
派生
52
平均合并
5 天 19 小时
30 天内合并 PR
2

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

DataDog/datadog-lambda-python 的其他 Issue

查看 DataDog/datadog-lambda-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。