-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(when): add ContextManager mocking support (#93)
- Loading branch information
Showing
19 changed files
with
727 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Wrappers around contextlib types and fallbacks.""" | ||
import contextlib | ||
from typing import Any, AsyncContextManager, ContextManager, Generic, TypeVar | ||
|
||
GeneratorContextManager = contextlib._GeneratorContextManager | ||
|
||
_EnterT = TypeVar("_EnterT") | ||
|
||
|
||
class ContextWrapper( | ||
ContextManager[_EnterT], | ||
AsyncContextManager[_EnterT], | ||
Generic[_EnterT], | ||
): | ||
"""A simple, do-nothing ContextManager that wraps a given value. | ||
Adapted from `contextlib.nullcontext` to ensure support across | ||
all Python versions. | ||
""" | ||
|
||
def __init__(self, enter_result: _EnterT) -> None: | ||
self._enter_result = enter_result | ||
|
||
def __enter__(self) -> _EnterT: | ||
"""Return the wrapped value.""" | ||
return self._enter_result | ||
|
||
def __exit__(self, *args: Any, **kwargs: Any) -> Any: | ||
"""No-op on exit.""" | ||
pass | ||
|
||
async def __aenter__(self) -> _EnterT: | ||
"""Return the wrapped value.""" | ||
return self._enter_result | ||
|
||
async def __aexit__(self, *args: Any, **kwargs: Any) -> Any: | ||
"""No-op on exit.""" | ||
pass | ||
|
||
|
||
__all__ = [ | ||
"AsyncContextManager", | ||
"GeneratorContextManager", | ||
"ContextManager", | ||
"ContextWrapper", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.