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

The content on this page is adapted from the Pylint User Guide, Copyright ©2003-2022, Logilab, PyCQA and contributors. All rights reserved. https://pylint.pycqa.org/en/latest/index.html#, and is used under the Python Software Foundation License Version 2. Examples, recipes, and other code in the Pylint documentation are additionally licensed under the Zero Clause BSD License.