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 摘要。