This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author cjwatson
Recipients cjwatson, docs@python
Date 2020-12-17.13:27:56
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1608211677.22.0.0507603390583.issue42669@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2020-12-17 13:27:57cjwatsonsetrecipients: + cjwatson, docs@python
2020-12-17 13:27:57cjwatsonsetmessageid: <1608211677.22.0.0507603390583.issue42669@roundup.psfhosted.org>
2020-12-17 13:27:57cjwatsonlinkissue42669 messages
2020-12-17 13:27:56cjwatsoncreate