@@ -160,7 +160,7 @@ Appendix
160160========
161161
162162Below is an abridged version of a
163- `research report<https://github.com/iritkatriel/finally/commits/main/README.md> `__
163+ `research report <https://github.com/iritkatriel/finally/commits/main/README.md >`__
164164by Irit Katriel, which was posted on 9 Nov 2024.
165165
166166``return `` in ``finally `` considered harmful
@@ -177,13 +177,13 @@ Method
177177The analysis is based on the 8,000 most popular PyPI packages, in terms of number
178178of downloads in the last 30 days. They were downloaded on the 17th-18th of
179179October, using
180- `a script<https://github.com/faster-cpython/tools/blob/main/scripts/download_packages.py> `__
180+ `a script <https://github.com/faster-cpython/tools/blob/main/scripts/download_packages.py >`__
181181written by Guido van Rossum, which in turn relies on Hugo van Kemenade's
182- `tool<https://hugovk.github.io/top-pypi-packages/> `__ that creates a list of the
182+ `tool <https://hugovk.github.io/top-pypi-packages/ >`__ that creates a list of the
183183most popular packages.
184184
185185Once downloaded, a
186- `second script<https://github.com/iritkatriel/finally/blob/main/scripts/ast_analysis.py> `__
186+ `second script <https://github.com/iritkatriel/finally/blob/main/scripts/ast_analysis.py >`__
187187was used to construct an AST for each file, and traverse it to identify ``break ``,
188188``continue `` and ``return `` statements which are directly inside a ``finally `` block.
189189
@@ -261,7 +261,7 @@ raised, but the ``return`` in the ``finally`` block would override the one in th
261261
262262> [!NOTE]
263263> Following the
264- > `discussion<https://discuss.python.org/t/an-analysis-of-return-in-finally-in-the-wild/70633/15> `__,
264+ > `discussion <https://discuss.python.org/t/an-analysis-of-return-in-finally-in-the-wild/70633/15 >`__,
265265> I repeated the analysis on a random selection of PyPI packages (to
266266> analyze code written by *average * programmers). The sample contained
267267> in total 77,398,892 lines of code with 316 instances of ``return ``/``break ``/``continue ``
@@ -296,7 +296,7 @@ the feature, because this is where working code will need to change. I did not
296296contact the authors in these cases, so we will need to assess the difficulty of
297297making these changes ourselves.
298298
299- - In `mosaicml<https://github.com/mosaicml/composer/blob/694e72159cf026b838ba00333ddf413185b4fb4f/composer/cli/launcher.py#L590> `__
299+ - In `mosaicml <https://github.com/mosaicml/composer/blob/694e72159cf026b838ba00333ddf413185b4fb4f/composer/cli/launcher.py#L590 >`__
300300 there is a return in a finally at the end of the ``main `` function, after an ``except: ``
301301 clause which swallows all exceptions. The return in the finally would swallow
302302 an exception raised from within the ``except: `` clause, but this seems to be the
@@ -305,7 +305,7 @@ making these changes ourselves.
305305 body of the ``except: `` clause by another ``try ``-``except `` to swallow any
306306 exceptions from within it.
307307
308- - In `webtest<https://github.com/Pylons/webtest/blob/617a2b823c60e8d7c5f6e12a220affbc72e09d7d/webtest/http.py#L131> `__
308+ - In `webtest <https://github.com/Pylons/webtest/blob/617a2b823c60e8d7c5f6e12a220affbc72e09d7d/webtest/http.py#L131 >`__
309309 there is a ``finally `` block that contains only ``return False ``. It could be replaced
310310 by
311311
@@ -315,27 +315,27 @@ making these changes ourselves.
315315 pass
316316 return False
317317
318- - In `kivy<https://github.com/kivy/kivy/blob/3b744c7ed274c1a99bd013fc55a5191ebd8a6a40/kivy/uix/codeinput.py#L204> `__
318+ - In `kivy <https://github.com/kivy/kivy/blob/3b744c7ed274c1a99bd013fc55a5191ebd8a6a40/kivy/uix/codeinput.py#L204 >`__
319319 there is a ``finally `` that contains only a ``return `` statement. Since there is also a
320320 bare ``except `` just before it, in this case the fix will be to just remove the ``finally: ``
321321 block and dedent the ``return `` statement.
322322
323- - In `logilab-common<https://forge.extranet.logilab.fr/open-source/logilab-common/-/blob/branch/default/test/data/module.py?ref_type=heads#L60> `__
323+ - In `logilab-common <https://forge.extranet.logilab.fr/open-source/logilab-common/-/blob/branch/default/test/data/module.py?ref_type=heads#L60 >`__
324324 there is, once again, a ``finally `` clause that can be replace by an ``except BaseException ``
325325 with the ``return `` dedented one level.
326326
327- - In `pluggy<https://github.com/pytest-dev/pluggy/blob/c760a77e17d512c3572d54d368fe6c6f9a7ac810/src/pluggy/_callers.py#L141> `__
327+ - In `pluggy <https://github.com/pytest-dev/pluggy/blob/c760a77e17d512c3572d54d368fe6c6f9a7ac810/src/pluggy/_callers.py#L141 >`__
328328 there is a lengthy ``finally `` with two ``return `` statements (the second on line 182). Here the
329329 return value can be assigned to a variable, and the ``return `` itself can appear after we've
330330 exited the ``finally `` clause.
331331
332- - In `aws-sam-cli<https://github.com/aws/aws-sam-cli/blob/97e63dcc2738529eded8eecfef4b875abc3a476f/samcli/local/apigw/local_apigw_service.py#L721> `__
332+ - In `aws-sam-cli <https://github.com/aws/aws-sam-cli/blob/97e63dcc2738529eded8eecfef4b875abc3a476f/samcli/local/apigw/local_apigw_service.py#L721 >`__
333333 there is a conditional return at the end of the block.
334334 From reading the code, it seems that the condition only holds when the exception has
335335 been handled. The conditional block can just move outside of the ``finally `` block and
336336 achieve the same effect.
337337
338- - In `scrappy<https://github.com/scrapy/scrapy/blob/52c072640aa61884de05214cb1bdda07c2a87bef/scrapy/utils/gz.py#L27> `__
338+ - In `scrappy <https://github.com/scrapy/scrapy/blob/52c072640aa61884de05214cb1bdda07c2a87bef/scrapy/utils/gz.py#L27 >`__
339339 there is a ``finally `` that contains only a ``break `` instruction. Assuming that it was the intention
340340 to swallow all exceptions, it can be replaced by
341341
@@ -377,10 +377,10 @@ The results indicate that ``return``, ``break`` and ``continue`` in a finally bl
377377I thank:
378378
379379- Alyssa Coghlan for
380- `bringing this issue my attention<https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/97> `__.
380+ `bringing this issue my attention <https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/97 >`__.
381381
382382- Guido van Rossum and Hugo van Kemenade for the
383- `script<https://github.com/faster-cpython/tools/blob/main/scripts/download_packages.py> `__
383+ `script <https://github.com/faster-cpython/tools/blob/main/scripts/download_packages.py >`__
384384 that downloads the most popular PyPI packages.
385385
386386- The many code authors I contacted for their responsiveness and grace.
0 commit comments