Sometimes I need a dummy context manager that does nothing. It can then be used as a stand-in for a more useful, but optional, context manager. For example:
ctx_
A simple solution for Python 3.6 and below, including 2.7:
from contextlib import contextmanager @contextmanager def nullcontext(enter_result=None): yield enter_result
Since Python 3.7 you should use the provided contextlib.nullcontext instead.