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
commitbdab4fa31dd367105f746ec972cd95de8c99eaef (patch)
tree368cf2181128338df46cd6dfe616bcf2269580c9 /coverage/misc.py
parent697202cca950b04fd27cdb06b67b0b7c627e3d69 (diff)
downloadpython-coveragepy-bdab4fa31dd367105f746ec972cd95de8c99eaef.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 240a258..e78a153 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.