PY3.R1732
Consider using with
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.
Noncompliant Code:
Copy
file = open("foo.txt", "r", encoding="utf8")
contents = file.read()
file.close()
Compliant Code:
Copy
with open("foo.txt", "r", encoding="utf8") as file:
contents = file.read()
Additional details:
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