Using return, break, or continue statements that exit a finally block can lead to confusing
control flow and unexpected behavior. When these statements exit a finally block, they can mask exceptions that were raised in the
corresponding try or except blocks, making debugging difficult.
Starting with Python 3.14, this pattern emits a SyntaxWarning as specified in PEP 765, indicating that this usage is discouraged and
may become a syntax error in future Python versions. The finally block is intended for cleanup code that should always execute,
regardless of how the try block exits. When control flow statements exit the finally block, they can interfere with this
guarantee.
Additionally, this pattern can make code harder to understand and maintain, as the actual exit point becomes unclear when reading the code.
What is the potential impact?
Using control flow statements that exit finally blocks can lead to:
- Debugging difficulties: The actual cause of issues may be hidden when exceptions are suppressed unexpectedly
- Future compatibility issues: Code may break in future Python versions when this becomes a syntax error
- Reduced code maintainability: Control flow becomes harder to follow and understand
How to fix?
Move the return statement outside the finally block and use a variable to store the return value if needed.
Non-compliant code example
def risky_function():
try:
do_something()
finally:
cleanup()
return "done" # Noncompliant
Compliant code example
def safe_function():
result = None
try:
do_something()
result = "done"
finally:
cleanup()
return result
Documentation