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

Ouverte Adaptée aux débutants
#758 0 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
70/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Calme
Stack technique
aws, python
Domaine
backend, cloud

Piste de recherche

Commencez dans datadog_lambda/wrapper.py, au niveau de _LambdaDecorator.call, et examinez le flux de contrôle try/except/finally, en particulier le return signalé à la ligne 203. Vérifiez les quatre chemins d’exécution décrits dans l’issue et ajoutez ou mettez à jour les tests afin que l’importation avec Python 3.14 n’émette aucun SyntaxWarning, tout en garantissant que les réponses bloquantes et normales restent correctes.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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
Langage dominant
Python
Étoiles
102
Forks
52
Merge moyen
5 j 19 h
PR mergées (30 j)
2

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de DataDog/datadog-lambda-python

Toutes les issues de DataDog/datadog-lambda-python

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.