bpo-34067: Include a more easily understood example for nullcontext#8158
Merged
ncoghlan merged 2 commits intopython:masterpython/cpython:masterfrom Jul 9, 2018
Merged
bpo-34067: Include a more easily understood example for nullcontext#8158ncoghlan merged 2 commits intopython:masterpython/cpython:masterfrom
ncoghlan merged 2 commits intopython:masterpython/cpython:masterfrom
Conversation
Contributor
Author
|
I figured this issue was simple enough to warrant not opening a bug or issue, let me know if I was mistaken. |
ncoghlan
reviewed
Jul 9, 2018
Contributor
ncoghlan
left a comment
There was a problem hiding this comment.
I like this addition, with one small tweak to the exception type being suppressed (which I'll edit in directly).
I've also associated it with an issue we already have open, pointing out some other problems with the current nullcontext example.
Doc/library/contextlib.rst
Outdated
| def myfunction(arg, ignore_exceptions=False): | ||
| if ignore_exceptions: | ||
| # Use suppress to ignore all exceptions. | ||
| cm = contextlib.suppress(BaseException) |
Contributor
There was a problem hiding this comment.
Suppressing KeyboardInterrupt and SystemExit is almost never the right thing to do, so the example exception here should only be Exception (not BaseException)
Contributor
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Jul 9, 2018
…ythonGH-8158) Include a more easily understood example for nullcontext (cherry picked from commit c287545) Co-authored-by: Daniel Porteous <danielporteous1@gmail.com>
|
GH-8199 is a backport of this pull request to the 3.7 branch. |
Contributor
Author
|
Awesome, super happy to help. Thanks! |
miss-islington
added a commit
that referenced
this pull request
Jul 9, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When trying to let a few friends know about
contextlib.nullcontext, they "didn't get it" based on the example in the docs. I feel that including examples that do and don't useenter_resultcan help new users understand the intention ofnullcontextas a no-op context manager.I also noticed that normally italics are used for an argument, so I fixed that up on line 155.
https://bugs.python.org/issue34067