Skip to content

Commit 1813f61

Browse files
committed
Fix flaky coverage for Pandas
1 parent 9577dd6 commit 1813f61

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

hypothesis-python/src/hypothesis/internal/coverage.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def pretty_file_name(f):
4848

4949

5050
IN_COVERAGE_TESTS = os.getenv("HYPOTHESIS_INTERNAL_COVERAGE") == "true"
51+
description_stack = []
5152

5253

5354
if IN_COVERAGE_TESTS:
@@ -64,8 +65,6 @@ def record_branch(name, value):
6465
with open(f"branch-check-{os.getpid()}", mode="a", encoding="utf-8") as log:
6566
log.write(json.dumps({"name": name, "value": value}) + "\n")
6667

67-
description_stack = []
68-
6968
@contextmanager
7069
def check_block(name, depth):
7170
# We add an extra two callers to the stack: One for the contextmanager

hypothesis-python/src/hypothesis/strategies/_internal/core.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
is_typed_named_tuple,
8484
)
8585
from hypothesis.internal.conjecture.utils import calc_label_from_cls, check_sample
86+
from hypothesis.internal.coverage import IN_COVERAGE_TESTS, description_stack
8687
from hypothesis.internal.entropy import get_seeder_and_restorer
8788
from hypothesis.internal.floats import float_of
8889
from hypothesis.internal.observability import TESTCASE_CALLBACKS
@@ -1757,8 +1758,22 @@ def __init__(self, definition, args, kwargs):
17571758
self.args = args
17581759
self.kwargs = kwargs
17591760

1760-
def do_draw(self, data):
1761-
return self.definition(data.draw, *self.args, **self.kwargs)
1761+
if IN_COVERAGE_TESTS:
1762+
# We do a bit of a dance here to ensure that whatever 'outer' description
1763+
# stack we might have, doesn't affect the validation code internal to the
1764+
# strategy we're drawing from. Otherwise, we'd get flaky fails like #3968.
1765+
def do_draw(self, data):
1766+
prev = description_stack[:]
1767+
try:
1768+
description_stack.clear()
1769+
return self.definition(data.draw, *self.args, **self.kwargs)
1770+
finally:
1771+
description_stack[:] = prev
1772+
1773+
else: # pragma: no cover
1774+
1775+
def do_draw(self, data):
1776+
return self.definition(data.draw, *self.args, **self.kwargs)
17621777

17631778
def calc_label(self):
17641779
return calc_label_from_cls(self.definition)

0 commit comments

Comments
 (0)