Skip to content

'return' in a 'finally' block is a SyntaxWarning in Python 3.14 #573

@Avasam

Description

@Avasam

See What’s new in Python 3.14 - PEP 765: Disallow return/break/continue that exit a finally block

When warnings are turned to errors, this is causing code like

finally:
# Fast path - only new-style wrappers, no Result.
if only_new_style_wrappers:
if firstresult: # first result hooks return a single value
result = results[0] if results else None
else:
result = results
# run all wrapper post-yield blocks
for teardown in reversed(teardowns):
try:
if exception is not None:
teardown.throw(exception) # type: ignore[union-attr]
else:
teardown.send(result) # type: ignore[union-attr]
# Following is unreachable for a well behaved hook wrapper.
# Try to force finalizers otherwise postponed till GC action.
# Note: close() may raise if generator handles GeneratorExit.
teardown.close() # type: ignore[union-attr]
except StopIteration as si:
result = si.value
exception = None
continue
except BaseException as e:
exception = e
continue
_raise_wrapfail(teardown, "has second yield") # type: ignore[arg-type]
if exception is not None:
raise exception.with_traceback(exception.__traceback__)
else:
return result

to produce errors like:

=================================== FAILURES ===================================
_________________________________ test session _________________________________
[gw0] linux -- Python 3.14.0 /home/runner/work/setuptools/setuptools/.tox/py/bin/python
mypy exited with status 2.
===================================== mypy =====================================
.tox/py/lib/python3.14/site-packages/pluggy/_callers.py:141: error: 'return' in a 'finally' block  [syntax]
Found 1 error in 1 file (errors prevented further checking)

(example log: https://github.com/pypa/setuptools/actions/runs/14441580075/job/40492746262?pr=4785#step:11:75)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions