From e29518f443fb8d4b21e2b69d615340d03f319d8b Mon Sep 17 00:00:00 2001
From: Joseph Weston <joseph.weston08@gmail.com>
Date: Fri, 17 Nov 2017 15:53:34 +0100
Subject: [PATCH] add stacklevel to all warnings

Now the line in the *calling* code will be displayed, which is
more useful for users.
---
 kwant/continuum/_common.py | 6 ++++--
 kwant/linalg/mumps.py      | 4 +++-
 kwant/plotter.py           | 4 ++--
 3 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/kwant/continuum/_common.py b/kwant/continuum/_common.py
index 9b14ef12..17a0e057 100644
--- a/kwant/continuum/_common.py
+++ b/kwant/continuum/_common.py
@@ -151,8 +151,10 @@ def sympify(expr, locals=None):
     # if ``expr`` is already a ``sympy`` object we may terminate a code path
     if isinstance(expr, tuple(sympy_classes)):
         if locals:
-            warnings.warn('Input expression is already SymPy object: ' +
-                          '"locals" will not be used.', RuntimeWarning)
+            warnings.warn('Input expression is already SymPy object: '
+                          '"locals" will not be used.',
+                          RuntimeWarning,
+                          stacklevel=2)
         return expr
 
     # if ``expr`` is not a "sympy" then we proceed with sympifying process
diff --git a/kwant/linalg/mumps.py b/kwant/linalg/mumps.py
index 397b4cc9..465ef1aa 100644
--- a/kwant/linalg/mumps.py
+++ b/kwant/linalg/mumps.py
@@ -299,7 +299,9 @@ class MUMPSContext:
         if reuse_analysis:
             if self.mumps_instance is None:
                 warnings.warn("Missing analysis although reuse_analysis=True. "
-                              "New analysis is performed.", RuntimeWarning)
+                              "New analysis is performed.",
+                              RuntimeWarning,
+                              stacklevel=2)
                 self.analyze(a, ordering=ordering, overwrite_a=overwrite_a)
             else:
                 dtype, row, col, data = _make_assembled_from_coo(a,
diff --git a/kwant/plotter.py b/kwant/plotter.py
index 4c17c8bb..d164dfcf 100644
--- a/kwant/plotter.py
+++ b/kwant/plotter.py
@@ -67,7 +67,7 @@ def matplotlib_chores():
         warnings.warn("Matplotlib 1.4.0 has a bug that makes 3D plotting "
                       "unusable (2D plotting is not affected). Please "
                       "consider using a different version of matplotlib.",
-                      RuntimeWarning)
+                      RuntimeWarning, stacklevel=2)
 
     pre_1_4_matplotlib = [int(x) for x in ver.split('.')[:2]] < [1, 4]
 
@@ -1457,7 +1457,7 @@ def mask_interpolate(coords, values, a=None, method='nearest', oversampling=3):
     if min_dist < 1e-6 * np.linalg.norm(cmax - cmin):
         warnings.warn("Some sites have nearly coinciding positions, "
                       "interpolation may be confusing.",
-                      RuntimeWarning)
+                      RuntimeWarning, stacklevel=2)
 
     if a is None:
         a = min_dist
-- 
GitLab