From 33c0b06fdf989d8df63dc68ba5336ecad2eadd30 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 24 Nov 2016 15:27:38 -0200 Subject: [PATCH] Fix error in approx's repr with complex numbers Fix #2082 --- CHANGELOG.rst | 6 ++++++ _pytest/python.py | 3 +++ testing/python/approx.py | 1 + 3 files changed, 10 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index eeb41a648..f97ff3787 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -7,6 +7,10 @@ subtle bugs (`#2078`_). Thanks `@nicoddemus`_ for the PR. +* Fix error message using ``approx`` with complex numbers (`#2082`_). + Thanks `@adler-j`_ for the report and `@nicoddemus`_ for the PR. + +* * Cope gracefully with a .pyc file with no matching .py file (`#2038`_). Thanks `@nedbat`_. @@ -15,10 +19,12 @@ * +.. _@adler-j: https://github.com/adler-j .. _@nedbat: https://github.com/nedbat .. _#2038: https://github.com/pytest-dev/pytest/issues/2038 .. _#2078: https://github.com/pytest-dev/pytest/issues/2078 +.. _#2082: https://github.com/pytest-dev/pytest/issues/2082 3.0.4 diff --git a/_pytest/python.py b/_pytest/python.py index 88c65b1ce..76cf4bd64 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1419,6 +1419,9 @@ class ApproxNonIterable(object): self.rel = rel def __repr__(self): + if isinstance(self.expected, complex): + return str(self.expected) + # Infinities aren't compared using tolerances, so don't show a # tolerance. if math.isinf(self.expected): diff --git a/testing/python/approx.py b/testing/python/approx.py index 2720c573f..d6bb1f9cf 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -28,6 +28,7 @@ class TestApprox: print(approx(inf)) print(approx(1.0, rel=nan)) print(approx(1.0, rel=inf)) + print(approx(1.0j, rel=inf)) def test_operator_overloading(self): assert 1 == approx(1, rel=1e-6, abs=1e-12)