From 38a1dbee4549a7b95f0d7e8e250606b12af79dee Mon Sep 17 00:00:00 2001
From: Joseph Weston <joseph@weston.cloud>
Date: Mon, 15 Jul 2019 11:47:07 +0200
Subject: [PATCH] normalise usage of 'pytest.raises'

In pytest v5 the behaviour of str(exc_info) changed. We now
use str(exc_info.value) as the canonical way of obtaining
the exception message.
---
 kwant/tests/test_builder.py  |  8 ++++----
 kwant/tests/test_operator.py | 24 ++++++++++++------------
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/kwant/tests/test_builder.py b/kwant/tests/test_builder.py
index bd02f680..84e3ef02 100644
--- a/kwant/tests/test_builder.py
+++ b/kwant/tests/test_builder.py
@@ -535,17 +535,17 @@ def test_hamiltonian_evaluation():
     def test_raising(fsyst, hop):
         a, b = hop
         # exceptions are converted to kwant.UserCodeError and we add our message
-        with raises(kwant.UserCodeError) as ctx:
+        with raises(kwant.UserCodeError) as exc_info:
             fsyst.hamiltonian(a, a)
         msg = 'Error occurred in user-supplied value function "onsite_raises"'
-        assert msg in ctx.exconly()
+        assert msg in str(exc_info.value)
 
         for hop in [(a, b), (b, a)]:
-            with raises(kwant.UserCodeError) as ctx:
+            with raises(kwant.UserCodeError) as exc_info:
                 fsyst.hamiltonian(*hop)
             msg = ('Error occurred in user-supplied '
                    'value function "hopping_raises"')
-            assert msg in ctx.exconly()
+            assert msg in str(exc_info.value)
 
     # test with finite system
     new_hop = (fam(-1, 0), fam(0, 0))
diff --git a/kwant/tests/test_operator.py b/kwant/tests/test_operator.py
index 6eb0e631..f6e5271b 100644
--- a/kwant/tests/test_operator.py
+++ b/kwant/tests/test_operator.py
@@ -425,14 +425,14 @@ def test_arg_passing(A):
     # test missing params
     op = A(fsyst, onsite=lambda x, a, b: 1)
     params = dict(a=1)
-    with raises(TypeError) as exc:
+    with raises(TypeError):
         op(wf, params=params)
-    with raises(TypeError) as exc:
+    with raises(TypeError):
         op.act(wf, params=params)
-    with raises(TypeError) as exc:
+    with raises(TypeError):
         op.bind(params=params)
     if hasattr(op, 'tocoo'):
-        with raises(TypeError) as exc:
+        with raises(TypeError):
             op.tocoo(params=params)
 
 
@@ -445,19 +445,19 @@ def test_arg_passing(A):
     if has_tocoo:
         tocoo_should_be = op.tocoo(args=canonical_args).toarray()
 
-    with raises(TypeError) as exc:
+    with raises(TypeError) as exc_info:
         op(wf, args=canonical_args, params=params)
-    assert 'mutually exclusive' in str(exc)
-    with raises(TypeError) as exc:
+    assert 'mutually exclusive' in str(exc_info.value)
+    with raises(TypeError) as exc_info:
         op.act(wf, args=canonical_args, params=params)
-    assert 'mutually exclusive' in str(exc)
-    with raises(TypeError) as exc:
+    assert 'mutually exclusive' in str(exc_info.value)
+    with raises(TypeError) as exc_info:
         op.bind(args=canonical_args, params=params)
-    assert 'mutually exclusive' in str(exc)
+    assert 'mutually exclusive' in str(exc_info.value)
     if has_tocoo:
-        with raises(TypeError) as exc:
+        with raises(TypeError) as exc_info:
             op.tocoo(args=canonical_args, params=params)
-        assert 'mutually exclusive' in str(exc)
+        assert 'mutually exclusive' in str(exc_info.value)
 
     np.testing.assert_array_equal(
         call_should_be, op(wf, params=params))
-- 
GitLab