consider-using-with / R1732#
Consider using 'with' for resource-allocating operations
Emitted if a resource-allocating assignment or call may be replaced by a 'with' block. By using 'with' the release of the allocated resources is ensured even in the case of an exception.
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]
with open("apple.txt", "r", encoding="utf8") as file: contents = file.read() with open("banana.txt", "r", encoding="utf8") as f: best = f.read()
write() without using the
with keyword or calling
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
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
Created by the refactoring checker.