From d1f93f0d197414050516de7f0eb510ca52ff48d3 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Thu, 9 Apr 2026 11:39:33 +0100 Subject: [PATCH 1/2] Type `pytest.approx` --- changelog/14373.improvement.rst | 1 + src/_pytest/python_api.py | 7 ++++++- testing/python/approx.py | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 changelog/14373.improvement.rst diff --git a/changelog/14373.improvement.rst b/changelog/14373.improvement.rst new file mode 100644 index 00000000000..bb9895c72fd --- /dev/null +++ b/changelog/14373.improvement.rst @@ -0,0 +1 @@ +Added type annotations for ``pytest.approx``. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index bab70aa4a8c..9e2e1826a4f 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -558,7 +558,12 @@ def __repr__(self) -> str: return f"{self.expected} ± {tol_str}" -def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: +def approx( + expected: Any, + rel: float | Decimal | None = None, + abs: float | Decimal | None = None, + nan_ok: bool = False, +) -> ApproxBase: """Assert that two numbers (or two ordered sequences of numbers) are equal to each other within some tolerance. diff --git a/testing/python/approx.py b/testing/python/approx.py index 481df80565c..bfbb59fb61d 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -616,6 +616,8 @@ def test_decimal(self): assert a != approx(x, rel=Decimal("5e-7"), abs=0) assert approx(x, rel=Decimal("5e-6"), abs=0) == a assert approx(x, rel=Decimal("5e-7"), abs=0) != a + assert approx(x, rel=0, abs=Decimal("5e-3")) == a + assert approx(x, rel=0, abs=Decimal("5e-7")) != a def test_fraction(self): within_1e6 = [ From e4ec125a4644cd9ee6b3794daa290df1f3151e4c Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 10 Apr 2026 09:51:45 +0300 Subject: [PATCH 2/2] Update changelog/14373.improvement.rst --- changelog/14373.improvement.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/14373.improvement.rst b/changelog/14373.improvement.rst index bb9895c72fd..ea145f8b4a8 100644 --- a/changelog/14373.improvement.rst +++ b/changelog/14373.improvement.rst @@ -1 +1 @@ -Added type annotations for ``pytest.approx``. +Added type annotations for :func:`pytest.approx`.