MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
185 stars 19 forks source link

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

Open terrykong opened 3 years ago

terrykong commented 3 years ago

Desired change

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.