@@ -6,9 +6,7 @@ Status: Draft
66Type: Standards Track
77Created: 15-Nov-2024
88Python-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
1311Abstract
1412========
@@ -26,14 +24,15 @@ Motivation
2624
2725The semantics of ``return ``, ``break `` and ``continue `` in a
2826finally 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
3837Both of these behaviours cause confusion, but the first is
3938particularly 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
4544as a programming style issue, to be handled by linters and :pep: `8 `.
4645Indeed, :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
5052Rationale
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:
6366This new data indicates that it would benefit Python's users if
6467Python 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> `__
6771was that language features should be orthogonal, and combine without
6872context-based restrictions. However, in the meantime :pep: `654 ` has
6973been implemented, and it forbids ``return ``, ``break `` and ``continue ``
8690This 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
143149The change will be documented in the language spec and in the
144150What'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> `__
146153shows that the changes necessary are typically quite
147154straightforward.
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-
165156Copyright
166157=========
167158
0 commit comments