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

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

37 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 

11from weakref import WeakKeyDictionary 

12 

13from hypothesis.control import note, should_note 

14from hypothesis.errors import InvalidState 

15from hypothesis.internal.reflection import ( 

16 convert_positional_arguments, 

17 nicerepr, 

18 proxies, 

19 repr_call, 

20) 

21from hypothesis.strategies._internal.strategies import RecurT, SearchStrategy 

22 

23 

24class FunctionStrategy(SearchStrategy): 

25 supports_find = False 

26 

27 def __init__(self, like, returns, pure): 

28 super().__init__() 

29 self.like = like 

30 self.returns = returns 

31 self.pure = pure 

32 # Using wekrefs-to-generated-functions means that the cache can be 

33 # garbage-collected at the end of each example, reducing memory use. 

34 self._cache = WeakKeyDictionary() 

35 

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

37 return recur(self.returns) 

38 

39 def do_draw(self, data): 

40 @proxies(self.like) 

41 def inner(*args, **kwargs): 

42 if data.frozen: 

43 raise InvalidState( 

44 f"This generated {nicerepr(self.like)} function can only " 

45 "be called within the scope of the @given that created it." 

46 ) 

47 if self.pure: 

48 args, kwargs = convert_positional_arguments(self.like, args, kwargs) 

49 key = (args, frozenset(kwargs.items())) 

50 cache = self._cache.setdefault(inner, {}) 

51 if key not in cache: 

52 cache[key] = data.draw(self.returns) 

53 if should_note(): # optimization to avoid needless repr_call 

54 rep = repr_call(self.like, args, kwargs, reorder=False) 

55 note(f"Called function: {rep} -> {cache[key]!r}") 

56 return cache[key] 

57 else: 

58 val = data.draw(self.returns) 

59 if should_note(): 

60 rep = repr_call(self.like, args, kwargs, reorder=False) 

61 note(f"Called function: {rep} -> {val!r}") 

62 return val 

63 

64 return inner