summaryrefslogtreecommitdiff
path: root/coverage/misc.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2017-01-16 07:58:53 -0500
committerNed Batchelder <ned@nedbatchelder.com>2017-01-16 07:58:53 -0500
commit5f5510fba829e12d57adb949667ce8e3c5ff09d9 (patch)
tree9bdf075c6e0b02abf2865f21a56b655a07bb4c52 /coverage/misc.py
parent8175a927c0189ff06e9f961bd780c9a8b7280abf (diff)
downloadpython-coveragepy-git-5f5510fba829e12d57adb949667ce8e3c5ff09d9.tar.gz
A one_of decorator for checking function arguments.
Diffstat (limited to 'coverage/misc.py')
-rw-r--r--coverage/misc.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/coverage/misc.py b/coverage/misc.py
index 240a2587..e78a1537 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -58,6 +58,17 @@ if env.TESTING:
new_contract('bytes', lambda v: isinstance(v, bytes))
if env.PY3:
new_contract('unicode', lambda v: isinstance(v, unicode_class))
+
+ def one_of(argnames):
+ """Ensure that only one of the argnames is non-None."""
+ def _decorator(func):
+ argnameset = set(name.strip() for name in argnames.split(","))
+ def _wrapped(*args, **kwargs):
+ vals = set(kwargs.get(name) for name in argnameset)
+ assert sum(val is not None for val in vals) == 1
+ return func(*args, **kwargs)
+ return _wrapped
+ return _decorator
else: # pragma: not covered
# We aren't using real PyContracts, so just define a no-op decorator as a
# stunt double.
@@ -69,6 +80,12 @@ else: # pragma: not covered
"""Dummy no-op implementation of `new_contract`."""
pass
+ def one_of(argnames_unused):
+ """Dummy no-op implementation of `one_of`."""
+ def _decorator(func):
+ return func
+ return _decorator
+
def nice_pair(pair):
"""Make a nice string representation of a pair of numbers.