PY3.W0124

Confusing with statement

Emitted when a with statement component returns multiple values and uses name binding with as only for a part of those values, as in with ctx() as a, b. This can be misleading, since it's not clear if the context manager returns a tuple or if the node without a name binding is another context manager.

Noncompliant Code:

Copy
with open('file.txt', 'w') as fh1, fh2:
    pass

Compliant Code:

Copy
from contextlib import ExitStack
with ExitStack() as stack:
    fh1 = stack.enter_context(open('file1.txt', 'w'))
    fh2 = stack.enter_context(open('file2.txt', 'w'))

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.