Skip to content

bpo-42669: Document that except rejects nested tuples #23822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 20, 2020

Conversation

cjwatson
Copy link
Contributor

@cjwatson cjwatson commented Dec 17, 2020

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:

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. However, the reference documentation was never updated to match this new restriction. Make it clear that the definition is no longer recursive.

https://bugs.python.org/issue42669

Automerge-Triggered-By: GH:ericvsmith

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:

    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.
However, the reference documentation was never updated to match this new
restriction.  Make it clear that the definition is no longer recursive.
@@ -254,7 +254,8 @@ present, must be last; it matches any exception. 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.
object, or a tuple containing an item that is the class or a base class of
the exception object. (Nested tuples are not allowed.)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this reworded sentence, I don't think the parenthesized statement is needed.

@@ -0,0 +1 @@
Explain that :keyword:`except` no longer accepts nested tuples. (This was changed in Python 3.0, but not previously documented.)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the blurb for this change. It's not substantive enough to be in the News file.

Eric V. Smith thinks this isn't a substantive enough change for that, so
I'll defer.
@cjwatson
Copy link
Contributor Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

: please review the changes made to this pull request.

Copy link
Member

@ericvsmith ericvsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks!

@miss-islington
Copy link
Contributor

@cjwatson: Status check is done, and it's a success ✅ .

@miss-islington miss-islington merged commit c95f8bc into python:master Dec 20, 2020
@miss-islington
Copy link
Contributor

Thanks @cjwatson for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 20, 2020
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:

    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.  However, the reference documentation was never updated to match this new restriction.  Make it clear that the definition is no longer recursive.

Automerge-Triggered-By: GH:ericvsmith
(cherry picked from commit c95f8bc)

Co-authored-by: Colin Watson <[email protected]>
@bedevere-bot
Copy link

GH-23870 is a backport of this pull request to the 3.9 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Dec 20, 2020
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 20, 2020
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:

    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.  However, the reference documentation was never updated to match this new restriction.  Make it clear that the definition is no longer recursive.

Automerge-Triggered-By: GH:ericvsmith
(cherry picked from commit c95f8bc)

Co-authored-by: Colin Watson <[email protected]>
@bedevere-bot
Copy link

GH-23871 is a backport of this pull request to the 3.8 branch.

ericvsmith pushed a commit that referenced this pull request Dec 20, 2020
…H-23870)

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:

    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.  However, the reference documentation was never updated to match this new restriction.  Make it clear that the definition is no longer recursive.

Automerge-Triggered-By: GH:ericvsmith
(cherry picked from commit c95f8bc)

Co-authored-by: Colin Watson <[email protected]>

Co-authored-by: Colin Watson <[email protected]>
ericvsmith pushed a commit that referenced this pull request Dec 20, 2020
…H-23871)

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:

    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.  However, the reference documentation was never updated to match this new restriction.  Make it clear that the definition is no longer recursive.

Automerge-Triggered-By: GH:ericvsmith
(cherry picked from commit c95f8bc)

Co-authored-by: Colin Watson <[email protected]>

Co-authored-by: Colin Watson <[email protected]>
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
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:

    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.  However, the reference documentation was never updated to match this new restriction.  Make it clear that the definition is no longer recursive.

Automerge-Triggered-By: GH:ericvsmith
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants