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

PEP 760: No more bare excepts #4015

Merged
merged 7 commits into from
Oct 9, 2024
Merged

PEP 760: No more bare excepts #4015

merged 7 commits into from
Oct 9, 2024

Conversation

pablogsal
Copy link
Member

@pablogsal pablogsal commented Oct 2, 2024

Basic requirements (all PEP Types)

  • Read and followed PEP 1 & PEP 12
  • File created from the latest PEP template
  • PEP has next available number, & set in filename (pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) and PEP header
  • Title clearly, accurately and concisely describes the content in 79 characters or less
  • Core dev/PEP editor listed as Author or Sponsor, and formally confirmed their approval
  • Author, Status (Draft), Type and Created headers filled out correctly
  • PEP-Delegate, Topic, Requires and Replaces headers completed if appropriate
  • Required sections included
    • Abstract (first section)
    • Copyright (last section; exact wording from template required)
  • Code is well-formatted (PEP 7/PEP 8) and is in code blocks, with the right lexer names if non-Python
  • PEP builds with no warnings, pre-commit checks pass and content displays as intended in the rendered HTML
  • Authors/sponsor added to .github/CODEOWNERS for the PEP

Standards Track requirements

  • PEP topic discussed in a suitable venue with general agreement that a PEP is appropriate
  • Suggested sections included (unless not applicable)
    • Motivation
    • Rationale
    • Specification
    • Backwards Compatibility
    • Security Implications
    • How to Teach This
    • Reference Implementation
    • Rejected Ideas
    • Open Issues
  • Python-Version set to valid (pre-beta) future Python version, if relevant
  • Any project stated in the PEP as supporting/endorsing/benefiting from the PEP formally confirmed such
  • Right before or after initial merging, PEP discussion thread created and linked to in Discussions-To and Post-History

📚 Documentation preview 📚: https://pep-previews--4015.org.readthedocs.build/

@pablogsal pablogsal requested a review from a team as a code owner October 2, 2024 22:16
@pablogsal pablogsal force-pushed the pep-760 branch 2 times, most recently from 944fe81 to f05871c Compare October 2, 2024 22:19
Signed-off-by: Pablo Galindo <[email protected]>
peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
.github/CODEOWNERS Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Outdated Show resolved Hide resolved
@warsaw
Copy link
Member

warsaw commented Oct 3, 2024

There's one pretty common use case that this will break: transaction handling in the face of exceptions. E.g. from Mailman:

@public
@contextmanager
def transaction():
    """Context manager for ensuring the transaction is complete."""
    try:
        yield
    except:                                              # noqa: E722
        config.db.abort()
        raise
    else:
        config.db.commit()

This guarantees that no matter what exception occurs, any open transaction will be aborted, while in the successful condition, the transaction will be committed.

This is a general case of "when anything goes wrong, do this thing and reraise the original exception".

If you want to argue that this should use except BareException:, that's fine but please do at least describe this common-ish pattern. While I don't love except BareException: a possible mitigation would be to create an alias to BareException perhaps called exceptions.AnyException so that the code would be more readable:

@public
@contextmanager
def transaction():
    """Context manager for ensuring the transaction is complete."""
    try:
        yield
    except AnyException:
        config.db.abort()
        raise
    else:
        config.db.commit()

Another thought, though probably harder to support, would be to allow bare except only in cases where there is a bare raise in the body of the exception handler.

peps/pep-0760.rst Outdated Show resolved Hide resolved
peps/pep-0760.rst Show resolved Hide resolved
brettcannon and others added 4 commits October 7, 2024 15:57
Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
Co-authored-by: Alex Waygood <[email protected]>
peps/pep-0760.rst Outdated Show resolved Hide resolved
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Let's get this merged, especially since people already started discussing the idea on Discourse.

@pablogsal pablogsal merged commit cb7ef4f into python:main Oct 9, 2024
6 checks passed
@pablogsal
Copy link
Member Author

Thanks everyone for you suggestions, comments and fixes. You all rock 🤘

@pablogsal pablogsal deleted the pep-760 branch October 9, 2024 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants