Skip to content

Commit

Permalink
Add type annotations for nullcontext()
Browse files Browse the repository at this point in the history
  • Loading branch information
atugushev committed Mar 6, 2021
1 parent 34a2214 commit f821c17
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions piptools/_compat/contextlib.py
Original file line number Diff line number Diff line change
@@ -1,18 +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=None):
def __init__(self, enter_result: Optional[_T] = None) -> None:
self.enter_result = enter_result

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

def __exit__(self, *excinfo):
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> Optional[bool]:
pass

0 comments on commit f821c17

Please sign in to comment.