New submission from Colin Watson <cjwat...@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@python
components: Documentation
messages: 383243
nosy: cjwatson, docs@python
priority: normal
severity: normal
status: open
title: "except" documentation still suggests nested tuples are allowed
versions: Python 3.10

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42669>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to