Skip to content

Commit

Permalink
Revert removed piptools._compat.contextlib (#1338)
Browse files Browse the repository at this point in the history
This reverts commit 0036708.
  • Loading branch information
atugushev authored Mar 7, 2021
1 parent 9aeeed1 commit 4f89da8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
31 changes: 31 additions & 0 deletions piptools/_compat/contextlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Ported from python 3.7 contextlib.py
from types import TracebackType
from typing import Optional, Type, TypeVar

_T = TypeVar("_T")


class nullcontext:
"""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 Python 3.6 being dropped
"""

def __init__(self, enter_result: Optional[_T] = None) -> None:
self.enter_result = enter_result

def __enter__(self) -> Optional[_T]:
return self.enter_result

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> Optional[bool]:
pass
5 changes: 2 additions & 3 deletions piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
from pip._internal.utils.misc import normalize_path
from pip._internal.utils.temp_dir import TempDirectory, global_tempdir_manager
from pip._internal.utils.urls import path_to_url, url_to_path
from pip._vendor import contextlib2
from pip._vendor.requests import RequestException

from .._compat import PIP_VERSION
from .._compat import PIP_VERSION, contextlib
from ..exceptions import NoCandidateFound
from ..logging import log
from ..utils import (
Expand Down Expand Up @@ -407,7 +406,7 @@ def _get_file_hash(self, link):
width=32,
)
else:
context_manager = contextlib2.nullcontext(chunks)
context_manager = contextlib.nullcontext(chunks)

# Iterate over the chosen context manager
with context_manager as bar:
Expand Down

0 comments on commit 4f89da8

Please sign in to comment.