-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from VyperCore/example-context
Create a context manager for contextual coverage data
- Loading branch information
Showing
4 changed files
with
47 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from copy import copy | ||
from types import SimpleNamespace | ||
|
||
class ContextBase: | ||
_stack: list["SimpleNamespace"] = [SimpleNamespace()] | ||
|
||
def __init__(self, replace=False, overlay=True, **kwargs): | ||
if overlay: | ||
self.data = copy(self._stack[-1]) | ||
for k, v in kwargs.items(): | ||
if not replace and hasattr(self.data, k): | ||
raise KeyError(f"Context already contains `{k}` and `replace` is False") | ||
setattr(self.data, k, v) | ||
else: | ||
self.data = SimpleNamespace(**kwargs) | ||
|
||
def __enter__(self): | ||
self._stack.append(self.data) | ||
|
||
def __exit__(self, _extype, _exval, _extb): | ||
self._stack.pop() | ||
|
||
@classmethod | ||
def get(cls): | ||
if len(cls._stack): | ||
return cls._stack[-1] | ||
return SimpleNamespace() | ||
|
||
class CoverageContext(ContextBase): ... |
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