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

contextlib.closing documentation should use a new example #66944

Open
mjpieters mannequin opened this issue Oct 28, 2014 · 4 comments
Open

contextlib.closing documentation should use a new example #66944

mjpieters mannequin opened this issue Oct 28, 2014 · 4 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir

Comments

@mjpieters
Copy link
Mannequin

mjpieters mannequin commented Oct 28, 2014

BPO 22755
Nosy @rhettinger, @mjpieters, @vadmium

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2014-10-28.22:40:52.490>
labels = ['3.11', '3.9', '3.10', 'docs']
title = 'contextlib.closing documentation should use a new example'
updated_at = <Date 2021-11-27.15:16:37.307>
user = 'https://github.com/mjpieters'

bugs.python.org fields:

activity = <Date 2021-11-27.15:16:37.307>
actor = 'iritkatriel'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2014-10-28.22:40:52.490>
creator = 'mjpieters'
dependencies = []
files = []
hgrepos = []
issue_num = 22755
keywords = []
message_count = 4.0
messages = ['230184', '230185', '230186', '230430']
nosy_count = 5.0
nosy_names = ['rhettinger', 'mjpieters', 'docs@python', 'martin.panter', 'abarnert']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue22755'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

Linked PRs

@mjpieters
Copy link
Mannequin Author

mjpieters mannequin commented Oct 28, 2014

urllib.request.urlopen() now always produces a context manager (either a HTTPResponse or addinfourl object). The example for contextlib.closing still uses urllib.request.urlopen as an example for the context manager wrapper, see

https://docs.python.org/3/library/contextlib.html#contextlib.closing

This is confusing users that now expect the object not to be a context manager, see:

http://stackoverflow.com/questions/26619404/with-and-closing-of-files-in-python

Can a different example be chosen?

@mjpieters mjpieters mannequin assigned docspython Oct 28, 2014
@mjpieters mjpieters mannequin added the docs Documentation in the Doc dir label Oct 28, 2014
@abarnert
Copy link
Mannequin

abarnert mannequin commented Oct 28, 2014

It may be pretty hard to come up with a good example for closing in the stdlib that shouldn't just be turned into a context manager…

There are some types where using it in a with statement is sensible, but unusual enough that you might want to make it explicit (maybe logging.Handler?), but does that make a good example?

@vadmium
Copy link
Member

vadmium commented Oct 28, 2014

One exception that comes to mind is generators. According to bpo-13814, it is deliberate that you have to call close(), so perhaps someone could come up with an example based on that.

@rhettinger
Copy link
Contributor

Can a different example be chosen?

The traditional examples (urllib, socket, StringIO) grow context manager support as soon as the need becomes apparent. So, that leaves only user-defined classes, third-party classes, generators, and cases where we already have a competing context manager that does something other than close.

A reasonable candidate is an sqlite connection. It is a legitimate use case for contextlib.closing() since connections need to be closed after use, and because they already are context managers that do something different than closing (the commit or rollback transactions).

A generator example would work, but closing generators is a rare and esoteric need (not many people even realize than you *can* close a generator), so this would make a bad example.

A user defined class can add its own __enter__ and __exit__ to close if needed, so it isn't common to use contextlib.closing() on your own classes. The primary use case is for third-party classes where you don't control the source code and a closing context manager isn't present; however, those make for bad examples because they aren't in the standard library (i.e. aren't familiar) and because the better maintained modules are also adding context manager support.

Out of all of these, only the sqlite connection example seems to be durable (won't go away) and familiar (in the standard library and not an exotic feature).

That said, I think it would be best to stick with the simplest and most familiar possible examples even if they are no longer needed. The purpose of the example is to show how closing() works. We can use the surrounding text to indicate that the example is no longer necessary and that the most common remain uses are to add closing() behavior to third-party code you don't control (if you do control the code, then adding CM support directly to the class would be preferable).

@iritkatriel iritkatriel added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Nov 27, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
scop added a commit to scop/cpython that referenced this issue Nov 17, 2023
scop added a commit to scop/cpython that referenced this issue Nov 17, 2023
AA-Turner added a commit that referenced this issue Jan 23, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 23, 2024
…ustrative purposes (pythonGH-112198)

(cherry picked from commit 9af9ac1)

Co-authored-by: Ville Skyttä <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 23, 2024
…ustrative purposes (pythonGH-112198)

(cherry picked from commit 9af9ac1)

Co-authored-by: Ville Skyttä <[email protected]>
Co-authored-by: Adam Turner <[email protected]>
AA-Turner pushed a commit that referenced this issue Jan 23, 2024
AA-Turner pushed a commit that referenced this issue Jan 23, 2024
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

3 participants