This rule raises an issue when a with statement is nested inside another with and both can be written as a single
with with multiple context managers.
A single with statement can open several context managers at once:
with A() as a, B() as b:
use(a, b)
Nesting a second with only to acquire another context manager adds an indentation level without changing semantics. The combined form
is the idiomatic style: it keeps related resource setup on one line and makes the shared body easier to read.
with statements obscure the intended resource lifetime.Nested with statements are acceptable when the inner with is not the only statement in the outer body.
Rewrite the nested with statements as one with that lists every context manager, separated by commas.
with open("a.txt") as a:
with open("b.txt") as b: # Noncompliant
process(a, b)
with open("a.txt") as a, open("b.txt") as b:
process(a, b)
with
statement