Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 2 commits into from
Jul 9, 2018

Conversation

banool
Copy link
Contributor

@banool banool commented Jul 7, 2018

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 use enter_result can help new users understand the intention of nullcontext 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

@banool
Copy link
Contributor Author

banool commented Jul 7, 2018

I figured this issue was simple enough to warrant not opening a bug or issue, let me know if I was mistaken.

@serhiy-storchaka serhiy-storchaka requested a review from ncoghlan July 9, 2018 11:58
@ncoghlan ncoghlan changed the title Docs: Include a more easily understood example for nullcontext bpo-34067: Include a more easily understood example for nullcontext Jul 9, 2018
Copy link
Contributor

@ncoghlan ncoghlan left a 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.

def myfunction(arg, ignore_exceptions=False):
if ignore_exceptions:
# Use suppress to ignore all exceptions.
cm = contextlib.suppress(BaseException)
Copy link
Contributor

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)

@ncoghlan ncoghlan merged commit c287545 into python:master Jul 9, 2018
@miss-islington
Copy link
Contributor

Thanks @banool for the PR, and @ncoghlan for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

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 <[email protected]>
@bedevere-bot
Copy link

GH-8199 is a backport of this pull request to the 3.7 branch.

@banool
Copy link
Contributor Author

banool commented Jul 9, 2018

Awesome, super happy to help. Thanks!

miss-islington added a commit that referenced this pull request Jul 9, 2018
…H-8158)

Include a more easily understood example for nullcontext
(cherry picked from commit c287545)

Co-authored-by: Daniel Porteous <[email protected]>
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