-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-34067: Include a more easily understood example for nullcontext #8158
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
Conversation
I figured this issue was simple enough to warrant not opening a bug or issue, let me know if I was mistaken. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppressing KeyboardInterrupt
and SystemExit
is almost never the right thing to do, so the example exception here should only be Exception
(not BaseException
)
…ythonGH-8158) Include a more easily understood example for nullcontext (cherry picked from commit c287545) Co-authored-by: Daniel Porteous <[email protected]>
GH-8199 is a backport of this pull request to the 3.7 branch. |
Awesome, super happy to help. Thanks! |
…H-8158) Include a more easily understood example for nullcontext (cherry picked from commit c287545) Co-authored-by: Daniel Porteous <[email protected]>
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_result
can help new users understand the intention ofnullcontext
as 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