Skip to content
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

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

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

@ericvsmith ericvsmith Dec 20, 2020

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

@ericvsmith ericvsmith Dec 20, 2020

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.

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

cjwatson commented Dec 20, 2020

I have made the requested changes; please review again.

@bedevere-bot
Copy link

bedevere-bot commented Dec 20, 2020

Thanks for making the requested changes!

: please review the changes made to this pull request.

@ericvsmith ericvsmith added skip news 🤖 automerge PR will be merged once it's been approved and all CI passed labels Dec 20, 2020
Copy link
Member

@ericvsmith ericvsmith left a comment

Looks good. Thanks!

@miss-islington
Copy link
Contributor

miss-islington commented Dec 20, 2020

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

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

miss-islington commented Dec 20, 2020

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 <cjwatson@debian.org>
@bedevere-bot
Copy link

bedevere-bot commented Dec 20, 2020

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

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 <cjwatson@debian.org>
@bedevere-bot
Copy link

bedevere-bot commented Dec 20, 2020

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 <cjwatson@debian.org>

Co-authored-by: Colin Watson <cjwatson@debian.org>
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 <cjwatson@debian.org>

Co-authored-by: Colin Watson <cjwatson@debian.org>
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
Labels
🤖 automerge PR will be merged once it's been approved and all CI passed skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants