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

Add documentation and code examples for consider-using-with #6009

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/data/messages/c/consider-using-with/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file = open("foo.txt", "r", encoding="utf8") # [consider-using-with]
contents = file.read()
file.close()
6 changes: 6 additions & 0 deletions doc/data/messages/c/consider-using-with/details.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This message applies to callables of Python's stdlib which can be replaced by a ``with`` statement.
It is suppressed in the following cases:

- the call is located inside a context manager
- the call result is returned from the enclosing function
- the call result is used in a ``with`` statement itself
2 changes: 2 additions & 0 deletions doc/data/messages/c/consider-using-with/good.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
with open("foo.txt", "r", encoding="utf8") as file:
contents = file.read()
2 changes: 2 additions & 0 deletions doc/data/messages/c/consider-using-with/related.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `PEP 343 <https://peps.python.org/pep-0343/>`_
- `Context managers in Python <https://johnlekberg.com/blog/2020-10-11-ctx-manage.html>`_ by John Lekberg