[New-bugs-announce] [issue42669] "except" documentation still suggests nested tuples are allowed

Colin Watson report at bugs.python.org
Thu Dec 17 08:27:57 EST 2020


New submission from Colin Watson <cjwatson at users.sourceforge.net>:

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.

----------
assignee: docs at python
components: Documentation
messages: 383243
nosy: cjwatson, docs at python
priority: normal
severity: normal
status: open
title: "except" documentation still suggests nested tuples are allowed
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42669>
_______________________________________


More information about the New-bugs-announce mailing list