1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/tests/lib/compat.py
Jon Dufresne d9b5525193 Prefer stdlib contextlib over contextlib2 when available
Removes the vendored contextlib2.

ExitStack and suppress are available from stdlib contextlib on all
supported Python versions.

The nullcontext context manager which isn't available in Python 3.6, but
the function is simple to implement. Once Python 3.6 support is dropped,
so too can the compat shim.
2021-02-19 04:13:33 -08:00

22 lines
586 B
Python

import contextlib
from typing import Iterator
@contextlib.contextmanager
def nullcontext():
# type: () -> Iterator[None]
"""
Context manager that does no additional processing.
Used as a stand-in for a normal context manager, when a particular block of
code is only sometimes used with a normal context manager:
cm = optional_cm if condition else nullcontext()
with cm:
# Perform operation, using optional_cm if condition is True
TODO: Replace with contextlib.nullcontext after dropping Python 3.6
support.
"""
yield