From b67dc79f841b98d5a997c9e63df877b5a6136a5e Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Tue, 1 Apr 2025 22:06:28 +0200 Subject: [PATCH 1/6] doc clarify definition of approx --- src/_pytest/python_api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index af078e25256..dc25329c83f 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -522,7 +522,8 @@ class ApproxDecimal(ApproxScalar): def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: """Assert that two numbers (or two ordered sequences of numbers) are equal to each other - within some tolerance. + within some tolerance. Note that unlike built-in equality, this function considers + booleans unequal to numeric zero or one. Due to the :doc:`python:tutorial/floatingpoint`, numbers that we would intuitively expect to be equal are not always so:: From 729d59fbd4ecddab04750e8c17deec979bfd3fad Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 2 Apr 2025 21:00:48 +0200 Subject: [PATCH 2/6] move the note further down and add an example --- src/_pytest/python_api.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index dc25329c83f..fb2853fb9cc 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -522,8 +522,7 @@ class ApproxDecimal(ApproxScalar): def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: """Assert that two numbers (or two ordered sequences of numbers) are equal to each other - within some tolerance. Note that unlike built-in equality, this function considers - booleans unequal to numeric zero or one. + within some tolerance. Due to the :doc:`python:tutorial/floatingpoint`, numbers that we would intuitively expect to be equal are not always so:: @@ -633,6 +632,12 @@ def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: >>> ["foo", 1.0000005] == approx([None,1]) False + Note that unlike built-in equality, this function considers + booleans unequal to numeric zero or one:: + + >>> 1 == approx(True) + False + If you're thinking about using ``approx``, then you might want to know how it compares to other good ways of comparing floating-point numbers. All of these algorithms are based on relative and absolute tolerances and should From b48581a3eabd62b2037f58dc5b28473ca15db50c Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 2 Apr 2025 21:48:29 +0200 Subject: [PATCH 3/6] added changelog/13218.doc.rst --- changelog/13218.doc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/13218.doc.rst diff --git a/changelog/13218.doc.rst b/changelog/13218.doc.rst new file mode 100644 index 00000000000..907a817e895 --- /dev/null +++ b/changelog/13218.doc.rst @@ -0,0 +1 @@ +Pointed out in the :func:`pytest.approx` documentation that it considers booleans unequal to numeric zero or one. From a5e83302c87b221e0978ddfff1bf44daccf826a5 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 3 Apr 2025 20:02:39 +0200 Subject: [PATCH 4/6] move the note and the example to a .. note:: section --- src/_pytest/python_api.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index fb2853fb9cc..d14ee8f797b 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -632,12 +632,6 @@ def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: >>> ["foo", 1.0000005] == approx([None,1]) False - Note that unlike built-in equality, this function considers - booleans unequal to numeric zero or one:: - - >>> 1 == approx(True) - False - If you're thinking about using ``approx``, then you might want to know how it compares to other good ways of comparing floating-point numbers. All of these algorithms are based on relative and absolute tolerances and should @@ -684,6 +678,12 @@ def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: from the `re_assert package `_. + Note that unlike built-in equality, this function considers + booleans unequal to numeric zero or one. For example:: + + >>> 1 == approx(True) + False + .. warning:: .. versionchanged:: 3.2 From 465a9b220be59b0eb8384f769ab83f912418a283 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 3 Apr 2025 16:13:08 -0300 Subject: [PATCH 5/6] Update src/_pytest/python_api.py --- src/_pytest/python_api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index d14ee8f797b..904d7dfa994 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -678,7 +678,10 @@ def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: from the `re_assert package `_. - Note that unlike built-in equality, this function considers + + .. note:: + + Unlike built-in equality, this function considers booleans unequal to numeric zero or one. For example:: >>> 1 == approx(True) From 8f0ad9821e02582f16c48282c0f44a260883a62b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Apr 2025 19:13:35 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/python_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 904d7dfa994..2afccbff078 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -680,7 +680,7 @@ def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: .. note:: - + Unlike built-in equality, this function considers booleans unequal to numeric zero or one. For example::