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

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

52 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 inspect 

12from collections.abc import Sequence 

13from typing import Callable, Optional 

14 

15from hypothesis.configuration import check_sideeffect_during_initialization 

16from hypothesis.errors import InvalidArgument 

17from hypothesis.internal.conjecture.data import ConjectureData 

18from hypothesis.internal.reflection import get_pretty_function_description 

19from hypothesis.strategies._internal.strategies import ( 

20 Ex, 

21 RecurT, 

22 SearchStrategy, 

23 check_strategy, 

24) 

25 

26 

27class DeferredStrategy(SearchStrategy[Ex]): 

28 """A strategy which may be used before it is fully defined.""" 

29 

30 def __init__(self, definition: Callable[[], SearchStrategy[Ex]]): 

31 super().__init__() 

32 self.__wrapped_strategy: Optional[SearchStrategy[Ex]] = None 

33 self.__in_repr: bool = False 

34 self.__definition: Optional[Callable[[], SearchStrategy[Ex]]] = definition 

35 

36 @property 

37 def wrapped_strategy(self) -> SearchStrategy[Ex]: 

38 if self.__wrapped_strategy is None: 

39 check_sideeffect_during_initialization("deferred evaluation of {!r}", self) 

40 

41 if not inspect.isfunction(self.__definition): 

42 raise InvalidArgument( 

43 f"Expected definition to be a function but got {self.__definition!r} " 

44 f"of type {type(self.__definition).__name__} instead." 

45 ) 

46 result = self.__definition() 

47 if result is self: 

48 raise InvalidArgument("Cannot define a deferred strategy to be itself") 

49 check_strategy(result, "definition()") 

50 self.__wrapped_strategy = result 

51 self.__definition = None 

52 return self.__wrapped_strategy 

53 

54 @property 

55 def branches(self) -> Sequence[SearchStrategy[Ex]]: 

56 return self.wrapped_strategy.branches 

57 

58 @property 

59 def supports_find(self) -> bool: 

60 return self.wrapped_strategy.supports_find 

61 

62 def calc_label(self) -> int: 

63 """Deferred strategies don't have a calculated label, because we would 

64 end up having to calculate the fixed point of some hash function in 

65 order to calculate it when they recursively refer to themself! 

66 

67 The label for the wrapped strategy will still appear because it 

68 will be passed to draw. 

69 """ 

70 # This is actually the same as the parent class implementation, but we 

71 # include it explicitly here in order to document that this is a 

72 # deliberate decision. 

73 return self.class_label 

74 

75 def calc_is_empty(self, recur: RecurT) -> bool: 

76 return recur(self.wrapped_strategy) 

77 

78 def calc_has_reusable_values(self, recur: RecurT) -> bool: 

79 return recur(self.wrapped_strategy) 

80 

81 def __repr__(self) -> str: 

82 if self.__wrapped_strategy is not None: 

83 if self.__in_repr: 

84 return f"(deferred@{id(self)!r})" 

85 try: 

86 self.__in_repr = True 

87 return repr(self.__wrapped_strategy) 

88 finally: 

89 self.__in_repr = False 

90 else: 

91 description = get_pretty_function_description(self.__definition) 

92 return f"deferred({description})" 

93 

94 def do_draw(self, data: ConjectureData) -> Ex: 

95 return data.draw(self.wrapped_strategy)