strategies.py 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. # coding=utf-8
  2. #
  3. # This file is part of Hypothesis, which may be found at
  4. # https://github.com/HypothesisWorks/hypothesis-python
  5. #
  6. # Most of this work is copyright (C) 2013-2018 David R. MacIver
  7. # (david@drmaciver.com), but it contains contributions by others. See
  8. # CONTRIBUTING.rst for a full list of people who may hold copyright, and
  9. # consult the git log if you need to determine who owns an individual
  10. # contribution.
  11. #
  12. # This Source Code Form is subject to the terms of the Mozilla Public License,
  13. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  14. # obtain one at http://mozilla.org/MPL/2.0/.
  15. #
  16. # END HEADER
  17. from __future__ import division, print_function, absolute_import
  18. import enum
  19. import math
  20. import datetime as dt
  21. import operator
  22. from decimal import Context, Decimal
  23. from inspect import isclass, isfunction
  24. from fractions import Fraction
  25. from functools import reduce
  26. from hypothesis.errors import InvalidArgument, ResolutionFailed
  27. from hypothesis.control import assume
  28. from hypothesis._settings import note_deprecation
  29. from hypothesis.internal.cache import LRUReusedCache
  30. from hypothesis.searchstrategy import SearchStrategy
  31. from hypothesis.internal.compat import gcd, ceil, floor, hrange, \
  32. text_type, get_type_hints, getfullargspec, implements_iterator
  33. from hypothesis.internal.floats import is_negative, float_to_int, \
  34. int_to_float, count_between_floats
  35. from hypothesis.internal.renaming import renamed_arguments
  36. from hypothesis.utils.conventions import infer, not_set
  37. from hypothesis.internal.reflection import proxies, required_args
  38. from hypothesis.internal.validation import check_type, try_convert, \
  39. check_strategy, check_valid_bound, check_valid_sizes, \
  40. check_valid_integer, check_valid_interval
  41. __all__ = [
  42. 'nothing',
  43. 'just', 'one_of',
  44. 'none',
  45. 'choices', 'streaming',
  46. 'booleans', 'integers', 'floats', 'complex_numbers', 'fractions',
  47. 'decimals',
  48. 'characters', 'text', 'from_regex', 'binary', 'uuids',
  49. 'tuples', 'lists', 'sets', 'frozensets', 'iterables',
  50. 'dictionaries', 'fixed_dictionaries',
  51. 'sampled_from', 'permutations',
  52. 'datetimes', 'dates', 'times', 'timedeltas',
  53. 'builds',
  54. 'randoms', 'random_module',
  55. 'recursive', 'composite',
  56. 'shared', 'runner', 'data',
  57. 'deferred',
  58. 'from_type', 'register_type_strategy',
  59. ]
  60. _strategies = set()
  61. class FloatKey(object):
  62. def __init__(self, f):
  63. self.value = float_to_int(f)
  64. def __eq__(self, other):
  65. return isinstance(other, FloatKey) and (
  66. other.value == self.value
  67. )
  68. def __ne__(self, other):
  69. return not self.__eq__(other)
  70. def __hash__(self):
  71. return hash(self.value)
  72. def convert_value(v):
  73. if isinstance(v, float):
  74. return FloatKey(v)
  75. return (type(v), v)
  76. STRATEGY_CACHE = LRUReusedCache(1024)
  77. def cacheable(fn):
  78. @proxies(fn)
  79. def cached_strategy(*args, **kwargs):
  80. kwargs_cache_key = set()
  81. try:
  82. for k, v in kwargs.items():
  83. kwargs_cache_key.add((k, convert_value(v)))
  84. except TypeError:
  85. return fn(*args, **kwargs)
  86. cache_key = (
  87. fn,
  88. tuple(map(convert_value, args)), frozenset(kwargs_cache_key))
  89. try:
  90. return STRATEGY_CACHE[cache_key]
  91. except TypeError:
  92. return fn(*args, **kwargs)
  93. except KeyError:
  94. result = fn(*args, **kwargs)
  95. if not isinstance(result, SearchStrategy) or result.is_cacheable:
  96. STRATEGY_CACHE[cache_key] = result
  97. return result
  98. cached_strategy.__clear_cache = STRATEGY_CACHE.clear
  99. return cached_strategy
  100. def base_defines_strategy(force_reusable):
  101. def decorator(strategy_definition):
  102. from hypothesis.searchstrategy.lazy import LazyStrategy
  103. _strategies.add(strategy_definition.__name__)
  104. @proxies(strategy_definition)
  105. def accept(*args, **kwargs):
  106. result = LazyStrategy(strategy_definition, args, kwargs)
  107. if force_reusable:
  108. result.force_has_reusable_values = True
  109. assert result.has_reusable_values
  110. return result
  111. return accept
  112. return decorator
  113. defines_strategy = base_defines_strategy(False)
  114. defines_strategy_with_reusable_values = base_defines_strategy(True)
  115. class Nothing(SearchStrategy):
  116. def calc_is_empty(self, recur):
  117. return True
  118. def do_draw(self, data):
  119. # This method should never be called because draw() will mark the
  120. # data as invalid immediately because is_empty is True.
  121. assert False # pragma: no cover
  122. def calc_has_reusable_values(self, recur):
  123. return True
  124. def __repr__(self):
  125. return 'nothing()'
  126. def map(self, f):
  127. return self
  128. def filter(self, f):
  129. return self
  130. def flatmap(self, f):
  131. return self
  132. NOTHING = Nothing()
  133. @cacheable
  134. def nothing():
  135. """This strategy never successfully draws a value and will always reject on
  136. an attempt to draw.
  137. Examples from this strategy do not shrink (because there are none).
  138. """
  139. return NOTHING
  140. def just(value):
  141. """Return a strategy which only generates ``value``.
  142. Note: ``value`` is not copied. Be wary of using mutable values.
  143. If ``value`` is the result of a callable, you can use
  144. :func:`builds(callable) <hypothesis.strategies.builds>` instead
  145. of ``just(callable())`` to get a fresh value each time.
  146. Examples from this strategy do not shrink (because there is only one).
  147. """
  148. from hypothesis.searchstrategy.misc import JustStrategy
  149. return JustStrategy(value)
  150. @defines_strategy
  151. def none():
  152. """Return a strategy which only generates None.
  153. Examples from this strategy do not shrink (because there is only
  154. one).
  155. """
  156. return just(None)
  157. def one_of(*args):
  158. """Return a strategy which generates values from any of the argument
  159. strategies.
  160. This may be called with one iterable argument instead of multiple
  161. strategy arguments. In which case one_of(x) and one_of(\*x) are
  162. equivalent.
  163. Examples from this strategy will generally shrink to ones that come from
  164. strategies earlier in the list, then shrink according to behaviour of the
  165. strategy that produced them. In order to get good shrinking behaviour,
  166. try to put simpler strategies first. e.g. ``one_of(none(), text())`` is
  167. better than ``one_of(text(), none())``.
  168. This is especially important when using recursive strategies. e.g.
  169. ``x = st.deferred(lambda: st.none() | st.tuples(x, x))`` will shrink well,
  170. but ``x = st.deferred(lambda: st.tuples(x, x) | st.none())`` will shrink
  171. very badly indeed.
  172. """
  173. if len(args) == 1 and not isinstance(args[0], SearchStrategy):
  174. try:
  175. args = tuple(args[0])
  176. except TypeError:
  177. pass
  178. from hypothesis.searchstrategy.strategies import OneOfStrategy
  179. return OneOfStrategy(args)
  180. @cacheable
  181. @defines_strategy_with_reusable_values
  182. def integers(min_value=None, max_value=None):
  183. """Returns a strategy which generates integers (in Python 2 these may be
  184. ints or longs).
  185. If min_value is not None then all values will be >= min_value. If
  186. max_value is not None then all values will be <= max_value
  187. Examples from this strategy will shrink towards being positive (e.g. 1000
  188. is considered simpler than -1) and then towards zero.
  189. """
  190. check_valid_bound(min_value, 'min_value')
  191. check_valid_bound(max_value, 'max_value')
  192. check_valid_interval(min_value, max_value, 'min_value', 'max_value')
  193. from hypothesis.searchstrategy.numbers import IntegersFromStrategy, \
  194. BoundedIntStrategy, WideRangeIntStrategy
  195. min_int_value = None if min_value is None else ceil(min_value)
  196. max_int_value = None if max_value is None else floor(max_value)
  197. if min_int_value is not None and max_int_value is not None and \
  198. min_int_value > max_int_value:
  199. raise InvalidArgument('No integers between min_value=%r and '
  200. 'max_value=%r' % (min_value, max_value))
  201. if min_int_value is None:
  202. if max_int_value is None:
  203. return (
  204. WideRangeIntStrategy()
  205. )
  206. else:
  207. return IntegersFromStrategy(0).map(lambda x: max_int_value - x)
  208. else:
  209. if max_int_value is None:
  210. return IntegersFromStrategy(min_int_value)
  211. else:
  212. assert min_int_value <= max_int_value
  213. if min_int_value == max_int_value:
  214. return just(min_int_value)
  215. elif min_int_value >= 0:
  216. return BoundedIntStrategy(min_int_value, max_int_value)
  217. elif max_int_value <= 0:
  218. return BoundedIntStrategy(
  219. -max_int_value, -min_int_value
  220. ).map(lambda t: -t)
  221. else:
  222. return integers(min_value=0, max_value=max_int_value) | \
  223. integers(min_value=min_int_value, max_value=0)
  224. @cacheable
  225. @defines_strategy
  226. def booleans():
  227. """Returns a strategy which generates instances of bool.
  228. Examples from this strategy will shrink towards False (i.e.
  229. shrinking will try to replace True with False where possible).
  230. """
  231. from hypothesis.searchstrategy.misc import BoolStrategy
  232. return BoolStrategy()
  233. @cacheable
  234. @defines_strategy_with_reusable_values
  235. def floats(
  236. min_value=None, max_value=None, allow_nan=None, allow_infinity=None
  237. ):
  238. """Returns a strategy which generates floats.
  239. - If min_value is not None, all values will be >= min_value.
  240. - If max_value is not None, all values will be <= max_value.
  241. - If min_value or max_value is not None, it is an error to enable
  242. allow_nan.
  243. - If both min_value and max_value are not None, it is an error to enable
  244. allow_infinity.
  245. Where not explicitly ruled out by the bounds, all of infinity, -infinity
  246. and NaN are possible values generated by this strategy.
  247. Examples from this strategy have a complicated and hard to explain
  248. shrinking behaviour, but it tries to improve "human readability". Finite
  249. numbers will be preferred to infinity and infinity will be preferred to
  250. NaN.
  251. """
  252. if allow_nan is None:
  253. allow_nan = bool(min_value is None and max_value is None)
  254. elif allow_nan:
  255. if min_value is not None or max_value is not None:
  256. raise InvalidArgument(
  257. 'Cannot have allow_nan=%r, with min_value or max_value' % (
  258. allow_nan
  259. ))
  260. min_value = try_convert(float, min_value, 'min_value')
  261. max_value = try_convert(float, max_value, 'max_value')
  262. check_valid_bound(min_value, 'min_value')
  263. check_valid_bound(max_value, 'max_value')
  264. check_valid_interval(min_value, max_value, 'min_value', 'max_value')
  265. if min_value == float(u'-inf'):
  266. min_value = None
  267. if max_value == float(u'inf'):
  268. max_value = None
  269. if allow_infinity is None:
  270. allow_infinity = bool(min_value is None or max_value is None)
  271. elif allow_infinity:
  272. if min_value is not None and max_value is not None:
  273. raise InvalidArgument(
  274. 'Cannot have allow_infinity=%r, with both min_value and '
  275. 'max_value' % (
  276. allow_infinity
  277. ))
  278. from hypothesis.searchstrategy.numbers import FloatStrategy, \
  279. FixedBoundedFloatStrategy
  280. if min_value is None and max_value is None:
  281. return FloatStrategy(
  282. allow_infinity=allow_infinity, allow_nan=allow_nan,
  283. )
  284. elif min_value is not None and max_value is not None:
  285. if min_value == max_value:
  286. return just(min_value)
  287. elif is_negative(min_value):
  288. if is_negative(max_value):
  289. return floats(min_value=-max_value, max_value=-min_value).map(
  290. operator.neg
  291. )
  292. else:
  293. return floats(min_value=0.0, max_value=max_value) | floats(
  294. min_value=0.0, max_value=-min_value).map(operator.neg)
  295. elif count_between_floats(min_value, max_value) > 1000:
  296. return FixedBoundedFloatStrategy(
  297. lower_bound=min_value, upper_bound=max_value
  298. )
  299. else:
  300. ub_int = float_to_int(max_value)
  301. lb_int = float_to_int(min_value)
  302. assert lb_int <= ub_int
  303. return integers(min_value=lb_int, max_value=ub_int).map(
  304. int_to_float
  305. )
  306. elif min_value is not None:
  307. if min_value < 0:
  308. result = floats(
  309. min_value=0.0
  310. ) | floats(min_value=min_value, max_value=-0.0)
  311. else:
  312. result = (
  313. floats(allow_infinity=allow_infinity, allow_nan=False).map(
  314. lambda x: assume(not math.isnan(x)) and min_value + abs(x)
  315. )
  316. )
  317. if min_value == 0 and not is_negative(min_value):
  318. result = result.filter(lambda x: math.copysign(1.0, x) == 1)
  319. return result
  320. else:
  321. assert max_value is not None
  322. if max_value > 0:
  323. result = floats(
  324. min_value=0.0,
  325. max_value=max_value,
  326. ) | floats(max_value=-0.0)
  327. else:
  328. result = (
  329. floats(allow_infinity=allow_infinity, allow_nan=False).map(
  330. lambda x: assume(not math.isnan(x)) and max_value - abs(x)
  331. )
  332. )
  333. if max_value == 0 and is_negative(max_value):
  334. result = result.filter(is_negative)
  335. return result
  336. @cacheable
  337. @defines_strategy_with_reusable_values
  338. def complex_numbers():
  339. """Returns a strategy that generates complex numbers.
  340. Examples from this strategy shrink by shrinking their component real
  341. and imaginary parts.
  342. """
  343. from hypothesis.searchstrategy.numbers import ComplexStrategy
  344. return ComplexStrategy(
  345. tuples(floats(), floats())
  346. )
  347. @cacheable
  348. @defines_strategy
  349. def tuples(*args):
  350. """Return a strategy which generates a tuple of the same length as args by
  351. generating the value at index i from args[i].
  352. e.g. tuples(integers(), integers()) would generate a tuple of length
  353. two with both values an integer.
  354. Examples from this strategy shrink by shrinking their component parts.
  355. """
  356. for arg in args:
  357. check_strategy(arg)
  358. from hypothesis.searchstrategy.collections import TupleStrategy
  359. return TupleStrategy(args, tuple)
  360. @defines_strategy
  361. def sampled_from(elements):
  362. """Returns a strategy which generates any value present in ``elements``.
  363. Note that as with :func:`~hypotheses.strategies.just`, values will not be
  364. copied and thus you should be careful of using mutable data.
  365. ``sampled_from`` supports ordered collections, as well as
  366. :class:`~python:enum.Enum` objects. :class:`~python:enum.Flag` objects
  367. may also generate any combination of their members.
  368. Examples from this strategy shrink by replacing them with values earlier in
  369. the list. So e.g. sampled_from((10, 1)) will shrink by trying to replace
  370. 1 values with 10, and sampled_from((1, 10)) will shrink by trying to
  371. replace 10 values with 1.
  372. """
  373. from hypothesis.searchstrategy.misc import SampledFromStrategy
  374. from hypothesis.internal.conjecture.utils import check_sample
  375. values = check_sample(elements)
  376. if not values:
  377. return nothing()
  378. if len(values) == 1:
  379. return just(values[0])
  380. if hasattr(enum, 'Flag') and isclass(elements) and \
  381. issubclass(elements, enum.Flag):
  382. # Combinations of enum.Flag members are also members. We generate
  383. # these dynamically, because static allocation takes O(2^n) memory.
  384. return sets(sampled_from(values), min_size=1).map(
  385. lambda s: reduce(operator.or_, s))
  386. return SampledFromStrategy(values)
  387. _AVERAGE_LIST_LENGTH = 5.0
  388. @cacheable
  389. @defines_strategy
  390. def lists(
  391. elements=None, min_size=None, average_size=None, max_size=None,
  392. unique_by=None, unique=False,
  393. ):
  394. """Returns a list containing values drawn from elements with length in the
  395. interval [min_size, max_size] (no bounds in that direction if these are
  396. None). If max_size is 0 then elements may be None and only the empty list
  397. will be drawn.
  398. average_size may be used as a size hint to roughly control the size
  399. of the list but it may not be the actual average of sizes you get, due
  400. to a variety of factors.
  401. If unique is True (or something that evaluates to True), we compare direct
  402. object equality, as if unique_by was `lambda x: x`. This comparison only
  403. works for hashable types.
  404. if unique_by is not None it must be a function returning a hashable type
  405. when given a value drawn from elements. The resulting list will satisfy the
  406. condition that for i != j, unique_by(result[i]) != unique_by(result[j]).
  407. Examples from this strategy shrink by trying to remove elements from the
  408. list, and by shrinking each individual element of the list.
  409. """
  410. check_valid_sizes(min_size, average_size, max_size)
  411. if elements is None or (max_size is not None and max_size <= 0):
  412. if max_size is None or max_size > 0:
  413. raise InvalidArgument(
  414. u'Cannot create non-empty lists without an element type'
  415. )
  416. else:
  417. return builds(list)
  418. check_strategy(elements)
  419. if unique:
  420. if unique_by is not None:
  421. raise InvalidArgument((
  422. 'cannot specify both unique and unique_by (you probably only '
  423. 'want to set unique_by)'
  424. ))
  425. else:
  426. def unique_by(x):
  427. return x
  428. if unique_by is not None:
  429. from hypothesis.searchstrategy.collections import UniqueListStrategy
  430. min_size = min_size or 0
  431. max_size = max_size or float(u'inf')
  432. if average_size is None:
  433. if max_size < float(u'inf'):
  434. if max_size <= 5:
  435. average_size = min_size + 0.75 * (max_size - min_size)
  436. else:
  437. average_size = (max_size + min_size) / 2
  438. else:
  439. average_size = max(
  440. _AVERAGE_LIST_LENGTH,
  441. min_size * 2
  442. )
  443. result = UniqueListStrategy(
  444. elements=elements,
  445. average_size=average_size,
  446. max_size=max_size,
  447. min_size=min_size,
  448. key=unique_by
  449. )
  450. else:
  451. from hypothesis.searchstrategy.collections import ListStrategy
  452. if min_size is None:
  453. min_size = 0
  454. if average_size is None:
  455. if max_size is None:
  456. average_size = _AVERAGE_LIST_LENGTH
  457. else:
  458. average_size = (min_size + max_size) * 0.5
  459. result = ListStrategy(
  460. (elements,), average_length=average_size,
  461. min_size=min_size, max_size=max_size,
  462. )
  463. return result
  464. @cacheable
  465. @defines_strategy
  466. def sets(elements=None, min_size=None, average_size=None, max_size=None):
  467. """This has the same behaviour as lists, but returns sets instead.
  468. Note that Hypothesis cannot tell if values are drawn from elements
  469. are hashable until running the test, so you can define a strategy
  470. for sets of an unhashable type but it will fail at test time.
  471. Examples from this strategy shrink by trying to remove elements from the
  472. set, and by shrinking each individual element of the set.
  473. """
  474. return lists(
  475. elements=elements, min_size=min_size, average_size=average_size,
  476. max_size=max_size, unique=True
  477. ).map(set)
  478. @cacheable
  479. @defines_strategy
  480. def frozensets(elements=None, min_size=None, average_size=None, max_size=None):
  481. """This is identical to the sets function but instead returns
  482. frozensets."""
  483. return lists(
  484. elements=elements, min_size=min_size, average_size=average_size,
  485. max_size=max_size, unique=True
  486. ).map(frozenset)
  487. @defines_strategy
  488. def iterables(elements=None, min_size=None, average_size=None, max_size=None,
  489. unique_by=None, unique=False):
  490. """This has the same behaviour as lists, but returns iterables instead.
  491. Some iterables cannot be indexed (e.g. sets) and some do not have a
  492. fixed length (e.g. generators). This strategy produces iterators,
  493. which cannot be indexed and do not have a fixed length. This ensures
  494. that you do not accidentally depend on sequence behaviour.
  495. """
  496. @implements_iterator
  497. class PrettyIter(object):
  498. def __init__(self, values):
  499. self._values = values
  500. self._iter = iter(self._values)
  501. def __iter__(self):
  502. return self._iter
  503. def __next__(self):
  504. return next(self._iter)
  505. def __repr__(self):
  506. return 'iter({!r})'.format(self._values)
  507. return lists(
  508. elements=elements, min_size=min_size, average_size=average_size,
  509. max_size=max_size, unique_by=unique_by, unique=unique
  510. ).map(PrettyIter)
  511. @defines_strategy
  512. def fixed_dictionaries(mapping):
  513. """Generates a dictionary of the same type as mapping with a fixed set of
  514. keys mapping to strategies. mapping must be a dict subclass.
  515. Generated values have all keys present in mapping, with the
  516. corresponding values drawn from mapping[key]. If mapping is an
  517. instance of OrderedDict the keys will also be in the same order,
  518. otherwise the order is arbitrary.
  519. Examples from this strategy shrink by shrinking each individual value in
  520. the generated dictionary.
  521. """
  522. from hypothesis.searchstrategy.collections import FixedKeysDictStrategy
  523. check_type(dict, mapping, 'mapping')
  524. for v in mapping.values():
  525. check_strategy(v)
  526. return FixedKeysDictStrategy(mapping)
  527. @cacheable
  528. @defines_strategy
  529. def dictionaries(
  530. keys, values, dict_class=dict,
  531. min_size=None, average_size=None, max_size=None
  532. ):
  533. """Generates dictionaries of type dict_class with keys drawn from the keys
  534. argument and values drawn from the values argument.
  535. The size parameters have the same interpretation as for lists.
  536. Examples from this strategy shrink by trying to remove keys from the
  537. generated dictionary, and by shrinking each generated key and value.
  538. """
  539. check_valid_sizes(min_size, average_size, max_size)
  540. if max_size == 0:
  541. return fixed_dictionaries(dict_class())
  542. check_strategy(keys)
  543. check_strategy(values)
  544. return lists(
  545. tuples(keys, values),
  546. min_size=min_size, average_size=average_size, max_size=max_size,
  547. unique_by=lambda x: x[0]
  548. ).map(dict_class)
  549. @defines_strategy
  550. def streaming(elements):
  551. """Generates an infinite stream of values where each value is drawn from
  552. elements.
  553. The result is iterable (the iterator will never terminate) and
  554. indexable.
  555. Examples from this strategy shrink by trying to shrink each value drawn.
  556. .. deprecated:: 3.15.0
  557. Use :func:`data() <hypothesis.strategies.data>` instead.
  558. """
  559. note_deprecation(
  560. 'streaming() has been deprecated. Use the data() strategy instead and '
  561. 'replace stream iteration with data.draw() calls.'
  562. )
  563. check_strategy(elements)
  564. from hypothesis.searchstrategy.streams import StreamStrategy
  565. return StreamStrategy(elements)
  566. @cacheable
  567. @defines_strategy_with_reusable_values
  568. def characters(whitelist_categories=None, blacklist_categories=None,
  569. blacklist_characters=None, min_codepoint=None,
  570. max_codepoint=None, whitelist_characters=None):
  571. """Generates unicode text type (unicode on python 2, str on python 3)
  572. characters following specified filtering rules.
  573. When no filtering rules are specifed, any character can be produced.
  574. If ``min_codepoint`` or ``max_codepoint`` is specifed, then only
  575. characters having a codepoint in that range will be produced.
  576. If ``whitelist_categories`` is specified, then only characters from those
  577. Unicode categories will be produced. This is a further restriction,
  578. characters must also satisfy ``min_codepoint`` and ``max_codepoint``.
  579. If ``blacklist_categories`` is specified, then any character from those
  580. categories will not be produced. This is a further restriction,
  581. characters that match both ``whitelist_categories`` and
  582. ``blacklist_categories`` will not be produced.
  583. If ``whitelist_characters`` is specified, then any additional characters
  584. in that list will also be produced.
  585. If ``blacklist_characters`` is specified, then any characters in that list
  586. will be not be produced. Any overlap between ``whitelist_characters`` and
  587. ``blacklist_characters`` will raise an exception.
  588. Examples from this strategy shrink towards smaller codepoints.
  589. """
  590. if (
  591. min_codepoint is not None and max_codepoint is not None and
  592. min_codepoint > max_codepoint
  593. ):
  594. raise InvalidArgument(
  595. 'Cannot have min_codepoint=%d > max_codepoint=%d ' % (
  596. min_codepoint, max_codepoint
  597. )
  598. )
  599. if all((whitelist_characters is not None,
  600. min_codepoint is None,
  601. max_codepoint is None,
  602. whitelist_categories is None,
  603. blacklist_categories is None,
  604. )):
  605. raise InvalidArgument(
  606. 'Cannot have just whitelist_characters=%r alone, '
  607. 'it would have no effect. Perhaps you want sampled_from()' % (
  608. whitelist_characters,
  609. )
  610. )
  611. if (
  612. whitelist_characters is not None and
  613. blacklist_characters is not None and
  614. set(blacklist_characters).intersection(set(whitelist_characters))
  615. ):
  616. raise InvalidArgument(
  617. 'Characters %r are present in both whitelist_characters=%r, and '
  618. 'blacklist_characters=%r' % (
  619. set(blacklist_characters).intersection(
  620. set(whitelist_characters)
  621. ),
  622. whitelist_characters, blacklist_characters,
  623. )
  624. )
  625. from hypothesis.searchstrategy.strings import OneCharStringStrategy
  626. return OneCharStringStrategy(whitelist_categories=whitelist_categories,
  627. blacklist_categories=blacklist_categories,
  628. blacklist_characters=blacklist_characters,
  629. min_codepoint=min_codepoint,
  630. max_codepoint=max_codepoint,
  631. whitelist_characters=whitelist_characters)
  632. @cacheable
  633. @defines_strategy_with_reusable_values
  634. def text(
  635. alphabet=None,
  636. min_size=None, average_size=None, max_size=None
  637. ):
  638. """Generates values of a unicode text type (unicode on python 2, str on
  639. python 3) with values drawn from alphabet, which should be an iterable of
  640. length one strings or a strategy generating such. If it is None it will
  641. default to generating the full unicode range. If it is an empty collection
  642. this will only generate empty strings.
  643. min_size, max_size and average_size have the usual interpretations.
  644. Examples from this strategy shrink towards shorter strings, and with the
  645. characters in the text shrinking as per the alphabet strategy.
  646. """
  647. from hypothesis.searchstrategy.strings import StringStrategy
  648. if alphabet is None:
  649. char_strategy = characters(blacklist_categories=('Cs',))
  650. elif not alphabet:
  651. if (min_size or 0) > 0:
  652. raise InvalidArgument(
  653. 'Invalid min_size %r > 0 for empty alphabet' % (
  654. min_size,
  655. )
  656. )
  657. return just(u'')
  658. elif isinstance(alphabet, SearchStrategy):
  659. char_strategy = alphabet
  660. else:
  661. char_strategy = sampled_from(list(map(text_type, alphabet)))
  662. return StringStrategy(lists(
  663. char_strategy, average_size=average_size, min_size=min_size,
  664. max_size=max_size
  665. ))
  666. @cacheable
  667. @defines_strategy
  668. def from_regex(regex):
  669. """Generates strings that contain a match for the given regex (i.e. ones
  670. for which :func:`re.search` will return a non-None result).
  671. ``regex`` may be a pattern or :func:`compiled regex <python:re.compile>`.
  672. Both byte-strings and unicode strings are supported, and will generate
  673. examples of the same type.
  674. You can use regex flags such as :const:`re.IGNORECASE`, :const:`re.DOTALL`
  675. or :const:`re.UNICODE` to control generation. Flags can be passed either
  676. in compiled regex or inside the pattern with a ``(?iLmsux)`` group.
  677. Some regular expressions are only partly supported - the underlying
  678. strategy checks local matching and relies on filtering to resolve
  679. context-dependent expressions. Using too many of these constructs may
  680. cause health-check errors as too many examples are filtered out. This
  681. mainly includes (positive or negative) lookahead and lookbehind groups.
  682. If you want the generated string to match the whole regex you should use
  683. boundary markers. So e.g. ``r"\\A.\\Z"`` will return a single character
  684. string, while ``"."`` will return any string, and ``r"\\A.$"`` will return
  685. a single character optionally followed by a ``"\\n"``.
  686. Examples from this strategy shrink towards shorter strings and lower
  687. character values.
  688. """
  689. from hypothesis.searchstrategy.regex import regex_strategy
  690. return regex_strategy(regex)
  691. @cacheable
  692. @defines_strategy_with_reusable_values
  693. def binary(
  694. min_size=None, average_size=None, max_size=None
  695. ):
  696. """Generates the appropriate binary type (str in python 2, bytes in python
  697. 3).
  698. min_size, average_size and max_size have the usual interpretations.
  699. Examples from this strategy shrink towards smaller strings and lower byte
  700. values.
  701. """
  702. from hypothesis.searchstrategy.strings import BinaryStringStrategy, \
  703. FixedSizeBytes
  704. check_valid_sizes(min_size, average_size, max_size)
  705. if min_size == max_size is not None:
  706. return FixedSizeBytes(min_size)
  707. return BinaryStringStrategy(
  708. lists(
  709. integers(min_value=0, max_value=255),
  710. average_size=average_size, min_size=min_size, max_size=max_size
  711. )
  712. )
  713. @cacheable
  714. @defines_strategy
  715. def randoms():
  716. """Generates instances of Random (actually a Hypothesis specific
  717. RandomWithSeed class which displays what it was initially seeded with)
  718. Examples from this strategy shrink to seeds closer to zero.
  719. """
  720. from hypothesis.searchstrategy.misc import RandomStrategy
  721. return RandomStrategy(integers())
  722. class RandomSeeder(object):
  723. def __init__(self, seed):
  724. self.seed = seed
  725. def __repr__(self):
  726. return 'random.seed(%r)' % (self.seed,)
  727. @cacheable
  728. @defines_strategy
  729. def random_module():
  730. """If your code depends on the global random module then you need to use
  731. this.
  732. It will explicitly seed the random module at the start of your test
  733. so that tests are reproducible. The value it passes you is an opaque
  734. object whose only useful feature is that its repr displays the
  735. random seed. It is not itself a random number generator. If you want
  736. a random number generator you should use the randoms() strategy
  737. which will give you one.
  738. Examples from these strategy shrink to seeds closer to zero.
  739. """
  740. from hypothesis.control import cleanup
  741. import random
  742. class RandomModule(SearchStrategy):
  743. def do_draw(self, data):
  744. data.can_reproduce_example_from_repr = False
  745. seed = data.draw(integers())
  746. state = random.getstate()
  747. random.seed(seed)
  748. cleanup(lambda: random.setstate(state))
  749. return RandomSeeder(seed)
  750. return shared(RandomModule(), 'hypothesis.strategies.random_module()')
  751. @cacheable
  752. @defines_strategy
  753. def builds(target, *args, **kwargs):
  754. """Generates values by drawing from ``args`` and ``kwargs`` and passing
  755. them to ``target`` in the appropriate argument position.
  756. e.g. ``builds(target, integers(), flag=booleans())`` would draw an
  757. integer ``i`` and a boolean ``b`` and call ``target(i, flag=b)``.
  758. If ``target`` has type annotations, they will be used to infer a strategy
  759. for required arguments that were not passed to builds. You can also tell
  760. builds to infer a strategy for an optional argument by passing the special
  761. value :const:`hypothesis.infer` as a keyword argument to
  762. builds, instead of a strategy for that argument to ``target``.
  763. Examples from this strategy shrink by shrinking the argument values to
  764. the target.
  765. """
  766. if infer in args:
  767. # Avoid an implementation nightmare juggling tuples and worse things
  768. raise InvalidArgument('infer was passed as a positional argument to '
  769. 'builds(), but is only allowed as a keyword arg')
  770. hints = get_type_hints(target.__init__ if isclass(target) else target)
  771. for kw in [k for k, v in kwargs.items() if v is infer]:
  772. if kw not in hints:
  773. raise InvalidArgument(
  774. 'passed %s=infer for %s, but %s has no type annotation'
  775. % (kw, target.__name__, kw))
  776. kwargs[kw] = from_type(hints[kw])
  777. required = required_args(target, args, kwargs)
  778. for ms in set(hints) & (required or set()):
  779. kwargs[ms] = from_type(hints[ms])
  780. return tuples(tuples(*args), fixed_dictionaries(kwargs)).map(
  781. lambda value: target(*value[0], **value[1])
  782. )
  783. def delay_error(func):
  784. """A decorator to make exceptions lazy but success immediate.
  785. We want from_type to resolve to a strategy immediately if possible,
  786. for a useful repr and interactive use, but delay errors until a
  787. value would be drawn to localise them to a particular test.
  788. """
  789. @proxies(func)
  790. def inner(*args, **kwargs):
  791. try:
  792. return func(*args, **kwargs)
  793. except Exception as e:
  794. error = e
  795. def lazy_error():
  796. raise error
  797. return builds(lazy_error)
  798. return inner
  799. @cacheable
  800. @delay_error
  801. def from_type(thing):
  802. """Looks up the appropriate search strategy for the given type.
  803. ``from_type`` is used internally to fill in missing arguments to
  804. :func:`~hypothesis.strategies.builds` and can be used interactively
  805. to explore what strategies are available or to debug type resolution.
  806. You can use :func:`~hypothesis.strategies.register_type_strategy` to
  807. handle your custom types, or to globally redefine certain strategies -
  808. for example excluding NaN from floats, or use timezone-aware instead of
  809. naive time and datetime strategies.
  810. The resolution logic may be changed in a future version, but currently
  811. tries these four options:
  812. 1. If ``thing`` is in the default lookup mapping or user-registered lookup,
  813. return the corresponding strategy. The default lookup covers all types
  814. with Hypothesis strategies, including extras where possible.
  815. 2. If ``thing`` is from the :mod:`python:typing` module, return the
  816. corresponding strategy (special logic).
  817. 3. If ``thing`` has one or more subtypes in the merged lookup, return
  818. the union of the strategies for those types that are not subtypes of
  819. other elements in the lookup.
  820. 4. Finally, if ``thing`` has type annotations for all required arguments,
  821. it is resolved via :func:`~hypothesis.strategies.builds`.
  822. """
  823. from hypothesis.searchstrategy import types
  824. if not isinstance(thing, type):
  825. try:
  826. # At runtime, `typing.NewType` returns an identity function rather
  827. # than an actual type, but we can check that for a possible match
  828. # and then read the magic attribute to unwrap it.
  829. import typing
  830. if all([
  831. hasattr(thing, '__supertype__'), hasattr(typing, 'NewType'),
  832. isfunction(thing), getattr(thing, '__module__', 0) == 'typing'
  833. ]):
  834. return from_type(thing.__supertype__)
  835. # Under Python 3.6, Unions are not instances of `type` - but we
  836. # still want to resolve them!
  837. if getattr(thing, '__origin__', None) is typing.Union:
  838. args = sorted(thing.__args__, key=types.type_sorting_key)
  839. return one_of([from_type(t) for t in args])
  840. except ImportError: # pragma: no cover
  841. pass
  842. raise InvalidArgument('thing=%s must be a type' % (thing,))
  843. # Now that we know `thing` is a type, the first step is to check for an
  844. # explicitly registered strategy. This is the best (and hopefully most
  845. # common) way to resolve a type to a strategy. Note that the value in the
  846. # lookup may be a strategy or a function from type -> strategy; and we
  847. # convert empty results into an explicit error.
  848. if thing in types._global_type_lookup:
  849. strategy = types._global_type_lookup[thing]
  850. if not isinstance(strategy, SearchStrategy):
  851. strategy = strategy(thing)
  852. if strategy.is_empty:
  853. raise ResolutionFailed(
  854. 'Error: %r resolved to an empty strategy' % (thing,))
  855. return strategy
  856. # If there's no explicitly registered strategy, maybe a subtype of thing
  857. # is registered - if so, we can resolve it to the subclass strategy.
  858. # We'll start by checking if thing is from from the typing module,
  859. # because there are several special cases that don't play well with
  860. # subclass and instance checks.
  861. try:
  862. import typing
  863. if isinstance(thing, typing.TypingMeta):
  864. return types.from_typing_type(thing)
  865. except ImportError: # pragma: no cover
  866. pass
  867. # If it's not from the typing module, we get all registered types that are
  868. # a subclass of `thing` and are not themselves a subtype of any other such
  869. # type. For example, `Number -> integers() | floats()`, but bools() is
  870. # not included because bool is a subclass of int as well as Number.
  871. strategies = [
  872. v if isinstance(v, SearchStrategy) else v(thing)
  873. for k, v in types._global_type_lookup.items()
  874. if issubclass(k, thing) and
  875. sum(types.try_issubclass(k, T) for T in types._global_type_lookup) == 1
  876. ]
  877. empty = ', '.join(repr(s) for s in strategies if s.is_empty)
  878. if empty:
  879. raise ResolutionFailed(
  880. 'Could not resolve %s to a strategy; consider using '
  881. 'register_type_strategy' % empty)
  882. elif strategies:
  883. return one_of(strategies)
  884. # If we don't have a strategy registered for this type or any subtype, we
  885. # may be able to fall back on type annotations.
  886. # Types created via typing.NamedTuple use a custom attribute instead -
  887. # but we can still use builds(), if we work out the right kwargs.
  888. if issubclass(thing, tuple) and hasattr(thing, '_fields') \
  889. and hasattr(thing, '_field_types'):
  890. kwargs = {k: from_type(thing._field_types[k]) for k in thing._fields}
  891. return builds(thing, **kwargs)
  892. if issubclass(thing, enum.Enum):
  893. assert len(thing), repr(thing) + ' has no members to sample'
  894. return sampled_from(thing)
  895. # If the constructor has an annotation for every required argument,
  896. # we can (and do) use builds() without supplying additional arguments.
  897. required = required_args(thing)
  898. if not required or required.issubset(get_type_hints(thing.__init__)):
  899. return builds(thing)
  900. # We have utterly failed, and might as well say so now.
  901. raise ResolutionFailed('Could not resolve %r to a strategy; consider '
  902. 'using register_type_strategy' % (thing,))
  903. @cacheable
  904. @defines_strategy_with_reusable_values
  905. def fractions(min_value=None, max_value=None, max_denominator=None):
  906. """Returns a strategy which generates Fractions.
  907. If min_value is not None then all generated values are no less than
  908. min_value. If max_value is not None then all generated values are no
  909. greater than max_value. min_value and max_value may be anything accepted
  910. by the :class:`~fractions.Fraction` constructor.
  911. If max_denominator is not None then the denominator of any generated
  912. values is no greater than max_denominator. Note that max_denominator must
  913. be None or a positive integer.
  914. Examples from this strategy shrink towards smaller denominators, then
  915. closer to zero.
  916. """
  917. min_value = try_convert(Fraction, min_value, 'min_value')
  918. max_value = try_convert(Fraction, max_value, 'max_value')
  919. check_valid_interval(min_value, max_value, 'min_value', 'max_value')
  920. check_valid_integer(max_denominator)
  921. if max_denominator is not None:
  922. if max_denominator < 1:
  923. raise InvalidArgument(
  924. 'max_denominator=%r must be >= 1' % max_denominator)
  925. def fraction_bounds(value):
  926. """Find the best lower and upper approximation for value."""
  927. # Adapted from CPython's Fraction.limit_denominator here:
  928. # https://github.com/python/cpython/blob/3.6/Lib/fractions.py#L219
  929. if value is None or value.denominator <= max_denominator:
  930. return value, value
  931. p0, q0, p1, q1 = 0, 1, 1, 0
  932. n, d = value.numerator, value.denominator
  933. while True:
  934. a = n // d
  935. q2 = q0 + a * q1
  936. if q2 > max_denominator:
  937. break
  938. p0, q0, p1, q1 = p1, q1, p0 + a * p1, q2
  939. n, d = d, n - a * d
  940. k = (max_denominator - q0) // q1
  941. low, high = Fraction(p1, q1), Fraction(p0 + k * p1, q0 + k * q1)
  942. assert low < value < high
  943. return low, high
  944. # Take the high approximation for min_value and low for max_value
  945. bounds = (max_denominator, min_value, max_value)
  946. _, min_value = fraction_bounds(min_value)
  947. max_value, _ = fraction_bounds(max_value)
  948. if None not in (min_value, max_value) and min_value > max_value:
  949. raise InvalidArgument(
  950. 'There are no fractions with a denominator <= %r between '
  951. 'min_value=%r and max_value=%r' % bounds)
  952. if min_value is not None and min_value == max_value:
  953. return just(min_value)
  954. def dm_func(denom):
  955. """Take denom, construct numerator strategy, and build fraction."""
  956. # Four cases of algebra to get integer bounds and scale factor.
  957. min_num, max_num = None, None
  958. if max_value is None and min_value is None:
  959. pass
  960. elif min_value is None:
  961. max_num = denom * max_value.numerator
  962. denom *= max_value.denominator
  963. elif max_value is None:
  964. min_num = denom * min_value.numerator
  965. denom *= min_value.denominator
  966. else:
  967. low = min_value.numerator * max_value.denominator
  968. high = max_value.numerator * min_value.denominator
  969. scale = min_value.denominator * max_value.denominator
  970. # After calculating our integer bounds and scale factor, we remove
  971. # the gcd to avoid drawing more bytes for the example than needed.
  972. # Note that `div` can be at most equal to `scale`.
  973. div = gcd(scale, gcd(low, high))
  974. min_num = denom * low // div
  975. max_num = denom * high // div
  976. denom *= scale // div
  977. return builds(
  978. Fraction,
  979. integers(min_value=min_num, max_value=max_num),
  980. just(denom)
  981. )
  982. if max_denominator is None:
  983. return integers(min_value=1).flatmap(dm_func)
  984. return integers(1, max_denominator).flatmap(dm_func).map(
  985. lambda f: f.limit_denominator(max_denominator))
  986. @cacheable
  987. @defines_strategy_with_reusable_values
  988. def decimals(min_value=None, max_value=None,
  989. allow_nan=None, allow_infinity=None, places=None):
  990. """Generates instances of :class:`decimals.Decimal`, which may be:
  991. - A finite rational number, between ``min_value`` and ``max_value``.
  992. - Not a Number, if ``allow_nan`` is True. None means "allow NaN, unless
  993. ``min_value`` and ``max_value`` are not None".
  994. - Positive or negative infinity, if ``max_value`` and ``min_value``
  995. respectively are None, and ``allow_infinity`` is not False. None means
  996. "allow infinity, unless excluded by the min and max values".
  997. Note that where floats have one ``NaN`` value, Decimals have four: signed,
  998. and either *quiet* or *signalling*. See `the decimal module docs
  999. <https://docs.python.org/3/library/decimal.html#special-values>`_ for
  1000. more information on special values.
  1001. If ``places`` is not None, all finite values drawn from the strategy will
  1002. have that number of digits after the decimal place.
  1003. Examples from this strategy do not have a well defined shrink order but
  1004. try to maximize human readability when shrinking.
  1005. """
  1006. # Convert min_value and max_value to Decimal values, and validate args
  1007. check_valid_integer(places)
  1008. if places is not None and places < 0:
  1009. raise InvalidArgument('places=%r may not be negative' % places)
  1010. if min_value is not None:
  1011. min_value = try_convert(Decimal, min_value, 'min_value')
  1012. if min_value.is_infinite() and min_value < 0:
  1013. if not (allow_infinity or allow_infinity is None):
  1014. raise InvalidArgument('allow_infinity=%r, but min_value=%r'
  1015. % (allow_infinity, min_value))
  1016. min_value = None
  1017. elif not min_value.is_finite():
  1018. # This could be positive infinity, quiet NaN, or signalling NaN
  1019. raise InvalidArgument(u'Invalid min_value=%r' % min_value)
  1020. if max_value is not None:
  1021. max_value = try_convert(Decimal, max_value, 'max_value')
  1022. if max_value.is_infinite() and max_value > 0:
  1023. if not (allow_infinity or allow_infinity is None):
  1024. raise InvalidArgument('allow_infinity=%r, but max_value=%r'
  1025. % (allow_infinity, max_value))
  1026. max_value = None
  1027. elif not max_value.is_finite():
  1028. raise InvalidArgument(u'Invalid max_value=%r' % max_value)
  1029. check_valid_interval(min_value, max_value, 'min_value', 'max_value')
  1030. if allow_infinity and (None not in (min_value, max_value)):
  1031. raise InvalidArgument('Cannot allow infinity between finite bounds')
  1032. # Set up a strategy for finite decimals. Note that both floating and
  1033. # fixed-point decimals require careful handling to remain isolated from
  1034. # any external precision context - in short, we always work out the
  1035. # required precision for lossless operation and use context methods.
  1036. if places is not None:
  1037. # Fixed-point decimals are basically integers with a scale factor
  1038. def ctx(val):
  1039. """Return a context in which this value is lossless."""
  1040. precision = ceil(math.log10(abs(val) or 1)) + places + 1
  1041. return Context(prec=max([precision, 1]))
  1042. def int_to_decimal(val):
  1043. context = ctx(val)
  1044. return context.quantize(context.multiply(val, factor), factor)
  1045. factor = Decimal(10) ** -places
  1046. min_num, max_num = None, None
  1047. if min_value is not None:
  1048. min_num = ceil(ctx(min_value).divide(min_value, factor))
  1049. if max_value is not None:
  1050. max_num = floor(ctx(max_value).divide(max_value, factor))
  1051. if None not in (min_num, max_num) and min_num > max_num:
  1052. raise InvalidArgument(
  1053. 'There are no decimals with %d places between min_value=%r '
  1054. 'and max_value=%r ' % (places, min_value, max_value))
  1055. strat = integers(min_num, max_num).map(int_to_decimal)
  1056. else:
  1057. # Otherwise, they're like fractions featuring a power of ten
  1058. def fraction_to_decimal(val):
  1059. precision = ceil(math.log10(abs(val.numerator) or 1) +
  1060. math.log10(val.denominator)) + 1
  1061. return Context(prec=precision or 1).divide(
  1062. Decimal(val.numerator), val.denominator)
  1063. strat = fractions(min_value, max_value).map(fraction_to_decimal)
  1064. # Compose with sampled_from for infinities and NaNs as appropriate
  1065. special = []
  1066. if allow_nan or (allow_nan is None and (None in (min_value, max_value))):
  1067. special.extend(map(Decimal, ('NaN', '-NaN', 'sNaN', '-sNaN')))
  1068. if allow_infinity or (allow_infinity is max_value is None):
  1069. special.append(Decimal('Infinity'))
  1070. if allow_infinity or (allow_infinity is min_value is None):
  1071. special.append(Decimal('-Infinity'))
  1072. return strat | sampled_from(special)
  1073. def recursive(base, extend, max_leaves=100):
  1074. """base: A strategy to start from.
  1075. extend: A function which takes a strategy and returns a new strategy.
  1076. max_leaves: The maximum number of elements to be drawn from base on a given
  1077. run.
  1078. This returns a strategy ``S`` such that ``S = extend(base | S)``. That is,
  1079. values may be drawn from base, or from any strategy reachable by mixing
  1080. applications of | and extend.
  1081. An example may clarify: ``recursive(booleans(), lists)`` would return a
  1082. strategy that may return arbitrarily nested and mixed lists of booleans.
  1083. So e.g. ``False``, ``[True]``, ``[False, []]``, and ``[[[[True]]]]`` are
  1084. all valid values to be drawn from that strategy.
  1085. Examples from this strategy shrink by trying to reduce the amount of
  1086. recursion and by shrinking according to the shrinking behaviour of base
  1087. and the result of extend.
  1088. """
  1089. from hypothesis.searchstrategy.recursive import RecursiveStrategy
  1090. return RecursiveStrategy(base, extend, max_leaves)
  1091. @defines_strategy
  1092. def permutations(values):
  1093. """Return a strategy which returns permutations of the collection
  1094. ``values``.
  1095. Examples from this strategy shrink by trying to become closer to the
  1096. original order of values.
  1097. """
  1098. from hypothesis.internal.conjecture.utils import integer_range
  1099. values = list(values)
  1100. if not values:
  1101. return builds(list)
  1102. class PermutationStrategy(SearchStrategy):
  1103. def do_draw(self, data):
  1104. # Reversed Fisher-Yates shuffle. Reverse order so that it shrinks
  1105. # propertly: This way we prefer things that are lexicographically
  1106. # closer to the identity.
  1107. result = list(values)
  1108. for i in hrange(len(result)):
  1109. j = integer_range(data, i, len(result) - 1)
  1110. result[i], result[j] = result[j], result[i]
  1111. return result
  1112. return PermutationStrategy()
  1113. @defines_strategy_with_reusable_values
  1114. @renamed_arguments(
  1115. min_datetime='min_value',
  1116. max_datetime='max_value',
  1117. )
  1118. def datetimes(
  1119. min_value=dt.datetime.min, max_value=dt.datetime.max,
  1120. timezones=none(),
  1121. min_datetime=None, max_datetime=None,
  1122. ):
  1123. """A strategy for generating datetimes, which may be timezone-aware.
  1124. This strategy works by drawing a naive datetime between ``min_datetime``
  1125. and ``max_datetime``, which must both be naive (have no timezone).
  1126. ``timezones`` must be a strategy that generates tzinfo objects (or None,
  1127. which is valid for naive datetimes). A value drawn from this strategy
  1128. will be added to a naive datetime, and the resulting tz-aware datetime
  1129. returned.
  1130. .. note::
  1131. tz-aware datetimes from this strategy may be ambiguous or non-existent
  1132. due to daylight savings, leap seconds, timezone and calendar
  1133. adjustments, etc. This is intentional, as malformed timestamps are a
  1134. common source of bugs.
  1135. :py:func:`hypothesis.extra.timezones` requires the ``pytz`` package, but
  1136. provides all timezones in the Olsen database. If you also want to allow
  1137. naive datetimes, combine strategies like ``none() | timezones()``.
  1138. Alternatively, you can create a list of the timezones you wish to allow
  1139. (e.g. from the standard library, ``datetutil``, or ``pytz``) and use
  1140. :py:func:`sampled_from`. Ensure that simple values such as None or UTC
  1141. are at the beginning of the list for proper minimisation.
  1142. Examples from this strategy shrink towards midnight on January 1st 2000.
  1143. """
  1144. # Why must bounds be naive? In principle, we could also write a strategy
  1145. # that took aware bounds, but the API and validation is much harder.
  1146. # If you want to generate datetimes between two particular momements in
  1147. # time I suggest (a) just filtering out-of-bounds values; (b) if bounds
  1148. # are very close, draw a value and subtract it's UTC offset, handling
  1149. # overflows and nonexistent times; or (c) do something customised to
  1150. # handle datetimes in e.g. a four-microsecond span which is not
  1151. # representable in UTC. Handling (d), all of the above, leads to a much
  1152. # more complex API for all users and a useful feature for very few.
  1153. from hypothesis.searchstrategy.datetime import DatetimeStrategy
  1154. check_type(dt.datetime, min_value, 'min_value')
  1155. check_type(dt.datetime, max_value, 'max_value')
  1156. if min_value.tzinfo is not None:
  1157. raise InvalidArgument('min_value=%r must not have tzinfo'
  1158. % (min_value,))
  1159. if max_value.tzinfo is not None:
  1160. raise InvalidArgument('max_value=%r must not have tzinfo'
  1161. % (max_value,))
  1162. check_valid_interval(min_value, max_value,
  1163. 'min_value', 'max_value')
  1164. if not isinstance(timezones, SearchStrategy):
  1165. raise InvalidArgument(
  1166. 'timezones=%r must be a SearchStrategy that can provide tzinfo '
  1167. 'for datetimes (either None or dt.tzinfo objects)' % (timezones,))
  1168. return DatetimeStrategy(min_value, max_value, timezones)
  1169. @defines_strategy_with_reusable_values
  1170. @renamed_arguments(
  1171. min_date='min_value',
  1172. max_date='max_value',
  1173. )
  1174. def dates(
  1175. min_value=dt.date.min, max_value=dt.date.max,
  1176. min_date=None, max_date=None,
  1177. ):
  1178. """A strategy for dates between ``min_date`` and ``max_date``.
  1179. Examples from this strategy shrink towards January 1st 2000.
  1180. """
  1181. from hypothesis.searchstrategy.datetime import DateStrategy
  1182. check_type(dt.date, min_value, 'min_value')
  1183. check_type(dt.date, max_value, 'max_value')
  1184. check_valid_interval(min_value, max_value, 'min_value', 'max_value')
  1185. if min_value == max_value:
  1186. return just(min_value)
  1187. return DateStrategy(min_value, max_value)
  1188. @defines_strategy_with_reusable_values
  1189. @renamed_arguments(
  1190. min_time='min_value',
  1191. max_time='max_value',
  1192. )
  1193. def times(
  1194. min_value=dt.time.min, max_value=dt.time.max, timezones=none(),
  1195. min_time=None, max_time=None,
  1196. ):
  1197. """A strategy for times between ``min_time`` and ``max_time``.
  1198. The ``timezones`` argument is handled as for :py:func:`datetimes`.
  1199. Examples from this strategy shrink towards midnight, with the timezone
  1200. component shrinking as for the strategy that provided it.
  1201. """
  1202. check_type(dt.time, min_value, 'min_value')
  1203. check_type(dt.time, max_value, 'max_value')
  1204. if min_value.tzinfo is not None:
  1205. raise InvalidArgument('min_value=%r must not have tzinfo' % min_value)
  1206. if max_value.tzinfo is not None:
  1207. raise InvalidArgument('max_value=%r must not have tzinfo' % max_value)
  1208. check_valid_interval(min_value, max_value, 'min_value', 'max_value')
  1209. day = dt.date(2000, 1, 1)
  1210. return datetimes(min_value=dt.datetime.combine(day, min_value),
  1211. max_value=dt.datetime.combine(day, max_value),
  1212. timezones=timezones).map(lambda t: t.timetz())
  1213. @defines_strategy_with_reusable_values
  1214. @renamed_arguments(
  1215. min_delta='min_value',
  1216. max_delta='max_value',
  1217. )
  1218. def timedeltas(
  1219. min_value=dt.timedelta.min, max_value=dt.timedelta.max,
  1220. min_delta=None, max_delta=None
  1221. ):
  1222. """A strategy for timedeltas between ``min_value`` and ``max_value``.
  1223. Examples from this strategy shrink towards zero.
  1224. """
  1225. from hypothesis.searchstrategy.datetime import TimedeltaStrategy
  1226. check_type(dt.timedelta, min_value, 'min_value')
  1227. check_type(dt.timedelta, max_value, 'max_value')
  1228. check_valid_interval(min_value, max_value, 'min_value', 'max_value')
  1229. if min_value == max_value:
  1230. return just(min_value)
  1231. return TimedeltaStrategy(min_value=min_value, max_value=max_value)
  1232. @cacheable
  1233. def composite(f):
  1234. """Defines a strategy that is built out of potentially arbitrarily many
  1235. other strategies.
  1236. This is intended to be used as a decorator. See
  1237. :ref:`the full documentation for more details <composite-strategies>`
  1238. about how to use this function.
  1239. Examples from this strategy shrink by shrinking the output of each draw
  1240. call.
  1241. """
  1242. from hypothesis.internal.reflection import define_function_signature
  1243. argspec = getfullargspec(f)
  1244. if (
  1245. argspec.defaults is not None and
  1246. len(argspec.defaults) == len(argspec.args)
  1247. ):
  1248. raise InvalidArgument(
  1249. 'A default value for initial argument will never be used')
  1250. if len(argspec.args) == 0 and not argspec.varargs:
  1251. raise InvalidArgument(
  1252. 'Functions wrapped with composite must take at least one '
  1253. 'positional argument.'
  1254. )
  1255. annots = {k: v for k, v in argspec.annotations.items()
  1256. if k in (argspec.args + argspec.kwonlyargs + ['return'])}
  1257. new_argspec = argspec._replace(args=argspec.args[1:], annotations=annots)
  1258. @defines_strategy
  1259. @define_function_signature(f.__name__, f.__doc__, new_argspec)
  1260. def accept(*args, **kwargs):
  1261. class CompositeStrategy(SearchStrategy):
  1262. def do_draw(self, data):
  1263. first_draw = [True]
  1264. def draw(strategy):
  1265. first_draw[0] = False
  1266. return data.draw(strategy)
  1267. return f(draw, *args, **kwargs)
  1268. return CompositeStrategy()
  1269. accept.__module__ = f.__module__
  1270. return accept
  1271. def shared(base, key=None):
  1272. """Returns a strategy that draws a single shared value per run, drawn from
  1273. base. Any two shared instances with the same key will share the same value,
  1274. otherwise the identity of this strategy will be used. That is:
  1275. >>> s = integers() # or any other strategy
  1276. >>> x = shared(s)
  1277. >>> y = shared(s)
  1278. In the above x and y may draw different (or potentially the same) values.
  1279. In the following they will always draw the same:
  1280. >>> x = shared(s, key="hi")
  1281. >>> y = shared(s, key="hi")
  1282. Examples from this strategy shrink as per their base strategy.
  1283. """
  1284. from hypothesis.searchstrategy.shared import SharedStrategy
  1285. return SharedStrategy(base, key)
  1286. @defines_strategy
  1287. def choices():
  1288. """Strategy that generates a function that behaves like random.choice.
  1289. Will note choices made for reproducibility.
  1290. .. deprecated:: 3.15.0
  1291. Use :func:`data() <hypothesis.strategies.data>` with
  1292. :func:`sampled_from() <hypothesis.strategies.sampled_from>` instead.
  1293. Examples from this strategy shrink by making each choice function return
  1294. an earlier value in the sequence passed to it.
  1295. """
  1296. from hypothesis.control import note, current_build_context
  1297. from hypothesis.internal.conjecture.utils import choice, check_sample
  1298. note_deprecation(
  1299. 'choices() has been deprecated. Use the data() strategy instead and '
  1300. 'replace its usage with data.draw(sampled_from(elements))) calls.'
  1301. )
  1302. class Chooser(object):
  1303. def __init__(self, build_context, data):
  1304. self.build_context = build_context
  1305. self.data = data
  1306. self.choice_count = 0
  1307. def __call__(self, values):
  1308. if not values:
  1309. raise IndexError('Cannot choose from empty sequence')
  1310. result = choice(self.data, check_sample(values))
  1311. with self.build_context.local():
  1312. self.choice_count += 1
  1313. note('Choice #%d: %r' % (self.choice_count, result))
  1314. return result
  1315. def __repr__(self):
  1316. return 'choice'
  1317. class ChoiceStrategy(SearchStrategy):
  1318. supports_find = False
  1319. def do_draw(self, data):
  1320. data.can_reproduce_example_from_repr = False
  1321. return Chooser(current_build_context(), data)
  1322. return shared(
  1323. ChoiceStrategy(),
  1324. key='hypothesis.strategies.chooser.choice_function'
  1325. )
  1326. @cacheable
  1327. @defines_strategy_with_reusable_values
  1328. def uuids(version=None):
  1329. """Returns a strategy that generates :class:`UUIDs <uuid.UUID>`.
  1330. If the optional version argument is given, value is passed through
  1331. to :class:`~python:uuid.UUID` and only UUIDs of that version will
  1332. be generated.
  1333. All returned values from this will be unique, so e.g. if you do
  1334. ``lists(uuids())`` the resulting list will never contain duplicates.
  1335. Examples from this strategy don't have any meaningful shrink order.
  1336. """
  1337. from uuid import UUID
  1338. if version not in (None, 1, 2, 3, 4, 5):
  1339. raise InvalidArgument((
  1340. 'version=%r, but version must be in (None, 1, 2, 3, 4, 5) '
  1341. 'to pass to the uuid.UUID constructor.') % (version, )
  1342. )
  1343. return shared(randoms(), key='hypothesis.strategies.uuids.generator').map(
  1344. lambda r: UUID(version=version, int=r.getrandbits(128))
  1345. )
  1346. @defines_strategy_with_reusable_values
  1347. def runner(default=not_set):
  1348. """A strategy for getting "the current test runner", whatever that may be.
  1349. The exact meaning depends on the entry point, but it will usually be the
  1350. associated 'self' value for it.
  1351. If there is no current test runner and a default is provided, return
  1352. that default. If no default is provided, raises InvalidArgument.
  1353. Examples from this strategy do not shrink (because there is only one).
  1354. """
  1355. class RunnerStrategy(SearchStrategy):
  1356. def do_draw(self, data):
  1357. runner = getattr(data, 'hypothesis_runner', not_set)
  1358. if runner is not_set:
  1359. if default is not_set:
  1360. raise InvalidArgument(
  1361. 'Cannot use runner() strategy with no '
  1362. 'associated runner or explicit default.'
  1363. )
  1364. else:
  1365. return default
  1366. else:
  1367. return runner
  1368. return RunnerStrategy()
  1369. @cacheable
  1370. def data():
  1371. """This isn't really a normal strategy, but instead gives you an object
  1372. which can be used to draw data interactively from other strategies.
  1373. It can only be used within :func:`@given <hypothesis.given>`, not
  1374. :func:`find() <hypothesis.find>`. This is because the lifetime
  1375. of the object cannot outlast the test body.
  1376. See :ref:`the rest of the documentation <interactive-draw>` for more
  1377. complete information.
  1378. Examples from this strategy do not shrink (because there is only one),
  1379. but the result of calls to each draw() call shrink as they normally would.
  1380. """
  1381. from hypothesis.control import note
  1382. class DataObject(object):
  1383. def __init__(self, data):
  1384. self.count = 0
  1385. self.data = data
  1386. def __repr__(self):
  1387. return 'data(...)'
  1388. def draw(self, strategy, label=None):
  1389. result = self.data.draw(strategy)
  1390. self.count += 1
  1391. if label is not None:
  1392. note('Draw %d (%s): %r' % (self.count, label, result))
  1393. else:
  1394. note('Draw %d: %r' % (self.count, result))
  1395. return result
  1396. class DataStrategy(SearchStrategy):
  1397. supports_find = False
  1398. def do_draw(self, data):
  1399. data.can_reproduce_example_from_repr = False
  1400. if not hasattr(data, 'hypothesis_shared_data_strategy'):
  1401. data.hypothesis_shared_data_strategy = DataObject(data)
  1402. return data.hypothesis_shared_data_strategy
  1403. def __repr__(self):
  1404. return 'data()'
  1405. def map(self, f):
  1406. self.__not_a_first_class_strategy('map')
  1407. def filter(self, f):
  1408. self.__not_a_first_class_strategy('filter')
  1409. def flatmap(self, f):
  1410. self.__not_a_first_class_strategy('flatmap')
  1411. def example(self):
  1412. self.__not_a_first_class_strategy('example')
  1413. def __not_a_first_class_strategy(self, name):
  1414. raise InvalidArgument((
  1415. 'Cannot call %s on a DataStrategy. You should probably be '
  1416. "using @composite for whatever it is you're trying to do."
  1417. ) % (name,))
  1418. return DataStrategy()
  1419. def register_type_strategy(custom_type, strategy):
  1420. """Add an entry to the global type-to-strategy lookup.
  1421. This lookup is used in :func:`~hypothesis.strategies.builds` and
  1422. :func:`@given <hypothesis.given>`.
  1423. :func:`~hypothesis.strategies.builds` will be used automatically for
  1424. classes with type annotations on ``__init__`` , so you only need to
  1425. register a strategy if one or more arguments need to be more tightly
  1426. defined than their type-based default, or if you want to supply a strategy
  1427. for an argument with a default value.
  1428. ``strategy`` may be a search strategy, or a function that takes a type and
  1429. returns a strategy (useful for generic types).
  1430. """
  1431. from hypothesis.searchstrategy import types
  1432. if not isinstance(custom_type, type):
  1433. raise InvalidArgument('custom_type=%r must be a type')
  1434. elif not (isinstance(strategy, SearchStrategy) or callable(strategy)):
  1435. raise InvalidArgument(
  1436. 'strategy=%r must be a SearchStrategy, or a function that takes '
  1437. 'a generic type and returns a specific SearchStrategy')
  1438. elif isinstance(strategy, SearchStrategy) and strategy.is_empty:
  1439. raise InvalidArgument('strategy=%r must not be empty')
  1440. types._global_type_lookup[custom_type] = strategy
  1441. from_type.__clear_cache()
  1442. @cacheable
  1443. def deferred(definition):
  1444. """A deferred strategy allows you to write a strategy that references other
  1445. strategies that have not yet been defined. This allows for the easy
  1446. definition of recursive and mutually recursive strategies.
  1447. The definition argument should be a zero-argument function that returns a
  1448. strategy. It will be evaluated the first time the strategy is used to
  1449. produce an example.
  1450. Example usage:
  1451. >>> import hypothesis.strategies as st
  1452. >>> x = st.deferred(lambda: st.booleans() | st.tuples(x, x))
  1453. >>> x.example()
  1454. (((False, (True, True)), (False, True)), (True, True))
  1455. >>> x.example()
  1456. (True, True)
  1457. Mutual recursion also works fine:
  1458. >>> a = st.deferred(lambda: st.booleans() | b)
  1459. >>> b = st.deferred(lambda: st.tuples(a, a))
  1460. >>> a.example()
  1461. (True, (True, False))
  1462. >>> b.example()
  1463. (False, True)
  1464. Examples from this strategy shrink as they normally would from the strategy
  1465. returned by the definition.
  1466. """
  1467. from hypothesis.searchstrategy.deferred import DeferredStrategy
  1468. return DeferredStrategy(definition)
  1469. assert _strategies.issubset(set(__all__)), _strategies - set(__all__)