summaryrefslogtreecommitdiff
path: root/coverage/misc.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-05-17 18:36:30 -0400
committerNed Batchelder <ned@nedbatchelder.com>2015-05-17 18:36:30 -0400
commit9520d008fb6a006aa70ae341906b0be18f40cd41 (patch)
tree7e07d938149129b71b334e76aa572af9cd27c6ab /coverage/misc.py
parent79a287fae7392ffda5d42593189243844027ffe9 (diff)
downloadpython-coveragepy-9520d008fb6a006aa70ae341906b0be18f40cd41.tar.gz
Use PyContracts so we can declare/enforce parameter and return types.
This commit doesn't add any uses of PyContracts, but gets the machinery in place.
Diffstat (limited to 'coverage/misc.py')
-rw-r--r--coverage/misc.py27
1 files changed, 26 insertions, 1 deletions
diff --git a/coverage/misc.py b/coverage/misc.py
index d5197ea..6411135 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -6,7 +6,32 @@ import inspect
import os
from coverage import env
-from coverage.backward import string_class, to_bytes
+from coverage.backward import string_class, to_bytes, unicode_class
+
+
+# Use PyContracts for assertion testing on parameters and returns, but only if
+# we are running our own test suite.
+contract = None
+
+if env.TESTING:
+ try:
+ from contracts import contract
+ except ImportError:
+ pass
+ else:
+ from contracts import new_contract
+
+ # Define contract words that PyContract doesn't have.
+ new_contract('bytes', lambda v: isinstance(v, bytes))
+ if env.PY3:
+ new_contract('unicode', lambda v: isinstance(v, unicode_class))
+
+if not contract:
+ # We aren't using real PyContracts, so just define a no-op decorator as a
+ # stunt double.
+ def contract(**unused): # pylint: disable=function-redefined
+ """Dummy no-op implementation of `contract`."""
+ return lambda func: func
def nice_pair(pair):