Skip to content

Commit 8b28c80

Browse files
committed
Final type-annotation tweaks
1 parent 35d655b commit 8b28c80

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

hypothesis-python/src/hypothesis/internal/conjecture/data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def wrapper(tp):
9898
InterestingOrigin = Tuple[
9999
Type[BaseException], str, int, Tuple[Any, ...], Tuple[Tuple[Any, ...], ...]
100100
]
101-
TargetObservations = Dict[Optional[str], Union[int, float]]
101+
TargetObservations = Dict[str, Union[int, float]]
102102

103103
T = TypeVar("T")
104104

hypothesis-python/src/hypothesis/internal/conjecture/engine.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from enum import Enum
1818
from random import Random, getrandbits
1919
from typing import (
20+
Any,
2021
Callable,
2122
Dict,
2223
Final,
@@ -27,6 +28,7 @@
2728
NoReturn,
2829
Optional,
2930
Set,
31+
Tuple,
3032
Union,
3133
overload,
3234
)
@@ -217,9 +219,7 @@ def __init__(
217219
self.stats_per_test_case: List[CallStats] = []
218220

219221
# At runtime, the keys are only ever type `InterestingOrigin`, but can be `None` during tests.
220-
self.interesting_examples: Dict[
221-
Optional[InterestingOrigin], ConjectureResult
222-
] = {}
222+
self.interesting_examples: Dict[InterestingOrigin, ConjectureResult] = {}
223223
# We use call_count because there may be few possible valid_examples.
224224
self.first_bug_found_at: Optional[int] = None
225225
self.last_bug_found_at: Optional[int] = None
@@ -317,15 +317,18 @@ def _cache_key_ir(
317317
*,
318318
nodes: Optional[List[IRNode]] = None,
319319
data: Union[ConjectureData, ConjectureResult, None] = None,
320-
):
320+
) -> Tuple[Tuple[Any, ...], ...]:
321321
assert (nodes is not None) ^ (data is not None)
322322
extension = []
323323
if data is not None:
324324
nodes = data.examples.ir_tree_nodes
325325
if data.invalid_at is not None:
326326
# if we're invalid then we should have at least one node left (the invalid one).
327+
assert isinstance(data, ConjectureData)
328+
assert data.ir_tree_nodes is not None
327329
assert data._node_index < len(data.ir_tree_nodes)
328330
extension = [data.ir_tree_nodes[data._node_index]]
331+
assert nodes is not None
329332

330333
# intentionally drop was_forced from equality here, because the was_forced
331334
# of node prefixes on ConjectureData has no impact on that data's result
@@ -356,7 +359,9 @@ def _cache(self, data: Union[ConjectureData, ConjectureResult]) -> None:
356359
key = self._cache_key_ir(data=data)
357360
self.__data_cache_ir[key] = result
358361

359-
def cached_test_function_ir(self, nodes: List[IRNode]) -> ConjectureResult:
362+
def cached_test_function_ir(
363+
self, nodes: List[IRNode]
364+
) -> Union[ConjectureResult, _Overrun]:
360365
key = self._cache_key_ir(nodes=nodes)
361366
try:
362367
return self.__data_cache_ir[key]
@@ -427,7 +432,6 @@ def test_function(self, data: ConjectureData) -> None:
427432

428433
if data.status >= Status.VALID:
429434
for k, v in data.target_observations.items():
430-
assert k is not None
431435
self.best_observed_targets[k] = max(self.best_observed_targets[k], v)
432436

433437
if k not in self.best_examples_of_observed_targets:
@@ -471,7 +475,7 @@ def test_function(self, data: ConjectureData) -> None:
471475
key = data.interesting_origin
472476
changed = False
473477
try:
474-
existing = self.interesting_examples[key]
478+
existing = self.interesting_examples[key] # type: ignore
475479
except KeyError:
476480
changed = True
477481
self.last_bug_found_at = self.call_count
@@ -1153,7 +1157,7 @@ def new_conjecture_data_ir(
11531157
ir_tree_prefix: List[IRNode],
11541158
*,
11551159
observer: Optional[DataObserver] = None,
1156-
):
1160+
) -> ConjectureData:
11571161
provider = (
11581162
HypothesisProvider if self._switch_to_hypothesis_provider else self.provider
11591163
)

hypothesis-python/tests/cover/test_composite.py

+4
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ def test_drawfn_cannot_be_instantiated():
196196

197197

198198
@pytest.mark.skipif(sys.version_info[:2] == (3, 9), reason="stack depth varies???")
199+
@pytest.mark.skipif(sys.version_info[:2] <= (3, 11), reason="TEMP: see PR #3961")
199200
def test_warns_on_strategy_annotation():
201+
# TODO: print the stack on Python 3.10 and 3.11 to determine the appropriate
202+
# stack depth to use. Consider adding a debug-print if IN_COVERAGE_TESTS
203+
# and the relevant depth is_hypothesis_file(), for easier future fixing.
200204
with pytest.warns(HypothesisWarning, match="Return-type annotation") as w:
201205

202206
@st.composite

0 commit comments

Comments
 (0)