diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2017-01-16 08:09:46 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2017-01-16 08:09:46 -0500 |
commit | 9fe048aab56d8dd4b856127f2734f6f159e18f41 (patch) | |
tree | 2f83314e61c2fd7b2dd66dd8bb541e1d24e12b77 | |
parent | 5f5510fba829e12d57adb949667ce8e3c5ff09d9 (diff) | |
download | python-coveragepy-git-9fe048aab56d8dd4b856127f2734f6f159e18f41.tar.gz |
A better way to neuter decorators when not testing.
-rw-r--r-- | coverage/misc.py | 22 | ||||
-rw-r--r-- | tests/test_misc.py | 12 |
2 files changed, 22 insertions, 12 deletions
diff --git a/coverage/misc.py b/coverage/misc.py index e78a1537..797a16d5 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -39,6 +39,13 @@ def isolate_module(mod): os = isolate_module(os) +def dummy_decorator_with_args(*args_unused, **kwargs_unused): + """Dummy no-op implementation of a decorator with arguments.""" + def _decorator(func): + return func + return _decorator + + # Use PyContracts for assertion testing on parameters and returns, but only if # we are running our own test suite. if env.TESTING: @@ -70,22 +77,15 @@ if env.TESTING: 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. - def contract(**unused): - """Dummy no-op implementation of `contract`.""" - return lambda func: func + # We aren't using real PyContracts, so just define our decorators as + # stunt-double no-ops. + contract = dummy_decorator_with_args + one_of = dummy_decorator_with_args def new_contract(*args_unused, **kwargs_unused): """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. diff --git a/tests/test_misc.py b/tests/test_misc.py index 0b6c9144..fc95744a 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -9,7 +9,7 @@ import pytest import coverage from coverage.version import _make_url, _make_version -from coverage.misc import contract, file_be_gone, Hasher, one_of +from coverage.misc import contract, dummy_decorator_with_args, file_be_gone, Hasher, one_of from tests.coveragetest import CoverageTest @@ -109,6 +109,16 @@ class ContractTest(CoverageTest): with pytest.raises(AssertionError): give_me_one() + def test_dummy_decorator_with_args(self): + @dummy_decorator_with_args("anything", this=17, that="is fine") + def undecorated(a=None, b=None): # pylint: disable=missing-docstring + return (a, b) + + assert undecorated() == (None, None) + assert undecorated(17) == (17, None) + assert undecorated(b=23) == (None, 23) + assert undecorated(b=42, a=3) == (3, 42) + class VersionTest(CoverageTest): """Tests of version.py""" |