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

Aberta Para iniciantes
#758 0 comentários 1 reação 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
70/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Pouca atividade
Stack de tecnologia
aws, python
Domínio
backend, cloud

Direção de pesquisa

Comece em datadog_lambda/wrapper.py, em _LambdaDecorator.call, e inspecione o fluxo de controle try/except/finally, especialmente o return relatado na linha 203. Verifique os quatro caminhos de execução descritos na issue e adicione ou atualize os testes para que a importação no Python 3.14 não emita nenhum SyntaxWarning, enquanto as respostas de bloqueio e normais continuem corretas.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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
Linguagem predominante
Python
Estrelas
102
Forks
52
Merge médio
5d 19h
PRs com merge (30d)
2

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de DataDog/datadog-lambda-python

Todas as issues de DataDog/datadog-lambda-python

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.