python_api.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. import math
  2. import pprint
  3. from collections.abc import Sized
  4. from decimal import Decimal
  5. from numbers import Complex
  6. from types import TracebackType
  7. from typing import Any
  8. from typing import Callable
  9. from typing import cast
  10. from typing import Generic
  11. from typing import Iterable
  12. from typing import List
  13. from typing import Mapping
  14. from typing import Optional
  15. from typing import overload
  16. from typing import Pattern
  17. from typing import Sequence
  18. from typing import Tuple
  19. from typing import Type
  20. from typing import TYPE_CHECKING
  21. from typing import TypeVar
  22. from typing import Union
  23. if TYPE_CHECKING:
  24. from numpy import ndarray
  25. import _pytest._code
  26. from _pytest.compat import final
  27. from _pytest.compat import STRING_TYPES
  28. from _pytest.outcomes import fail
  29. def _non_numeric_type_error(value, at: Optional[str]) -> TypeError:
  30. at_str = f" at {at}" if at else ""
  31. return TypeError(
  32. "cannot make approximate comparisons to non-numeric values: {!r} {}".format(
  33. value, at_str
  34. )
  35. )
  36. def _compare_approx(
  37. full_object: object,
  38. message_data: Sequence[Tuple[str, str, str]],
  39. number_of_elements: int,
  40. different_ids: Sequence[object],
  41. max_abs_diff: float,
  42. max_rel_diff: float,
  43. ) -> List[str]:
  44. message_list = list(message_data)
  45. message_list.insert(0, ("Index", "Obtained", "Expected"))
  46. max_sizes = [0, 0, 0]
  47. for index, obtained, expected in message_list:
  48. max_sizes[0] = max(max_sizes[0], len(index))
  49. max_sizes[1] = max(max_sizes[1], len(obtained))
  50. max_sizes[2] = max(max_sizes[2], len(expected))
  51. explanation = [
  52. f"comparison failed. Mismatched elements: {len(different_ids)} / {number_of_elements}:",
  53. f"Max absolute difference: {max_abs_diff}",
  54. f"Max relative difference: {max_rel_diff}",
  55. ] + [
  56. f"{indexes:<{max_sizes[0]}} | {obtained:<{max_sizes[1]}} | {expected:<{max_sizes[2]}}"
  57. for indexes, obtained, expected in message_list
  58. ]
  59. return explanation
  60. # builtin pytest.approx helper
  61. class ApproxBase:
  62. """Provide shared utilities for making approximate comparisons between
  63. numbers or sequences of numbers."""
  64. # Tell numpy to use our `__eq__` operator instead of its.
  65. __array_ufunc__ = None
  66. __array_priority__ = 100
  67. def __init__(self, expected, rel=None, abs=None, nan_ok: bool = False) -> None:
  68. __tracebackhide__ = True
  69. self.expected = expected
  70. self.abs = abs
  71. self.rel = rel
  72. self.nan_ok = nan_ok
  73. self._check_type()
  74. def __repr__(self) -> str:
  75. raise NotImplementedError
  76. def _repr_compare(self, other_side: Any) -> List[str]:
  77. return [
  78. "comparison failed",
  79. f"Obtained: {other_side}",
  80. f"Expected: {self}",
  81. ]
  82. def __eq__(self, actual) -> bool:
  83. return all(
  84. a == self._approx_scalar(x) for a, x in self._yield_comparisons(actual)
  85. )
  86. def __bool__(self):
  87. __tracebackhide__ = True
  88. raise AssertionError(
  89. "approx() is not supported in a boolean context.\nDid you mean: `assert a == approx(b)`?"
  90. )
  91. # Ignore type because of https://github.com/python/mypy/issues/4266.
  92. __hash__ = None # type: ignore
  93. def __ne__(self, actual) -> bool:
  94. return not (actual == self)
  95. def _approx_scalar(self, x) -> "ApproxScalar":
  96. if isinstance(x, Decimal):
  97. return ApproxDecimal(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)
  98. return ApproxScalar(x, rel=self.rel, abs=self.abs, nan_ok=self.nan_ok)
  99. def _yield_comparisons(self, actual):
  100. """Yield all the pairs of numbers to be compared.
  101. This is used to implement the `__eq__` method.
  102. """
  103. raise NotImplementedError
  104. def _check_type(self) -> None:
  105. """Raise a TypeError if the expected value is not a valid type."""
  106. # This is only a concern if the expected value is a sequence. In every
  107. # other case, the approx() function ensures that the expected value has
  108. # a numeric type. For this reason, the default is to do nothing. The
  109. # classes that deal with sequences should reimplement this method to
  110. # raise if there are any non-numeric elements in the sequence.
  111. pass
  112. def _recursive_list_map(f, x):
  113. if isinstance(x, list):
  114. return [_recursive_list_map(f, xi) for xi in x]
  115. else:
  116. return f(x)
  117. class ApproxNumpy(ApproxBase):
  118. """Perform approximate comparisons where the expected value is numpy array."""
  119. def __repr__(self) -> str:
  120. list_scalars = _recursive_list_map(self._approx_scalar, self.expected.tolist())
  121. return f"approx({list_scalars!r})"
  122. def _repr_compare(self, other_side: "ndarray") -> List[str]:
  123. import itertools
  124. import math
  125. def get_value_from_nested_list(
  126. nested_list: List[Any], nd_index: Tuple[Any, ...]
  127. ) -> Any:
  128. """
  129. Helper function to get the value out of a nested list, given an n-dimensional index.
  130. This mimics numpy's indexing, but for raw nested python lists.
  131. """
  132. value: Any = nested_list
  133. for i in nd_index:
  134. value = value[i]
  135. return value
  136. np_array_shape = self.expected.shape
  137. approx_side_as_list = _recursive_list_map(
  138. self._approx_scalar, self.expected.tolist()
  139. )
  140. if np_array_shape != other_side.shape:
  141. return [
  142. "Impossible to compare arrays with different shapes.",
  143. f"Shapes: {np_array_shape} and {other_side.shape}",
  144. ]
  145. number_of_elements = self.expected.size
  146. max_abs_diff = -math.inf
  147. max_rel_diff = -math.inf
  148. different_ids = []
  149. for index in itertools.product(*(range(i) for i in np_array_shape)):
  150. approx_value = get_value_from_nested_list(approx_side_as_list, index)
  151. other_value = get_value_from_nested_list(other_side, index)
  152. if approx_value != other_value:
  153. abs_diff = abs(approx_value.expected - other_value)
  154. max_abs_diff = max(max_abs_diff, abs_diff)
  155. if other_value == 0.0:
  156. max_rel_diff = math.inf
  157. else:
  158. max_rel_diff = max(max_rel_diff, abs_diff / abs(other_value))
  159. different_ids.append(index)
  160. message_data = [
  161. (
  162. str(index),
  163. str(get_value_from_nested_list(other_side, index)),
  164. str(get_value_from_nested_list(approx_side_as_list, index)),
  165. )
  166. for index in different_ids
  167. ]
  168. return _compare_approx(
  169. self.expected,
  170. message_data,
  171. number_of_elements,
  172. different_ids,
  173. max_abs_diff,
  174. max_rel_diff,
  175. )
  176. def __eq__(self, actual) -> bool:
  177. import numpy as np
  178. # self.expected is supposed to always be an array here.
  179. if not np.isscalar(actual):
  180. try:
  181. actual = np.asarray(actual)
  182. except Exception as e:
  183. raise TypeError(f"cannot compare '{actual}' to numpy.ndarray") from e
  184. if not np.isscalar(actual) and actual.shape != self.expected.shape:
  185. return False
  186. return super().__eq__(actual)
  187. def _yield_comparisons(self, actual):
  188. import numpy as np
  189. # `actual` can either be a numpy array or a scalar, it is treated in
  190. # `__eq__` before being passed to `ApproxBase.__eq__`, which is the
  191. # only method that calls this one.
  192. if np.isscalar(actual):
  193. for i in np.ndindex(self.expected.shape):
  194. yield actual, self.expected[i].item()
  195. else:
  196. for i in np.ndindex(self.expected.shape):
  197. yield actual[i].item(), self.expected[i].item()
  198. class ApproxMapping(ApproxBase):
  199. """Perform approximate comparisons where the expected value is a mapping
  200. with numeric values (the keys can be anything)."""
  201. def __repr__(self) -> str:
  202. return "approx({!r})".format(
  203. {k: self._approx_scalar(v) for k, v in self.expected.items()}
  204. )
  205. def _repr_compare(self, other_side: Mapping[object, float]) -> List[str]:
  206. import math
  207. approx_side_as_map = {
  208. k: self._approx_scalar(v) for k, v in self.expected.items()
  209. }
  210. number_of_elements = len(approx_side_as_map)
  211. max_abs_diff = -math.inf
  212. max_rel_diff = -math.inf
  213. different_ids = []
  214. for (approx_key, approx_value), other_value in zip(
  215. approx_side_as_map.items(), other_side.values()
  216. ):
  217. if approx_value != other_value:
  218. max_abs_diff = max(
  219. max_abs_diff, abs(approx_value.expected - other_value)
  220. )
  221. max_rel_diff = max(
  222. max_rel_diff,
  223. abs((approx_value.expected - other_value) / approx_value.expected),
  224. )
  225. different_ids.append(approx_key)
  226. message_data = [
  227. (str(key), str(other_side[key]), str(approx_side_as_map[key]))
  228. for key in different_ids
  229. ]
  230. return _compare_approx(
  231. self.expected,
  232. message_data,
  233. number_of_elements,
  234. different_ids,
  235. max_abs_diff,
  236. max_rel_diff,
  237. )
  238. def __eq__(self, actual) -> bool:
  239. try:
  240. if set(actual.keys()) != set(self.expected.keys()):
  241. return False
  242. except AttributeError:
  243. return False
  244. return super().__eq__(actual)
  245. def _yield_comparisons(self, actual):
  246. for k in self.expected.keys():
  247. yield actual[k], self.expected[k]
  248. def _check_type(self) -> None:
  249. __tracebackhide__ = True
  250. for key, value in self.expected.items():
  251. if isinstance(value, type(self.expected)):
  252. msg = "pytest.approx() does not support nested dictionaries: key={!r} value={!r}\n full mapping={}"
  253. raise TypeError(msg.format(key, value, pprint.pformat(self.expected)))
  254. class ApproxSequencelike(ApproxBase):
  255. """Perform approximate comparisons where the expected value is a sequence of numbers."""
  256. def __repr__(self) -> str:
  257. seq_type = type(self.expected)
  258. if seq_type not in (tuple, list, set):
  259. seq_type = list
  260. return "approx({!r})".format(
  261. seq_type(self._approx_scalar(x) for x in self.expected)
  262. )
  263. def _repr_compare(self, other_side: Sequence[float]) -> List[str]:
  264. import math
  265. import numpy as np
  266. if len(self.expected) != len(other_side):
  267. return [
  268. "Impossible to compare lists with different sizes.",
  269. f"Lengths: {len(self.expected)} and {len(other_side)}",
  270. ]
  271. approx_side_as_map = _recursive_list_map(self._approx_scalar, self.expected)
  272. number_of_elements = len(approx_side_as_map)
  273. max_abs_diff = -math.inf
  274. max_rel_diff = -math.inf
  275. different_ids = []
  276. for i, (approx_value, other_value) in enumerate(
  277. zip(approx_side_as_map, other_side)
  278. ):
  279. if approx_value != other_value:
  280. abs_diff = abs(approx_value.expected - other_value)
  281. max_abs_diff = max(max_abs_diff, abs_diff)
  282. if other_value == 0.0:
  283. max_rel_diff = np.inf
  284. else:
  285. max_rel_diff = max(max_rel_diff, abs_diff / abs(other_value))
  286. different_ids.append(i)
  287. message_data = [
  288. (str(i), str(other_side[i]), str(approx_side_as_map[i]))
  289. for i in different_ids
  290. ]
  291. return _compare_approx(
  292. self.expected,
  293. message_data,
  294. number_of_elements,
  295. different_ids,
  296. max_abs_diff,
  297. max_rel_diff,
  298. )
  299. def __eq__(self, actual) -> bool:
  300. try:
  301. if len(actual) != len(self.expected):
  302. return False
  303. except TypeError:
  304. return False
  305. return super().__eq__(actual)
  306. def _yield_comparisons(self, actual):
  307. return zip(actual, self.expected)
  308. def _check_type(self) -> None:
  309. __tracebackhide__ = True
  310. for index, x in enumerate(self.expected):
  311. if isinstance(x, type(self.expected)):
  312. msg = "pytest.approx() does not support nested data structures: {!r} at index {}\n full sequence: {}"
  313. raise TypeError(msg.format(x, index, pprint.pformat(self.expected)))
  314. class ApproxScalar(ApproxBase):
  315. """Perform approximate comparisons where the expected value is a single number."""
  316. # Using Real should be better than this Union, but not possible yet:
  317. # https://github.com/python/typeshed/pull/3108
  318. DEFAULT_ABSOLUTE_TOLERANCE: Union[float, Decimal] = 1e-12
  319. DEFAULT_RELATIVE_TOLERANCE: Union[float, Decimal] = 1e-6
  320. def __repr__(self) -> str:
  321. """Return a string communicating both the expected value and the
  322. tolerance for the comparison being made.
  323. For example, ``1.0 ± 1e-6``, ``(3+4j) ± 5e-6 ∠ ±180°``.
  324. """
  325. # Don't show a tolerance for values that aren't compared using
  326. # tolerances, i.e. non-numerics and infinities. Need to call abs to
  327. # handle complex numbers, e.g. (inf + 1j).
  328. if (not isinstance(self.expected, (Complex, Decimal))) or math.isinf(
  329. abs(self.expected) # type: ignore[arg-type]
  330. ):
  331. return str(self.expected)
  332. # If a sensible tolerance can't be calculated, self.tolerance will
  333. # raise a ValueError. In this case, display '???'.
  334. try:
  335. vetted_tolerance = f"{self.tolerance:.1e}"
  336. if (
  337. isinstance(self.expected, Complex)
  338. and self.expected.imag
  339. and not math.isinf(self.tolerance)
  340. ):
  341. vetted_tolerance += " ∠ ±180°"
  342. except ValueError:
  343. vetted_tolerance = "???"
  344. return f"{self.expected} ± {vetted_tolerance}"
  345. def __eq__(self, actual) -> bool:
  346. """Return whether the given value is equal to the expected value
  347. within the pre-specified tolerance."""
  348. asarray = _as_numpy_array(actual)
  349. if asarray is not None:
  350. # Call ``__eq__()`` manually to prevent infinite-recursion with
  351. # numpy<1.13. See #3748.
  352. return all(self.__eq__(a) for a in asarray.flat)
  353. # Short-circuit exact equality.
  354. if actual == self.expected:
  355. return True
  356. # If either type is non-numeric, fall back to strict equality.
  357. # NB: we need Complex, rather than just Number, to ensure that __abs__,
  358. # __sub__, and __float__ are defined.
  359. if not (
  360. isinstance(self.expected, (Complex, Decimal))
  361. and isinstance(actual, (Complex, Decimal))
  362. ):
  363. return False
  364. # Allow the user to control whether NaNs are considered equal to each
  365. # other or not. The abs() calls are for compatibility with complex
  366. # numbers.
  367. if math.isnan(abs(self.expected)): # type: ignore[arg-type]
  368. return self.nan_ok and math.isnan(abs(actual)) # type: ignore[arg-type]
  369. # Infinity shouldn't be approximately equal to anything but itself, but
  370. # if there's a relative tolerance, it will be infinite and infinity
  371. # will seem approximately equal to everything. The equal-to-itself
  372. # case would have been short circuited above, so here we can just
  373. # return false if the expected value is infinite. The abs() call is
  374. # for compatibility with complex numbers.
  375. if math.isinf(abs(self.expected)): # type: ignore[arg-type]
  376. return False
  377. # Return true if the two numbers are within the tolerance.
  378. result: bool = abs(self.expected - actual) <= self.tolerance
  379. return result
  380. # Ignore type because of https://github.com/python/mypy/issues/4266.
  381. __hash__ = None # type: ignore
  382. @property
  383. def tolerance(self):
  384. """Return the tolerance for the comparison.
  385. This could be either an absolute tolerance or a relative tolerance,
  386. depending on what the user specified or which would be larger.
  387. """
  388. def set_default(x, default):
  389. return x if x is not None else default
  390. # Figure out what the absolute tolerance should be. ``self.abs`` is
  391. # either None or a value specified by the user.
  392. absolute_tolerance = set_default(self.abs, self.DEFAULT_ABSOLUTE_TOLERANCE)
  393. if absolute_tolerance < 0:
  394. raise ValueError(
  395. f"absolute tolerance can't be negative: {absolute_tolerance}"
  396. )
  397. if math.isnan(absolute_tolerance):
  398. raise ValueError("absolute tolerance can't be NaN.")
  399. # If the user specified an absolute tolerance but not a relative one,
  400. # just return the absolute tolerance.
  401. if self.rel is None:
  402. if self.abs is not None:
  403. return absolute_tolerance
  404. # Figure out what the relative tolerance should be. ``self.rel`` is
  405. # either None or a value specified by the user. This is done after
  406. # we've made sure the user didn't ask for an absolute tolerance only,
  407. # because we don't want to raise errors about the relative tolerance if
  408. # we aren't even going to use it.
  409. relative_tolerance = set_default(
  410. self.rel, self.DEFAULT_RELATIVE_TOLERANCE
  411. ) * abs(self.expected)
  412. if relative_tolerance < 0:
  413. raise ValueError(
  414. f"relative tolerance can't be negative: {relative_tolerance}"
  415. )
  416. if math.isnan(relative_tolerance):
  417. raise ValueError("relative tolerance can't be NaN.")
  418. # Return the larger of the relative and absolute tolerances.
  419. return max(relative_tolerance, absolute_tolerance)
  420. class ApproxDecimal(ApproxScalar):
  421. """Perform approximate comparisons where the expected value is a Decimal."""
  422. DEFAULT_ABSOLUTE_TOLERANCE = Decimal("1e-12")
  423. DEFAULT_RELATIVE_TOLERANCE = Decimal("1e-6")
  424. def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase:
  425. """Assert that two numbers (or two sets of numbers) are equal to each other
  426. within some tolerance.
  427. Due to the :std:doc:`tutorial/floatingpoint`, numbers that we
  428. would intuitively expect to be equal are not always so::
  429. >>> 0.1 + 0.2 == 0.3
  430. False
  431. This problem is commonly encountered when writing tests, e.g. when making
  432. sure that floating-point values are what you expect them to be. One way to
  433. deal with this problem is to assert that two floating-point numbers are
  434. equal to within some appropriate tolerance::
  435. >>> abs((0.1 + 0.2) - 0.3) < 1e-6
  436. True
  437. However, comparisons like this are tedious to write and difficult to
  438. understand. Furthermore, absolute comparisons like the one above are
  439. usually discouraged because there's no tolerance that works well for all
  440. situations. ``1e-6`` is good for numbers around ``1``, but too small for
  441. very big numbers and too big for very small ones. It's better to express
  442. the tolerance as a fraction of the expected value, but relative comparisons
  443. like that are even more difficult to write correctly and concisely.
  444. The ``approx`` class performs floating-point comparisons using a syntax
  445. that's as intuitive as possible::
  446. >>> from pytest import approx
  447. >>> 0.1 + 0.2 == approx(0.3)
  448. True
  449. The same syntax also works for sequences of numbers::
  450. >>> (0.1 + 0.2, 0.2 + 0.4) == approx((0.3, 0.6))
  451. True
  452. Dictionary *values*::
  453. >>> {'a': 0.1 + 0.2, 'b': 0.2 + 0.4} == approx({'a': 0.3, 'b': 0.6})
  454. True
  455. ``numpy`` arrays::
  456. >>> import numpy as np # doctest: +SKIP
  457. >>> np.array([0.1, 0.2]) + np.array([0.2, 0.4]) == approx(np.array([0.3, 0.6])) # doctest: +SKIP
  458. True
  459. And for a ``numpy`` array against a scalar::
  460. >>> import numpy as np # doctest: +SKIP
  461. >>> np.array([0.1, 0.2]) + np.array([0.2, 0.1]) == approx(0.3) # doctest: +SKIP
  462. True
  463. By default, ``approx`` considers numbers within a relative tolerance of
  464. ``1e-6`` (i.e. one part in a million) of its expected value to be equal.
  465. This treatment would lead to surprising results if the expected value was
  466. ``0.0``, because nothing but ``0.0`` itself is relatively close to ``0.0``.
  467. To handle this case less surprisingly, ``approx`` also considers numbers
  468. within an absolute tolerance of ``1e-12`` of its expected value to be
  469. equal. Infinity and NaN are special cases. Infinity is only considered
  470. equal to itself, regardless of the relative tolerance. NaN is not
  471. considered equal to anything by default, but you can make it be equal to
  472. itself by setting the ``nan_ok`` argument to True. (This is meant to
  473. facilitate comparing arrays that use NaN to mean "no data".)
  474. Both the relative and absolute tolerances can be changed by passing
  475. arguments to the ``approx`` constructor::
  476. >>> 1.0001 == approx(1)
  477. False
  478. >>> 1.0001 == approx(1, rel=1e-3)
  479. True
  480. >>> 1.0001 == approx(1, abs=1e-3)
  481. True
  482. If you specify ``abs`` but not ``rel``, the comparison will not consider
  483. the relative tolerance at all. In other words, two numbers that are within
  484. the default relative tolerance of ``1e-6`` will still be considered unequal
  485. if they exceed the specified absolute tolerance. If you specify both
  486. ``abs`` and ``rel``, the numbers will be considered equal if either
  487. tolerance is met::
  488. >>> 1 + 1e-8 == approx(1)
  489. True
  490. >>> 1 + 1e-8 == approx(1, abs=1e-12)
  491. False
  492. >>> 1 + 1e-8 == approx(1, rel=1e-6, abs=1e-12)
  493. True
  494. You can also use ``approx`` to compare nonnumeric types, or dicts and
  495. sequences containing nonnumeric types, in which case it falls back to
  496. strict equality. This can be useful for comparing dicts and sequences that
  497. can contain optional values::
  498. >>> {"required": 1.0000005, "optional": None} == approx({"required": 1, "optional": None})
  499. True
  500. >>> [None, 1.0000005] == approx([None,1])
  501. True
  502. >>> ["foo", 1.0000005] == approx([None,1])
  503. False
  504. If you're thinking about using ``approx``, then you might want to know how
  505. it compares to other good ways of comparing floating-point numbers. All of
  506. these algorithms are based on relative and absolute tolerances and should
  507. agree for the most part, but they do have meaningful differences:
  508. - ``math.isclose(a, b, rel_tol=1e-9, abs_tol=0.0)``: True if the relative
  509. tolerance is met w.r.t. either ``a`` or ``b`` or if the absolute
  510. tolerance is met. Because the relative tolerance is calculated w.r.t.
  511. both ``a`` and ``b``, this test is symmetric (i.e. neither ``a`` nor
  512. ``b`` is a "reference value"). You have to specify an absolute tolerance
  513. if you want to compare to ``0.0`` because there is no tolerance by
  514. default. More information: :py:func:`math.isclose`.
  515. - ``numpy.isclose(a, b, rtol=1e-5, atol=1e-8)``: True if the difference
  516. between ``a`` and ``b`` is less that the sum of the relative tolerance
  517. w.r.t. ``b`` and the absolute tolerance. Because the relative tolerance
  518. is only calculated w.r.t. ``b``, this test is asymmetric and you can
  519. think of ``b`` as the reference value. Support for comparing sequences
  520. is provided by :py:func:`numpy.allclose`. More information:
  521. :std:doc:`numpy:reference/generated/numpy.isclose`.
  522. - ``unittest.TestCase.assertAlmostEqual(a, b)``: True if ``a`` and ``b``
  523. are within an absolute tolerance of ``1e-7``. No relative tolerance is
  524. considered , so this function is not appropriate for very large or very
  525. small numbers. Also, it's only available in subclasses of ``unittest.TestCase``
  526. and it's ugly because it doesn't follow PEP8. More information:
  527. :py:meth:`unittest.TestCase.assertAlmostEqual`.
  528. - ``a == pytest.approx(b, rel=1e-6, abs=1e-12)``: True if the relative
  529. tolerance is met w.r.t. ``b`` or if the absolute tolerance is met.
  530. Because the relative tolerance is only calculated w.r.t. ``b``, this test
  531. is asymmetric and you can think of ``b`` as the reference value. In the
  532. special case that you explicitly specify an absolute tolerance but not a
  533. relative tolerance, only the absolute tolerance is considered.
  534. .. note::
  535. ``approx`` can handle numpy arrays, but we recommend the
  536. specialised test helpers in :std:doc:`numpy:reference/routines.testing`
  537. if you need support for comparisons, NaNs, or ULP-based tolerances.
  538. .. warning::
  539. .. versionchanged:: 3.2
  540. In order to avoid inconsistent behavior, :py:exc:`TypeError` is
  541. raised for ``>``, ``>=``, ``<`` and ``<=`` comparisons.
  542. The example below illustrates the problem::
  543. assert approx(0.1) > 0.1 + 1e-10 # calls approx(0.1).__gt__(0.1 + 1e-10)
  544. assert 0.1 + 1e-10 > approx(0.1) # calls approx(0.1).__lt__(0.1 + 1e-10)
  545. In the second example one expects ``approx(0.1).__le__(0.1 + 1e-10)``
  546. to be called. But instead, ``approx(0.1).__lt__(0.1 + 1e-10)`` is used to
  547. comparison. This is because the call hierarchy of rich comparisons
  548. follows a fixed behavior. More information: :py:meth:`object.__ge__`
  549. .. versionchanged:: 3.7.1
  550. ``approx`` raises ``TypeError`` when it encounters a dict value or
  551. sequence element of nonnumeric type.
  552. .. versionchanged:: 6.1.0
  553. ``approx`` falls back to strict equality for nonnumeric types instead
  554. of raising ``TypeError``.
  555. """
  556. # Delegate the comparison to a class that knows how to deal with the type
  557. # of the expected value (e.g. int, float, list, dict, numpy.array, etc).
  558. #
  559. # The primary responsibility of these classes is to implement ``__eq__()``
  560. # and ``__repr__()``. The former is used to actually check if some
  561. # "actual" value is equivalent to the given expected value within the
  562. # allowed tolerance. The latter is used to show the user the expected
  563. # value and tolerance, in the case that a test failed.
  564. #
  565. # The actual logic for making approximate comparisons can be found in
  566. # ApproxScalar, which is used to compare individual numbers. All of the
  567. # other Approx classes eventually delegate to this class. The ApproxBase
  568. # class provides some convenient methods and overloads, but isn't really
  569. # essential.
  570. __tracebackhide__ = True
  571. if isinstance(expected, Decimal):
  572. cls: Type[ApproxBase] = ApproxDecimal
  573. elif isinstance(expected, Mapping):
  574. cls = ApproxMapping
  575. elif _is_numpy_array(expected):
  576. expected = _as_numpy_array(expected)
  577. cls = ApproxNumpy
  578. elif (
  579. isinstance(expected, Iterable)
  580. and isinstance(expected, Sized)
  581. # Type ignored because the error is wrong -- not unreachable.
  582. and not isinstance(expected, STRING_TYPES) # type: ignore[unreachable]
  583. ):
  584. cls = ApproxSequencelike
  585. else:
  586. cls = ApproxScalar
  587. return cls(expected, rel, abs, nan_ok)
  588. def _is_numpy_array(obj: object) -> bool:
  589. """
  590. Return true if the given object is implicitly convertible to ndarray,
  591. and numpy is already imported.
  592. """
  593. return _as_numpy_array(obj) is not None
  594. def _as_numpy_array(obj: object) -> Optional["ndarray"]:
  595. """
  596. Return an ndarray if the given object is implicitly convertible to ndarray,
  597. and numpy is already imported, otherwise None.
  598. """
  599. import sys
  600. np: Any = sys.modules.get("numpy")
  601. if np is not None:
  602. # avoid infinite recursion on numpy scalars, which have __array__
  603. if np.isscalar(obj):
  604. return None
  605. elif isinstance(obj, np.ndarray):
  606. return obj
  607. elif hasattr(obj, "__array__") or hasattr("obj", "__array_interface__"):
  608. return np.asarray(obj)
  609. return None
  610. # builtin pytest.raises helper
  611. E = TypeVar("E", bound=BaseException)
  612. @overload
  613. def raises(
  614. expected_exception: Union[Type[E], Tuple[Type[E], ...]],
  615. *,
  616. match: Optional[Union[str, Pattern[str]]] = ...,
  617. ) -> "RaisesContext[E]":
  618. ...
  619. @overload
  620. def raises(
  621. expected_exception: Union[Type[E], Tuple[Type[E], ...]],
  622. func: Callable[..., Any],
  623. *args: Any,
  624. **kwargs: Any,
  625. ) -> _pytest._code.ExceptionInfo[E]:
  626. ...
  627. def raises(
  628. expected_exception: Union[Type[E], Tuple[Type[E], ...]], *args: Any, **kwargs: Any
  629. ) -> Union["RaisesContext[E]", _pytest._code.ExceptionInfo[E]]:
  630. r"""Assert that a code block/function call raises ``expected_exception``
  631. or raise a failure exception otherwise.
  632. :kwparam match:
  633. If specified, a string containing a regular expression,
  634. or a regular expression object, that is tested against the string
  635. representation of the exception using :py:func:`re.search`. To match a literal
  636. string that may contain :std:ref:`special characters <re-syntax>`, the pattern can
  637. first be escaped with :py:func:`re.escape`.
  638. (This is only used when :py:func:`pytest.raises` is used as a context manager,
  639. and passed through to the function otherwise.
  640. When using :py:func:`pytest.raises` as a function, you can use:
  641. ``pytest.raises(Exc, func, match="passed on").match("my pattern")``.)
  642. .. currentmodule:: _pytest._code
  643. Use ``pytest.raises`` as a context manager, which will capture the exception of the given
  644. type::
  645. >>> import pytest
  646. >>> with pytest.raises(ZeroDivisionError):
  647. ... 1/0
  648. If the code block does not raise the expected exception (``ZeroDivisionError`` in the example
  649. above), or no exception at all, the check will fail instead.
  650. You can also use the keyword argument ``match`` to assert that the
  651. exception matches a text or regex::
  652. >>> with pytest.raises(ValueError, match='must be 0 or None'):
  653. ... raise ValueError("value must be 0 or None")
  654. >>> with pytest.raises(ValueError, match=r'must be \d+$'):
  655. ... raise ValueError("value must be 42")
  656. The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the
  657. details of the captured exception::
  658. >>> with pytest.raises(ValueError) as exc_info:
  659. ... raise ValueError("value must be 42")
  660. >>> assert exc_info.type is ValueError
  661. >>> assert exc_info.value.args[0] == "value must be 42"
  662. .. note::
  663. When using ``pytest.raises`` as a context manager, it's worthwhile to
  664. note that normal context manager rules apply and that the exception
  665. raised *must* be the final line in the scope of the context manager.
  666. Lines of code after that, within the scope of the context manager will
  667. not be executed. For example::
  668. >>> value = 15
  669. >>> with pytest.raises(ValueError) as exc_info:
  670. ... if value > 10:
  671. ... raise ValueError("value must be <= 10")
  672. ... assert exc_info.type is ValueError # this will not execute
  673. Instead, the following approach must be taken (note the difference in
  674. scope)::
  675. >>> with pytest.raises(ValueError) as exc_info:
  676. ... if value > 10:
  677. ... raise ValueError("value must be <= 10")
  678. ...
  679. >>> assert exc_info.type is ValueError
  680. **Using with** ``pytest.mark.parametrize``
  681. When using :ref:`pytest.mark.parametrize ref`
  682. it is possible to parametrize tests such that
  683. some runs raise an exception and others do not.
  684. See :ref:`parametrizing_conditional_raising` for an example.
  685. **Legacy form**
  686. It is possible to specify a callable by passing a to-be-called lambda::
  687. >>> raises(ZeroDivisionError, lambda: 1/0)
  688. <ExceptionInfo ...>
  689. or you can specify an arbitrary callable with arguments::
  690. >>> def f(x): return 1/x
  691. ...
  692. >>> raises(ZeroDivisionError, f, 0)
  693. <ExceptionInfo ...>
  694. >>> raises(ZeroDivisionError, f, x=0)
  695. <ExceptionInfo ...>
  696. The form above is fully supported but discouraged for new code because the
  697. context manager form is regarded as more readable and less error-prone.
  698. .. note::
  699. Similar to caught exception objects in Python, explicitly clearing
  700. local references to returned ``ExceptionInfo`` objects can
  701. help the Python interpreter speed up its garbage collection.
  702. Clearing those references breaks a reference cycle
  703. (``ExceptionInfo`` --> caught exception --> frame stack raising
  704. the exception --> current frame stack --> local variables -->
  705. ``ExceptionInfo``) which makes Python keep all objects referenced
  706. from that cycle (including all local variables in the current
  707. frame) alive until the next cyclic garbage collection run.
  708. More detailed information can be found in the official Python
  709. documentation for :ref:`the try statement <python:try>`.
  710. """
  711. __tracebackhide__ = True
  712. if isinstance(expected_exception, type):
  713. excepted_exceptions: Tuple[Type[E], ...] = (expected_exception,)
  714. else:
  715. excepted_exceptions = expected_exception
  716. for exc in excepted_exceptions:
  717. if not isinstance(exc, type) or not issubclass(exc, BaseException):
  718. msg = "expected exception must be a BaseException type, not {}" # type: ignore[unreachable]
  719. not_a = exc.__name__ if isinstance(exc, type) else type(exc).__name__
  720. raise TypeError(msg.format(not_a))
  721. message = f"DID NOT RAISE {expected_exception}"
  722. if not args:
  723. match: Optional[Union[str, Pattern[str]]] = kwargs.pop("match", None)
  724. if kwargs:
  725. msg = "Unexpected keyword arguments passed to pytest.raises: "
  726. msg += ", ".join(sorted(kwargs))
  727. msg += "\nUse context-manager form instead?"
  728. raise TypeError(msg)
  729. return RaisesContext(expected_exception, message, match)
  730. else:
  731. func = args[0]
  732. if not callable(func):
  733. raise TypeError(f"{func!r} object (type: {type(func)}) must be callable")
  734. try:
  735. func(*args[1:], **kwargs)
  736. except expected_exception as e:
  737. # We just caught the exception - there is a traceback.
  738. assert e.__traceback__ is not None
  739. return _pytest._code.ExceptionInfo.from_exc_info(
  740. (type(e), e, e.__traceback__)
  741. )
  742. fail(message)
  743. # This doesn't work with mypy for now. Use fail.Exception instead.
  744. raises.Exception = fail.Exception # type: ignore
  745. @final
  746. class RaisesContext(Generic[E]):
  747. def __init__(
  748. self,
  749. expected_exception: Union[Type[E], Tuple[Type[E], ...]],
  750. message: str,
  751. match_expr: Optional[Union[str, Pattern[str]]] = None,
  752. ) -> None:
  753. self.expected_exception = expected_exception
  754. self.message = message
  755. self.match_expr = match_expr
  756. self.excinfo: Optional[_pytest._code.ExceptionInfo[E]] = None
  757. def __enter__(self) -> _pytest._code.ExceptionInfo[E]:
  758. self.excinfo = _pytest._code.ExceptionInfo.for_later()
  759. return self.excinfo
  760. def __exit__(
  761. self,
  762. exc_type: Optional[Type[BaseException]],
  763. exc_val: Optional[BaseException],
  764. exc_tb: Optional[TracebackType],
  765. ) -> bool:
  766. __tracebackhide__ = True
  767. if exc_type is None:
  768. fail(self.message)
  769. assert self.excinfo is not None
  770. if not issubclass(exc_type, self.expected_exception):
  771. return False
  772. # Cast to narrow the exception type now that it's verified.
  773. exc_info = cast(Tuple[Type[E], E, TracebackType], (exc_type, exc_val, exc_tb))
  774. self.excinfo.fill_unfilled(exc_info)
  775. if self.match_expr is not None:
  776. self.excinfo.match(self.match_expr)
  777. return True