Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

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

3# 

4# Most of this work is copyright (C) 2013-2020 David R. MacIver 

5# (david@drmaciver.com), but it contains contributions by others. See 

6# CONTRIBUTING.rst for a full list of people who may hold copyright, and 

7# consult the git log if you need to determine who owns an individual 

8# contribution. 

9# 

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

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

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

13# 

14# END HEADER 

15 

16import decimal 

17import math 

18from numbers import Rational, Real 

19 

20from hypothesis.errors import InvalidArgument 

21from hypothesis.internal.coverage import check_function 

22 

23 

24@check_function 

25def check_type(typ, arg, name=""): 

26 if name: 

27 name += "=" 

28 if not isinstance(arg, typ): 

29 if isinstance(typ, tuple): 

30 assert len(typ) >= 2, "Use bare type instead of len-1 tuple" 

31 typ_string = "one of %s" % (", ".join(t.__name__ for t in typ)) 

32 else: 

33 typ_string = typ.__name__ 

34 raise InvalidArgument( 

35 "Expected %s but got %s%r (type=%s)" 

36 % (typ_string, name, arg, type(arg).__name__) 

37 ) 

38 

39 

40@check_function 

41def check_valid_integer(value): 

42 """Checks that value is either unspecified, or a valid integer. 

43 

44 Otherwise raises InvalidArgument. 

45 """ 

46 if value is None: 

47 return 

48 check_type(int, value) 

49 

50 

51@check_function 

52def check_valid_bound(value, name): 

53 """Checks that value is either unspecified, or a valid interval bound. 

54 

55 Otherwise raises InvalidArgument. 

56 """ 

57 if value is None or isinstance(value, (int, Rational)): 

58 return 

59 if not isinstance(value, (Real, decimal.Decimal)): 

60 raise InvalidArgument("%s=%r must be a real number." % (name, value)) 

61 if math.isnan(value): 

62 raise InvalidArgument("Invalid end point %s=%r" % (name, value)) 

63 

64 

65@check_function 

66def check_valid_magnitude(value, name): 

67 """Checks that value is either unspecified, or a non-negative valid 

68 interval bound. 

69 

70 Otherwise raises InvalidArgument. 

71 """ 

72 check_valid_bound(value, name) 

73 if value is not None and value < 0: 

74 raise InvalidArgument("%s=%r must not be negative." % (name, value)) 

75 

76 

77@check_function 

78def try_convert(typ, value, name): 

79 if value is None: 

80 return None 

81 if isinstance(value, typ): 

82 return value 

83 try: 

84 return typ(value) 

85 except (TypeError, ValueError, ArithmeticError): 

86 raise InvalidArgument( 

87 "Cannot convert %s=%r of type %s to type %s" 

88 % (name, value, type(value).__name__, typ.__name__) 

89 ) 

90 

91 

92@check_function 

93def check_valid_size(value, name): 

94 """Checks that value is either unspecified, or a valid non-negative size 

95 expressed as an integer. 

96 

97 Otherwise raises InvalidArgument. 

98 """ 

99 if value is None and name != "min_size": 

100 return 

101 check_type(int, value, name) 

102 if value < 0: 

103 raise InvalidArgument("Invalid size %s=%r < 0" % (name, value)) 

104 

105 

106@check_function 

107def check_valid_interval(lower_bound, upper_bound, lower_name, upper_name): 

108 """Checks that lower_bound and upper_bound are either unspecified, or they 

109 define a valid interval on the number line. 

110 

111 Otherwise raises InvalidArgument. 

112 """ 

113 if lower_bound is None or upper_bound is None: 

114 return 

115 if upper_bound < lower_bound: 

116 raise InvalidArgument( 

117 "Cannot have %s=%r < %s=%r" 

118 % (upper_name, upper_bound, lower_name, lower_bound) 

119 ) 

120 

121 

122@check_function 

123def check_valid_sizes(min_size, max_size): 

124 check_valid_size(min_size, "min_size") 

125 check_valid_size(max_size, "max_size") 

126 check_valid_interval(min_size, max_size, "min_size", "max_size")