Skip to content

Commit 7ec364a

Browse files
iritkatrielhugovk
andauthored
Apply suggestions from code review
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 48b787b commit 7ec364a

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

peps/pep-0765.rst

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ Status: Draft
66
Type: Standards Track
77
Created: 15-Nov-2024
88
Python-Version: 3.14
9-
Post-History: 09-Nov-2024
10-
11-
.. highlight:: rst
9+
Post-History: `09-Nov-2024 <https://discuss.python.org/t/an-analysis-of-return-in-finally-in-the-wild/70633>`__,
1210

1311
Abstract
1412
========
@@ -26,14 +24,15 @@ Motivation
2624

2725
The semantics of ``return``, ``break`` and ``continue`` in a
2826
finally block are surprising for many developers.
29-
The documentation [1]_ mentions that
27+
The :ref:`documentation <python:tut-cleanup>` mentions that:
28+
29+
- If the ``finally`` clause executes a ``break``, ``continue``
30+
or ``return`` statement, exceptions are not re-raised.
3031

31-
1. If the ``finally`` clause executes a ``break``, ``continue``
32-
or ``return`` statement, exceptions are not re-raised.
33-
2. If a ``finally`` clause includes a ``return`` statement, the
34-
returned value will be the one from the ``finally`` clause’s
35-
``return`` statement, not the value from the ``try`` clause’s
36-
``return`` statement.
32+
- If a ``finally`` clause includes a ``return`` statement, the
33+
returned value will be the one from the ``finally`` clause’s
34+
``return`` statement, not the value from the ``try`` clause’s
35+
``return`` statement.
3736

3837
Both of these behaviours cause confusion, but the first is
3938
particularly dangerous because a swallowed exception is more
@@ -44,15 +43,19 @@ In 2019, :pep:`601` proposed to change Python to emit a
4443
``SyntaxError``. It was rejected in favour of viewing this
4544
as a programming style issue, to be handled by linters and :pep:`8`.
4645
Indeed, :pep:`8` now recommends not to use control flow statements
47-
in a ``finally`` block, and linters such as pylint [2]_, ruff [3]_
48-
and flake8-bugbear [4]_ flag them as a problem.
46+
in a ``finally`` block, and linters such as
47+
`Pylint <https://pylint.readthedocs.io/en/stable/>`__,
48+
`Ruff <https://docs.astral.sh/ruff/>`__ and
49+
`flake8-bugbear <https://github.com/PyCQA/flake8-bugbear>`__
50+
flag them as a problem.
4951

5052
Rationale
5153
=========
5254

53-
A recent analysis of real world code [5]_ shows that:
55+
A recent `analysis of real world code
56+
<https://github.com/iritkatriel/finally/blob/main/README.md>`_ shows that:
5457

55-
- These features are rare (2 per million LOC in the top 8000 PyPI
58+
- These features are rare (2 per million LOC in the top 8,000 PyPI
5659
packages, 4 per million LOC in a random selection of packages).
5760
This could be thanks to the linters that flag this pattern.
5861
- Most of the usages are incorrect, and introduce unintended
@@ -63,7 +66,8 @@ A recent analysis of real world code [5]_ shows that:
6366
This new data indicates that it would benefit Python's users if
6467
Python itself moved them away from this harmful feature.
6568

66-
One of the arguments brought up [6]_ in the discussion about :pep:`601`
69+
`One of the arguments brought up
70+
<https://discuss.python.org/t/pep-601-forbid-return-break-continue-breaking-out-of-finally/2239/24>`__
6771
was that language features should be orthogonal, and combine without
6872
context-based restrictions. However, in the meantime :pep:`654` has
6973
been implemented, and it forbids ``return``, ``break`` and ``continue``
@@ -86,6 +90,7 @@ of it.
8690
This includes the following examples:
8791

8892
.. code-block::
93+
:class: bad
8994
9095
def f():
9196
try:
@@ -97,11 +102,12 @@ This includes the following examples:
97102
try:
98103
...
99104
finally:
100-
break # (or continue)
105+
break # (or continue)
101106
102107
But excludes these:
103108

104109
.. code-block::
110+
:class: good
105111
106112
try:
107113
...
@@ -113,7 +119,7 @@ But excludes these:
113119
...
114120
finally:
115121
for x in o:
116-
break # (or continue)
122+
break # (or continue)
117123
118124
119125
CPython will emit a ``SyntaxWarning`` in version 3.14, and we leave
@@ -142,26 +148,11 @@ How to Teach This
142148

143149
The change will be documented in the language spec and in the
144150
What's New documentation. The ``SyntaxWarning`` will alert users
145-
that their code needs to change. The empirical evidence [5]_
151+
that their code needs to change. The `empirical evidence
152+
<https://github.com/iritkatriel/finally/blob/main/README.md>`__
146153
shows that the changes necessary are typically quite
147154
straightforward.
148155

149-
References
150-
==========
151-
152-
.. [1] https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions
153-
154-
.. [2] https://pylint.readthedocs.io/en/stable/
155-
156-
.. [3] https://docs.astral.sh/ruff/
157-
158-
.. [4] https://github.com/PyCQA/flake8-bugbear
159-
160-
.. [5] https://github.com/iritkatriel/finally/blob/main/README.md
161-
162-
.. [6] https://discuss.python.org/t/pep-601-forbid-return-break-continue-breaking-out-of-finally/2239/24
163-
164-
165156
Copyright
166157
=========
167158

0 commit comments

Comments
 (0)