Skip to content

Commit

Permalink
Add documentation and code examples for consider-using-with (#6009)
Browse files Browse the repository at this point in the history
  • Loading branch information
DudeNr33 authored Mar 28, 2022
1 parent 1b85ca5 commit de1cca2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
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

0 comments on commit de1cca2

Please sign in to comment.