diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-07-31 19:05:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-31 19:05:43 +0200 |
commit | 4cf2d60dfdf09b5f4843bd07ae6f1b8c49d8df5f (patch) | |
tree | ccef3a53e6d96a55aec5910c1599a6a9231b0c6c | |
parent | 5d5f5f7f9637926dcfd10eaeff69f04bb16da26e (diff) | |
download | pylint-git-4cf2d60dfdf09b5f4843bd07ae6f1b8c49d8df5f.tar.gz |
[doc consider-using-with] Add the case where the file isn't even closed (#7248)
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
4 files changed, 13 insertions, 2 deletions
diff --git a/doc/data/messages/c/consider-using-with/bad.py b/doc/data/messages/c/consider-using-with/bad.py index bd657299b..f6ea102c1 100644 --- a/doc/data/messages/c/consider-using-with/bad.py +++ b/doc/data/messages/c/consider-using-with/bad.py @@ -1,3 +1,5 @@ -file = open("foo.txt", "r", encoding="utf8") # [consider-using-with] +file = open("apple.txt", "r", encoding="utf8") # [consider-using-with] contents = file.read() file.close() + +worst = open("banana.txt", "r", encoding="utf8").read() # [consider-using-with] diff --git a/doc/data/messages/c/consider-using-with/details.rst b/doc/data/messages/c/consider-using-with/details.rst index 1c990988e..58d763fa4 100644 --- a/doc/data/messages/c/consider-using-with/details.rst +++ b/doc/data/messages/c/consider-using-with/details.rst @@ -1,3 +1,7 @@ +Calling ``write()`` without using the ``with`` keyword or calling ``close()`` might +result in the arguments of ``write()`` not being completely written to the disk, +even if the program exits successfully. + This message applies to callables of Python's stdlib which can be replaced by a ``with`` statement. It is suppressed in the following cases: diff --git a/doc/data/messages/c/consider-using-with/good.py b/doc/data/messages/c/consider-using-with/good.py index 70e09b146..7f677ca7e 100644 --- a/doc/data/messages/c/consider-using-with/good.py +++ b/doc/data/messages/c/consider-using-with/good.py @@ -1,2 +1,5 @@ -with open("foo.txt", "r", encoding="utf8") as file: +with open("apple.txt", "r", encoding="utf8") as file: contents = file.read() + +with open("banana.txt", "r", encoding="utf8") as f: + best = f.read() diff --git a/doc/data/messages/c/consider-using-with/related.rst b/doc/data/messages/c/consider-using-with/related.rst index c4864e02e..a2a209ca0 100644 --- a/doc/data/messages/c/consider-using-with/related.rst +++ b/doc/data/messages/c/consider-using-with/related.rst @@ -1,2 +1,4 @@ +- `Python doc: Reading and writing files <https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files>`_ - `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 +- `Rationale <https://stackoverflow.com/a/73181877/2519059>`_ |