Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/hypothesis/strategies/_internal/shared.py: 42%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

31 statements  

1# This file is part of Hypothesis, which may be found at 

2# https://github.com/HypothesisWorks/hypothesis/ 

3# 

4# Copyright the Hypothesis Authors. 

5# Individual contributors are listed in AUTHORS.rst and the git log. 

6# 

7# This Source Code Form is subject to the terms of the Mozilla Public License, 

8# v. 2.0. If a copy of the MPL was not distributed with this file, You can 

9# obtain one at https://mozilla.org/MPL/2.0/. 

10 

11import warnings 

12from collections.abc import Hashable 

13from typing import Any, Optional 

14 

15from hypothesis.errors import HypothesisWarning 

16from hypothesis.internal.conjecture.data import ConjectureData 

17from hypothesis.strategies._internal import SearchStrategy 

18from hypothesis.strategies._internal.strategies import Ex 

19 

20 

21class SharedStrategy(SearchStrategy[Ex]): 

22 def __init__(self, base: SearchStrategy[Ex], key: Optional[Hashable] = None): 

23 self.key = key 

24 self.base = base 

25 

26 @property 

27 def supports_find(self) -> bool: 

28 return self.base.supports_find 

29 

30 def __repr__(self) -> str: 

31 if self.key is not None: 

32 return f"shared({self.base!r}, key={self.key!r})" 

33 else: 

34 return f"shared({self.base!r})" 

35 

36 # Ideally would be -> Ex, but key collisions with different-typed values are 

37 # possible. See https://github.com/HypothesisWorks/hypothesis/issues/4301. 

38 def do_draw(self, data: ConjectureData) -> Any: 

39 if self.key is None or getattr(self.base, "_is_singleton", False): 

40 strat_label = id(self.base) 

41 else: 

42 # Assume that uncached strategies are distinguishable by their 

43 # label. False negatives (even collisions w/id above) are ok as 

44 # long as they are infrequent. 

45 strat_label = self.base.label 

46 key = self.key or self 

47 if key not in data._shared_strategy_draws: 

48 drawn = data.draw(self.base) 

49 data._shared_strategy_draws[key] = (strat_label, drawn) 

50 else: 

51 drawn_strat_label, drawn = data._shared_strategy_draws[key] 

52 if drawn_strat_label != strat_label: 

53 warnings.warn( 

54 f"Different strategies are shared under {key=}. This" 

55 " risks drawing values that are not valid examples for the strategy," 

56 " or that have a narrower range than expected.", 

57 HypothesisWarning, 

58 stacklevel=1, 

59 ) 

60 return drawn