Skip to content

Commit

Permalink
typing, tests, release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed Nov 8, 2024
1 parent 31f7af5 commit eedbd9a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

This patch migrates the :obj:`~hypothesis.Phase.explain` :ref:`phase <phases>` to our IR layer (:issue:`3921`). This should improve both its speed and precision.
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def ir_node(self, ir_node: "IRNode") -> None:
def finish(self):
return tuple(self.result)

ir_tree_nodes: "tuple[IRNode]" = calculated_example_property(_ir_tree_nodes)
ir_tree_nodes: "tuple[IRNode, ...]" = calculated_example_property(_ir_tree_nodes)

class _label_indices(ExampleProperty):
def start_example(self, i: int, label_index: int) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# obtain one at https://mozilla.org/MPL/2.0/.

from collections import defaultdict
from collections.abc import Sequence
from typing import TYPE_CHECKING, Callable, Optional, TypeVar, Union

import attr
Expand All @@ -22,6 +23,7 @@
from hypothesis.internal.conjecture.data import (
ConjectureData,
ConjectureResult,
IRNode,
Status,
ir_to_buffer,
ir_value_equal,
Expand Down Expand Up @@ -391,7 +393,7 @@ def cached_test_function_ir(self, tree):
self.check_calls()
return result

def consider_new_tree(self, tree):
def consider_new_tree(self, tree: Sequence[IRNode]) -> bool:
tree = tree[: len(self.nodes)]

if startswith(tree, self.nodes):
Expand Down
26 changes: 26 additions & 0 deletions hypothesis-python/tests/conjecture/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,32 @@ def test(cd):
assert runner.call_count == 1


@given(ir_nodes(was_forced=False))
def test_overruns_with_extend_are_not_cached(node):
assume(compute_max_children(node.ir_type, node.kwargs) > 100)

def test(cd):
_draw(cd, node)
_draw(cd, node)

runner = ConjectureRunner(test)
assert runner.call_count == 0

data = runner.cached_test_function_ir([node])
assert runner.call_count == 1
assert data.status is Status.OVERRUN

# cache hit
data = runner.cached_test_function_ir([node])
assert runner.call_count == 1
assert data.status is Status.OVERRUN

# cache miss
data = runner.cached_test_function_ir([node], extend=1)
assert runner.call_count == 2
assert data.status is Status.VALID


def test_simulate_to_evicted_data(monkeypatch):
# test that we do not rely on the false invariant that correctly simulating
# a data to a result means we have that result in the cache, due to e.g.
Expand Down
6 changes: 6 additions & 0 deletions hypothesis-python/tests/conjecture/test_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ def test_ir_node_equality(node):
assert node != 42


@given(ir_nodes(was_forced=True))
def test_cannot_modify_forced_nodes(node):
with pytest.raises(AssertionError):
node.copy(with_value=42)


def test_data_with_empty_ir_tree_is_overrun():
data = ConjectureData.for_ir_tree([])
with pytest.raises(StopTest):
Expand Down
12 changes: 12 additions & 0 deletions hypothesis-python/tests/conjecture/test_junkdrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
clamp,
replace_all,
stack_depth_of_caller,
startswith,
endswith,
)


Expand Down Expand Up @@ -202,3 +204,13 @@ def zero(n):

assert x.find(zero) == 0
assert count[0] == 21


@given(st.binary(), st.binary())
def test_startswith(b1, b2):
assert b1.startswith(b2) == startswith(b1, b2)


@given(st.binary(), st.binary())
def test_endswith(b1, b2):
assert b1.endswith(b2) == endswith(b1, b2)

0 comments on commit eedbd9a

Please sign in to comment.