classification
Title: "except" documentation still suggests nested tuples are allowed
Type: Stage: resolved
Components: Documentation Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: cjwatson, docs@python, eric.smith, miss-islington
Priority: normal Keywords: patch

Created on 2020-12-17 13:27 by cjwatson, last changed 2020-12-20 21:20 by eric.smith. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23822 merged cjwatson, 2020-12-17 13:30
PR 23870 merged miss-islington, 2020-12-20 18:24
PR 23871 merged miss-islington, 2020-12-20 18:24
Messages (4)
msg383243 - (view) Author: Colin Watson (cjwatson) * Date: 2020-12-17 13:27
In Python 2, it was possible to use `except` with a nested tuple, and occasionally natural.  For example, `zope.formlib.interfaces.InputErrors` is a tuple of several exception classes, and one might reasonably think to do something like this (this is real code used in several places in https://git.launchpad.net/launchpad):

    try:
        self.getInputValue()
        return True
    except (InputErrors, SomethingElse):
        return False

As of Python 3.0, this raises "TypeError: catching classes that do not inherit from BaseException is not allowed" instead: one must instead either break it up into multiple "except" clauses or flatten the tuple.  The change was mentioned in https://bugs.python.org/issue2380 and seems to have been intentional: I'm not requesting that the previous behaviour be restored, since it's a fairly rare porting issue and by now well-established in Python 3.

However, the relevant sentences of documentation in https://docs.python.org/2/reference/compound_stmts.html#try and https://docs.python.org/3/reference/compound_stmts.html#the-try-statement are identical aside from punctuation, and they both read:

     For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception.  An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception.

I think this admits a recursive reading: I certainly read it that way.  It should make it clear that nested tuples are not allowed.
msg383435 - (view) Author: miss-islington (miss-islington) Date: 2020-12-20 18:24
New changeset c95f8bc2700b42f4568886505a819816c9b0ba28 by Colin Watson in branch 'master':
bpo-42669: Document that `except` rejects nested tuples (GH-23822)
https://github.com/python/cpython/commit/c95f8bc2700b42f4568886505a819816c9b0ba28
msg383456 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-12-20 21:18
New changeset 409ce4a09e4f96ca9b251c19f5819205aae9ae34 by Miss Islington (bot) in branch '3.9':
bpo-42669: Document that `except` rejects nested tuples (GH-23822) (GH-23870)
https://github.com/python/cpython/commit/409ce4a09e4f96ca9b251c19f5819205aae9ae34
msg383457 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-12-20 21:18
New changeset 81f706d2db0f57c4fdd747df6e0a4cffcbc54704 by Miss Islington (bot) in branch '3.8':
bpo-42669: Document that `except` rejects nested tuples (GH-23822) (GH-23871)
https://github.com/python/cpython/commit/81f706d2db0f57c4fdd747df6e0a4cffcbc54704
History
Date User Action Args
2020-12-20 21:20:03eric.smithsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-12-20 21:18:45eric.smithsetmessages: + msg383457
2020-12-20 21:18:24eric.smithsetnosy: + eric.smith
messages: + msg383456
2020-12-20 18:24:33miss-islingtonsetpull_requests: + pull_request22733
2020-12-20 18:24:24miss-islingtonsetpull_requests: + pull_request22732
2020-12-20 18:24:17miss-islingtonsetnosy: + miss-islington
messages: + msg383435
2020-12-17 13:30:44cjwatsonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request22682
2020-12-17 13:27:57cjwatsoncreate