DeepSource has joined Harness. Learn more
Python logoPython/
PTC-W0062

`with` statements can be mergedPTC-W0062

Major severityMajor
Anti-pattern categoryAnti-pattern

Nested with statements can be merged into a single compound with statement.

This issue is raised only when there are no other statements between the nested with statements. Merging collapsible with statements can decrease cognitive complexity, improving code readability.

Bad practice

with open("file1", "w") as file1:
    with open("file2", "w") as file2:
        doSomething()

Preferred

with open("file1", "w") as file1, open("file2", "w") as file2:
    doSomething()