Skip to content

Commit 35d471e

Browse files
author
cld
committed
Add stacklevel=2 to some warings.warn() calls
1 parent 89f38ae commit 35d471e

File tree

9 files changed

+24
-22
lines changed

9 files changed

+24
-22
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ def set_adjustable(self, adjustable, share=False):
13351335
"""
13361336
if adjustable == 'box-forced':
13371337
warnings.warn("The 'box-forced' keyword argument is deprecated"
1338-
" since 2.2.", cbook.mplDeprecation)
1338+
" since 2.2.", cbook.mplDeprecation, stacklevel=2)
13391339
if adjustable not in ('box', 'datalim', 'box-forced'):
13401340
raise ValueError("argument must be 'box', or 'datalim'")
13411341
if share:
@@ -1486,7 +1486,7 @@ def apply_aspect(self, position=None):
14861486
if aspect != "auto":
14871487
warnings.warn(
14881488
'aspect is not supported for Axes with xscale=%s, '
1489-
'yscale=%s' % (xscale, yscale))
1489+
'yscale=%s' % (xscale, yscale), stacklevel=2)
14901490
aspect = "auto"
14911491
else: # some custom projections have their own scales.
14921492
pass
@@ -2287,7 +2287,8 @@ def margins(self, *margins, x=None, y=None, tight=True):
22872287

22882288
if x is None and y is None:
22892289
if tight is not True:
2290-
warnings.warn('ignoring tight=%r in get mode' % (tight,))
2290+
warnings.warn('ignoring tight=%r in get mode' % (tight,),
2291+
stacklevel=2)
22912292
return self._xmargin, self._ymargin
22922293

22932294
if x is not None:
@@ -3117,21 +3118,21 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
31173118
warnings.warn(
31183119
('Attempting to set identical left==right results\n'
31193120
'in singular transformations; automatically expanding.\n'
3120-
'left=%s, right=%s') % (left, right))
3121+
'left=%s, right=%s') % (left, right), stacklevel=2)
31213122
left, right = mtransforms.nonsingular(left, right, increasing=False)
31223123

31233124
if self.get_xscale() == 'log':
31243125
if left <= 0:
31253126
warnings.warn(
31263127
'Attempted to set non-positive left xlim on a '
31273128
'log-scaled axis.\n'
3128-
'Invalid limit will be ignored.')
3129+
'Invalid limit will be ignored.', stacklevel=2)
31293130
left = old_left
31303131
if right <= 0:
31313132
warnings.warn(
31323133
'Attempted to set non-positive right xlim on a '
31333134
'log-scaled axis.\n'
3134-
'Invalid limit will be ignored.')
3135+
'Invalid limit will be ignored.', stacklevel=2)
31353136
right = old_right
31363137

31373138
left, right = self.xaxis.limit_range_for_scale(left, right)
@@ -3456,7 +3457,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
34563457
warnings.warn(
34573458
('Attempting to set identical bottom==top results\n'
34583459
'in singular transformations; automatically expanding.\n'
3459-
'bottom=%s, top=%s') % (bottom, top))
3460+
'bottom=%s, top=%s') % (bottom, top), stacklevel=2)
34603461

34613462
bottom, top = mtransforms.nonsingular(bottom, top, increasing=False)
34623463

@@ -3465,13 +3466,13 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
34653466
warnings.warn(
34663467
'Attempted to set non-positive bottom ylim on a '
34673468
'log-scaled axis.\n'
3468-
'Invalid limit will be ignored.')
3469+
'Invalid limit will be ignored.', stacklevel=2)
34693470
bottom = old_bottom
34703471
if top <= 0:
34713472
warnings.warn(
34723473
'Attempted to set non-positive top ylim on a '
34733474
'log-scaled axis.\n'
3474-
'Invalid limit will be ignored.')
3475+
'Invalid limit will be ignored.', stacklevel=2)
34753476
top = old_top
34763477
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
34773478

@@ -3879,7 +3880,7 @@ def _set_view_from_bbox(self, bbox, direction='in',
38793880
# should be len 3 or 4 but nothing else
38803881
warnings.warn(
38813882
"Warning in _set_view_from_bbox: bounding box is not a tuple "
3882-
"of length 3 or 4. Ignoring the view change.")
3883+
"of length 3 or 4. Ignoring the view change.", stacklevel=2)
38833884
return
38843885

38853886
# Just grab bounding box
@@ -4066,7 +4067,7 @@ def format_deltas(key, dx, dy):
40664067
result = (mtransforms.Bbox(newpoints)
40674068
.transformed(p.trans_inverse))
40684069
except OverflowError:
4069-
warnings.warn('Overflow while panning')
4070+
warnings.warn('Overflow while panning', stacklevel=2)
40704071
return
40714072
else:
40724073
return

lib/matplotlib/backends/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def do_nothing_show(*args, **kwargs):
7171
Your currently selected backend, '%s' does not support show().
7272
Please select a GUI backend in your matplotlibrc file ('%s')
7373
or with matplotlib.use()""" %
74-
(name, matplotlib.matplotlib_fname()))
74+
(name, matplotlib.matplotlib_fname()), stacklevel=2)
7575

7676
def do_nothing(*args, **kwargs):
7777
pass

lib/matplotlib/backends/backend_cairo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def _save(self, fo, fmt, **kwargs):
637637
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
638638
surface = cairo.SVGSurface(fo, width_in_points, height_in_points)
639639
else:
640-
warnings.warn("unknown format: %s" % fmt)
640+
warnings.warn("unknown format: %s" % fmt, stacklevel=2)
641641
return
642642

643643
# surface.set_dpi() can be used

lib/matplotlib/backends/backend_pdf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,10 +1567,11 @@ def is_date(x):
15671567
'Trapped': check_trapped}
15681568
for k in self.infoDict:
15691569
if k not in keywords:
1570-
warnings.warn('Unknown infodict keyword: %s' % k)
1570+
warnings.warn('Unknown infodict keyword: %s' % k, stacklevel=2)
15711571
else:
15721572
if not keywords[k](self.infoDict[k]):
1573-
warnings.warn('Bad value for infodict keyword %s' % k)
1573+
warnings.warn('Bad value for infodict keyword %s' % k,
1574+
stacklevel=2)
15741575

15751576
self.infoObject = self.reserveObject('info')
15761577
self.writeObject(self.infoObject, self.infoDict)

lib/matplotlib/backends/backend_pgf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def __init__(self, figure, fh, dummy=False):
420420
if not hasattr(fh, 'name') or not os.path.exists(fh.name):
421421
warnings.warn("streamed pgf-code does not support raster "
422422
"graphics, consider using the pgf-to-pdf option",
423-
UserWarning)
423+
UserWarning, stacklevel=2)
424424
self.__dict__["draw_image"] = lambda *args, **kwargs: None
425425

426426
def draw_markers(self, gc, marker_path, marker_trans, path, trans,

lib/matplotlib/backends/backend_webagg_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def handle_event(self, event):
241241

242242
def handle_unknown_event(self, event):
243243
warnings.warn('Unhandled message type {0}. {1}'.format(
244-
event['type'], event))
244+
event['type'], event), stacklevel=2)
245245

246246
def handle_ack(self, event):
247247
# Network latency tends to decrease if traffic is flowing

lib/matplotlib/backends/qt_editor/formlayout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def to_qcolor(color):
9292
try:
9393
rgba = mcolors.to_rgba(color)
9494
except ValueError:
95-
warnings.warn('Ignoring invalid color %r' % color)
95+
warnings.warn('Ignoring invalid color %r' % color, stacklevel=2)
9696
return qcolor # return invalid QColor
9797
qcolor.setRgbF(*rgba)
9898
return qcolor
@@ -259,7 +259,7 @@ def setup(self):
259259
elif not isinstance(selindex, int):
260260
warnings.warn(
261261
"index '%s' is invalid (label: %s, value: %s)" %
262-
(selindex, label, value))
262+
(selindex, label, value), stacklevel=2)
263263
selindex = 0
264264
field.setCurrentIndex(selindex)
265265
elif isinstance(value, bool):

lib/matplotlib/sphinxext/mathmpl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def latex2png(latex, filename, fontset='cm'):
5050
depth = mathtext_parser.to_png(filename, latex, dpi=100)
5151
except:
5252
warnings.warn("Could not render math expression %s" % latex,
53-
Warning)
53+
Warning, stacklevel=2)
5454
depth = 0
5555
rcParams['mathtext.fontset'] = orig_fontset
5656
sys.stdout.write("#")

lib/matplotlib/style/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _remove_blacklisted_style_params(d, warn=True):
4545
if warn:
4646
warnings.warn(
4747
"Style includes a parameter, '{0}', that is not related "
48-
"to style. Ignoring".format(key))
48+
"to style. Ignoring".format(key), stacklevel=2)
4949
else:
5050
o[key] = val
5151
return o
@@ -184,7 +184,7 @@ def read_style_directory(style_dir):
184184

185185
for w in warns:
186186
message = 'In %s: %s' % (path, w.message)
187-
warnings.warn(message)
187+
warnings.warn(message, stacklevel=2)
188188

189189
return styles
190190

0 commit comments

Comments
 (0)