flake8-simplify icon indicating copy to clipboard operation
flake8-simplify copied to clipboard

[Adjust Rule] Let SIM115 accept usage of contextlib.ExitStack to manage contextmanagers

Open terrykong opened this issue 2 years ago • 0 comments

Desired change

  • Rule(s): SIM115
  • Adjustment: Accept usage of contextlib.ExitStack

Explanation

In some applications, we may have a dynamic number of contextmanagers which we may choose to use contextlib.ExitStack to cleanup. This does not satisfy SIM115

Example

This is an example where the mentioned rule(s) would currently be suboptimal:

import contextlib

files_to_read = ['/tmp/foo', '/tmp/bar']

with contextlib.ExitStack() as stack:
    files = [stack.enter_context(open(path)) for path in files_to_read]
    # do stuff to files

The above example unfortunately doesn't satisfy SIM115, even though it takes care of exiting.

terrykong avatar Jul 23 '21 23:07 terrykong