Skip to content

Commit bc2af6c

Browse files
authored
Merge pull request #4832 from Zac-HD/claude/hypothesis-nocover-cleanup-1yu7ao
Remove nocover pragmas, add new tests, etc
2 parents 793e92d + cef8c21 commit bc2af6c

50 files changed

Lines changed: 1052 additions & 266 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/main.yml‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ jobs:
102102
- check-py313-cover
103103
- check-py313-nocover
104104
- check-py313-niche
105+
# niche tests also collect coverage on 3.14; see the run step below.
106+
- check-py314-niche
105107
- check-quality
106108
- check-pytest9
107109
- check-pytest84
@@ -135,6 +137,11 @@ jobs:
135137
- name: Run tests
136138
run: |
137139
export TASK=${{ matrix.task }}
140+
if [[ $TASK == check-py314-niche ]]; then
141+
# Collect (sys.monitoring-based) coverage from this job too.
142+
export COVERAGE_PROCESS_START=${{ github.workspace }}/hypothesis/pyproject.toml
143+
export COVERAGE_FILE=${{ github.workspace }}/hypothesis/.coverage
144+
fi
138145
if [[ $TASK == check-crosshair-custom-* ]]; then
139146
GROUP="${TASK#check-crosshair-custom-}"
140147
./build.sh check-crosshair-custom -- -n auto $(cd hypothesis && echo tests/$GROUP | xargs -n1 echo | grep -Ev "_py312|_py314" | xargs)
@@ -144,7 +151,9 @@ jobs:
144151
- name: Upload coverage data
145152
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
146153
# Invoke the magic `always` function to run on both success and failure.
147-
if: ${{ always() && endsWith(matrix.task, '-coverage') }}
154+
# check-django60 and check-py314-niche also collect coverage; see
155+
# [testenv:django] in tox.ini and the run step above.
156+
if: ${{ always() && (endsWith(matrix.task, '-coverage') || matrix.task == 'check-django60' || matrix.task == 'check-py314-niche') }}
148157
with:
149158
name: coverage-data-${{ matrix.task }}
150159
include-hidden-files: true
@@ -160,10 +169,6 @@ jobs:
160169
task:
161170
- check-py314-cover
162171
- check-py314-nocover
163-
# Blocked by a 3.14rc1 bug. see
164-
# https://github.com/HypothesisWorks/hypothesis/pull/4490#issuecomment-3144989862.
165-
# Can revisit in 3.14rc2.
166-
# - check-py314-niche
167172
- check-py314t-cover
168173
- check-py314t-nocover
169174
- check-py314t-niche

‎hypothesis/RELEASE.rst‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
RELEASE_TYPE: patch
2+
3+
Now that we combine coverage data across our CI jobs (:issue:`4261`), this patch
4+
removes around a hundred ``# pragma: no cover`` comments which are no longer
5+
needed, and expands our 100%-coverage requirement to several previously-omitted
6+
files.
7+
8+
The ``explain`` phase no longer reports lines inside the standard library,
9+
which were occasionally included in (and made for misleading) explanations.
10+
11+
The constants-collection feature introduced in :ref:`version 6.131.1 <v6.131.1>`
12+
now also collects the float components of complex literals like ``3.5j``.

‎hypothesis/pyproject.toml‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,29 @@ exclude = [
153153
[tool.coverage.run]
154154
parallel = true
155155
branch = true
156-
source = ["hypothesis"]
156+
# Some of our CI jobs don't import any modules measured under coverage.
157+
disable_warnings = ["module-not-imported"]
158+
# note that we use source_pkgs here to avoid the ambiguity between
159+
# the "hypothesis" package and the "hypothesis" directory during
160+
# resolution by coverage.py. source_pkgs forces package-only resolution.
161+
source_pkgs = [
162+
"hypothesis",
163+
"_hypothesis_ftz_detector",
164+
"_hypothesis_globals",
165+
"_hypothesis_pytestplugin",
166+
]
157167

158168
[tool.coverage.paths]
159169
# Tests run against an installed copy, so each job measures a different
160170
# site-packages directory; map them onto our source tree so we can combine them.
161171
#
162-
# The key of `source-paths` here is arbitrary and ignored. See
172+
# The keys of `source-paths` and `top-level-modules` here are arbitrary and
173+
# ignored. See
163174
# https://coverage.readthedocs.io/en/latest/config.html#paths.
175+
# (Note however that order matters).
164176
source-paths = ["src/hypothesis", "*/site-packages/hypothesis"]
177+
# e.g. our _hypothesis_pytestplugin, _hypothesis_globals, etc modules.
178+
top-level-modules = ["src", "*/site-packages"]
165179

166180
[tool.coverage.report]
167181
fail_under = 100
@@ -170,20 +184,8 @@ skip_covered = true
170184
# Omitted when reporting rather than when measuring, so that we can also report
171185
# on how close the combined data comes to covering these modules.
172186
omit = [
173-
# never measured anyway, being outside `source`
187+
# only reachable via an also-excluded branch in strategies/_internal/numbers.py
174188
"**/_hypothesis_ftz_detector.py",
175-
"**/_hypothesis_pytestplugin.py",
176-
"**/_hypothesis_globals.py",
177-
"**/extra/array_api.py",
178-
"**/extra/cli.py",
179-
"**/extra/django/*.py",
180-
"**/extra/ghostwriter.py",
181-
"**/extra/pytestplugin.py",
182-
"**/internal/scrutineer.py",
183-
"**/utils/terminal.py",
184-
"**/internal/conjecture/provider_conformance.py",
185-
# covered by our attrs tests in check-niche
186-
"**/strategies/_internal/attrs.py",
187189
]
188190
exclude_lines = [
189191
"pragma: no cover",

‎hypothesis/src/_hypothesis_pytestplugin.py‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def __init__(self, config):
6565
def __call__(self, msg):
6666
if self.config.getoption("capture", "fd") == "no":
6767
self.report(msg)
68-
if not isinstance(msg, str):
68+
if not isinstance(msg, str): # pragma: no cover
69+
# e.g. the dict payloads from our observability machinery
6970
msg = repr(msg)
7071
self.results.append(msg)
7172

@@ -88,7 +89,8 @@ def __call__(self, msg):
8889
# need balanced increment/decrement in configure/sessionstart to support nested
8990
# pytest (e.g. runpytest_inprocess), so this early increment in effect replaces
9091
# the first one in pytest_configure.
91-
if not os.environ.get("HYPOTHESIS_EXTEND_INITIALIZATION"):
92+
# (the else-branch can only run in a child process)
93+
if not os.environ.get("HYPOTHESIS_EXTEND_INITIALIZATION"): # pragma: no branch
9294
_hypothesis_globals.in_initialization += 1
9395
if "hypothesis" in sys.modules:
9496
# Some other plugin has imported hypothesis, so we'll check if there
@@ -169,7 +171,7 @@ def pytest_configure(config):
169171
# with the extra values given (in this case 'verbosity')
170172
settings.register_profile(name, verbosity=verbosity_value)
171173
settings.load_profile(name)
172-
if (
174+
if ( # pragma: no cover # the explain option is currently untested
173175
config.getoption(EXPLAIN_OPTION)
174176
and Phase.explain not in settings.default.phases
175177
):
@@ -326,10 +328,12 @@ def _stash_get(config, key, default):
326328
if hasattr(config, "stash"):
327329
# pytest 7
328330
return config.stash.get(key, default)
329-
elif hasattr(config, "_store"):
331+
# The old-pytest fallbacks below are covered by the check-pytest*
332+
# jobs, which don't measure coverage.
333+
elif hasattr(config, "_store"): # pragma: no cover
330334
# pytest 5.4
331335
return config._store.get(key, default)
332-
else:
336+
else: # pragma: no cover
333337
return getattr(config, key, default)
334338

335339
@pytest.hookimpl(hookwrapper=True)
@@ -358,7 +362,7 @@ def pytest_runtest_makereport(item, call):
358362
xml.add_global_property(name, stats_base64)
359363

360364
# If there's a terminal report, include our summary stats for each test
361-
if terminalreporter is not None:
365+
if terminalreporter is not None: # pragma: no branch
362366
report.__dict__[STATS_KEY] = stats
363367

364368
# If there's an HTML report, include our summary stats for each test
@@ -399,7 +403,9 @@ def pytest_terminal_summary(terminalreporter):
399403

400404
from hypothesis.internal.observability import _WROTE_TO
401405

402-
if _WROTE_TO:
406+
if _WROTE_TO: # pragma: no cover
407+
# Observability output is only produced by tests which run pytest
408+
# in a subprocess, where we don't measure coverage.
403409
terminalreporter.section("Hypothesis")
404410
for fname in sorted(_WROTE_TO):
405411
terminalreporter.write_line(f"observations written to {fname}")
@@ -412,10 +418,10 @@ def pytest_terminal_summary(terminalreporter):
412418
try:
413419
gc_patches()
414420
fname = save_patch(patch)
415-
except Exception:
421+
except Exception: # pragma: no cover
416422
# fail gracefully if we hit any filesystem or permissions problems
417423
return
418-
if not _WROTE_TO:
424+
if not _WROTE_TO: # pragma: no branch
419425
terminalreporter.section("Hypothesis")
420426
terminalreporter.write_line(
421427
f"`git apply {fname}` to add failing test cases to your code."

‎hypothesis/src/hypothesis/_settings.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ def local_settings(s: settings) -> Generator[settings, None, None]:
12341234
settings.register_profile("ci", CI)
12351235

12361236

1237-
if is_in_ci(): # pragma: no cover # covered in ci, but not locally
1237+
if is_in_ci():
12381238
settings.load_profile("ci")
12391239

12401240
assert settings.default is not None

‎hypothesis/src/hypothesis/core.py‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ def get_random_for_wrapped_test(test, wrapped_test):
722722
if global_force_seed is not None:
723723
return Random(global_force_seed)
724724

725-
if threadlocal._hypothesis_global_random is None: # pragma: no cover
725+
if threadlocal._hypothesis_global_random is None:
726726
threadlocal._hypothesis_global_random = Random()
727727
seed = threadlocal._hypothesis_global_random.getrandbits(128)
728728
wrapped_test._hypothesis_internal_use_generated_seed = seed
@@ -1221,11 +1221,7 @@ def _execute_once_for_engine(self, data: ConjectureData) -> None:
12211221
with Tracer(should_trace=self._should_trace()) as tracer:
12221222
try:
12231223
result = self.execute_once(data)
1224-
if (
1225-
data.status == Status.VALID and tracer.branches
1226-
): # pragma: no cover
1227-
# This is in fact covered by our *non-coverage* tests, but due
1228-
# to the settrace() contention *not* by our coverage tests.
1224+
if data.status == Status.VALID and tracer.branches:
12291225
self.explain_traces[None].add(tracer.branches)
12301226
finally:
12311227
trace = tracer.branches
@@ -1317,8 +1313,7 @@ def _execute_once_for_engine(self, data: ConjectureData) -> None:
13171313
self.failed_normally = True
13181314

13191315
interesting_origin = InterestingOrigin.from_exception(e)
1320-
if trace: # pragma: no cover
1321-
# Trace collection is explicitly disabled under coverage.
1316+
if trace:
13221317
self.explain_traces[interesting_origin].add(trace)
13231318
if interesting_origin.exc_type == DeadlineExceeded:
13241319
self.failed_due_to_deadline = True
@@ -1493,8 +1488,7 @@ def run_engine(self):
14931488
and not PYPY
14941489
and self._should_trace()
14951490
and not Tracer.can_trace()
1496-
): # pragma: no cover
1497-
# actually covered by our tests, but only on >= 3.12
1491+
):
14981492
warnings.warn(
14991493
"avoiding tracing test function because tool id "
15001494
f"{MONITORING_TOOL_ID} is already taken by tool "
@@ -1827,26 +1821,23 @@ def given(
18271821
_: EllipsisType, /
18281822
) -> Callable[
18291823
[Callable[..., Coroutine[Any, Any, None] | None]], Callable[[], None]
1830-
]: # pragma: no cover
1831-
...
1824+
]: ...
18321825

18331826

18341827
@overload
18351828
def given(
18361829
*_given_arguments: SearchStrategy[Any],
18371830
) -> Callable[
18381831
[Callable[..., Coroutine[Any, Any, None] | None]], Callable[..., None]
1839-
]: # pragma: no cover
1840-
...
1832+
]: ...
18411833

18421834

18431835
@overload
18441836
def given(
18451837
**_given_kwargs: SearchStrategy[Any] | EllipsisType,
18461838
) -> Callable[
18471839
[Callable[..., Coroutine[Any, Any, None] | None]], Callable[..., None]
1848-
]: # pragma: no cover
1849-
...
1840+
]: ...
18501841

18511842

18521843
def given(
@@ -1979,7 +1970,7 @@ def run_test_as_given(test):
19791970
and isinstance(
19801971
test, sys.modules["_pytest.fixtures"].FixtureFunctionDefinition
19811972
)
1982-
): # pragma: no cover # covered by pytest/test_fixtures, but not by cover/
1973+
):
19831974
raise InvalidArgument("@given cannot be applied to a pytest fixture")
19841975

19851976
given_arguments = tuple(_given_arguments)
@@ -2206,8 +2197,7 @@ def wrapped_test(*arguments, **kwargs):
22062197
# wrapped up in an exception group. Because we break out of the loop
22072198
# immediately on finding a skip, if present it's always the last error.
22082199
if isinstance(errors[-1].exception, skip_exceptions_to_reraise()):
2209-
# Covered by `test_issue_3453_regression`, just in a subprocess.
2210-
del errors[:-1] # pragma: no cover
2200+
del errors[:-1]
22112201

22122202
if state.settings.verbosity < Verbosity.verbose:
22132203
# keep only one error per interesting origin, unless

‎hypothesis/src/hypothesis/extra/_array_helpers.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def do_draw(self, data):
691691
while j < len(result) and result[j] == slice(None):
692692
j += 1
693693
result[i:j] = [Ellipsis]
694-
elif self.allow_fewer_indices_than_dims: # pragma: no cover
694+
elif self.allow_fewer_indices_than_dims:
695695
while result[-1:] == [slice(None, None)] and data.draw(st.integers(0, 7)):
696696
result.pop()
697697
if len(result) == 1 and data.draw(st.booleans()):

‎hypothesis/src/hypothesis/extra/_patching.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def make_patch(
332332
return "".join(diffs)
333333

334334

335-
def save_patch(patch: str, *, slug: str = "") -> Path: # pragma: no cover
335+
def save_patch(patch: str, *, slug: str = "") -> Path:
336336
assert re.fullmatch(r"|[a-z]+-", slug), f"malformed {slug=}"
337337
now = date.today().isoformat()
338338
cleaned = re.sub(r"^Date: .+?$", "", patch, count=1, flags=re.MULTILINE)

‎hypothesis/src/hypothesis/extra/array_api.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ def complex_dtypes(
10771077
np = Mock()
10781078
else:
10791079
np = None # type: ignore[assignment]
1080-
if np is not None:
1080+
if np is not None: # pragma: no branch # numpy is a hard requirement of our test suite
10811081

10821082
class FloatInfo(NamedTuple):
10831083
bits: int

‎hypothesis/src/hypothesis/extra/django/_fields.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ def timezones():
6464
if django.VERSION < (5, 0, 0) and getattr(
6565
django.conf.settings, "USE_DEPRECATED_PYTZ", True
6666
):
67-
from hypothesis.extra.pytz import timezones
67+
# Our coverage-measured test suite only runs against Django >= 5.0,
68+
# so this branch is unreachable there; it remains covered by our
69+
# older, non-coverage-measured Django test jobs.
70+
from hypothesis.extra.pytz import timezones # pragma: no cover
6871
else:
6972
from hypothesis.strategies import timezones
7073

@@ -139,7 +142,8 @@ def using_sqlite():
139142
.get("ENGINE", "")
140143
.endswith(".sqlite3")
141144
)
142-
except django.core.exceptions.ImproperlyConfigured:
145+
except django.core.exceptions.ImproperlyConfigured: # pragma: no cover
146+
# Only reachable with unconfigured settings, which our runner sets up.
143147
return None
144148

145149

@@ -268,7 +272,10 @@ def _for_text(field):
268272
return strategy
269273

270274

271-
if "django.contrib.auth" in settings.INSTALLED_APPS:
275+
if "django.contrib.auth" in settings.INSTALLED_APPS: # pragma: no branch
276+
# Our coverage-measured test settings always install contrib.auth; the
277+
# "without contrib.auth" branch is instead covered by the separate
278+
# django-nocontrib test job, which does not measure coverage.
272279
from django.contrib.auth.forms import UsernameField
273280

274281
register_for(UsernameField)(_for_text)

0 commit comments

Comments
 (0)