summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-11-04 07:20:50 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-11-04 07:20:50 -0400
commitc7c32846ea2af9863fa921c5f5735ff3958b775f (patch)
treed455d8e4da0e4101bfffff1d1d6f62f2a61cad85
parent8aa27024bdeb8950d61455b37a18bd508bf37904 (diff)
downloadpython-coveragepy-git-c7c32846ea2af9863fa921c5f5735ff3958b775f.tar.gz
refactor(test): don't even import contracts if not using PyContracts
On CPython 3.11, PyContracts fails on import, so we need to shut it off more completely. PyContracts is unmaintained these days, so we'll probably get rid of it completely soon.
-rw-r--r--coverage/env.py5
-rw-r--r--coverage/inorout.py9
-rw-r--r--coverage/misc.py7
-rw-r--r--tests/test_misc.py4
4 files changed, 13 insertions, 12 deletions
diff --git a/coverage/env.py b/coverage/env.py
index 68da83dd..a01bb816 100644
--- a/coverage/env.py
+++ b/coverage/env.py
@@ -124,3 +124,8 @@ METACOV = os.getenv('COVERAGE_COVERAGE', '') != ''
# Even when running tests, you can use COVERAGE_TESTING=0 to disable the
# test-specific behavior like contracts.
TESTING = os.getenv('COVERAGE_TESTING', '') == 'True'
+
+# Environment COVERAGE_NO_CONTRACTS=1 can turn off contracts while debugging
+# tests to remove noise from stack traces.
+# $set_env.py: COVERAGE_NO_CONTRACTS - Disable PyContracts to simplify stack traces.
+USE_CONTRACTS = TESTING and not bool(int(os.environ.get("COVERAGE_NO_CONTRACTS", 0)))
diff --git a/coverage/inorout.py b/coverage/inorout.py
index 8badf4f7..174a19dc 100644
--- a/coverage/inorout.py
+++ b/coverage/inorout.py
@@ -180,10 +180,11 @@ def add_coverage_paths(paths):
# When testing, we use PyContracts, which should be considered
# part of coverage.py, and it uses six. Exclude those directories
# just as we exclude ourselves.
- import contracts
- import six
- for mod in [contracts, six]:
- paths.add(canonical_path(mod))
+ if env.USE_CONTRACTS:
+ import contracts
+ import six
+ for mod in [contracts, six]:
+ paths.add(canonical_path(mod))
class InOrOut:
diff --git a/coverage/misc.py b/coverage/misc.py
index 40f00930..584efeaa 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -100,14 +100,9 @@ def dummy_decorator_with_args(*args_unused, **kwargs_unused):
return _decorator
-# Environment COVERAGE_NO_CONTRACTS=1 can turn off contracts while debugging
-# tests to remove noise from stack traces.
-# $set_env.py: COVERAGE_NO_CONTRACTS - Disable PyContracts to simplify stack traces.
-USE_CONTRACTS = env.TESTING and not bool(int(os.environ.get("COVERAGE_NO_CONTRACTS", 0)))
-
# Use PyContracts for assertion testing on parameters and returns, but only if
# we are running our own test suite.
-if USE_CONTRACTS:
+if env.USE_CONTRACTS:
from contracts import contract # pylint: disable=unused-import
from contracts import new_contract as raw_new_contract
diff --git a/tests/test_misc.py b/tests/test_misc.py
index 58dba6a8..bdee2b50 100644
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -7,11 +7,11 @@ import sys
import pytest
+from coverage import env
from coverage.exceptions import CoverageException
from coverage.misc import contract, dummy_decorator_with_args, file_be_gone
from coverage.misc import Hasher, one_of, substitute_variables, import_third_party
from coverage.misc import human_sorted, human_sorted_items
-from coverage.misc import USE_CONTRACTS
from tests.coveragetest import CoverageTest
@@ -80,7 +80,7 @@ class RemoveFileTest(CoverageTest):
file_be_gone(".")
-@pytest.mark.skipif(not USE_CONTRACTS, reason="Contracts are disabled, can't test them")
+@pytest.mark.skipif(not env.USE_CONTRACTS, reason="Contracts are disabled, can't test them")
class ContractTest(CoverageTest):
"""Tests of our contract decorators."""