Skip to content

Commit f78ff0e

Browse files
authored
Merge pull request #11225 from cldssty/master
Add stacklevel=2 to some more warnings.warn() calls
2 parents 4e57529 + 4ed006e commit f78ff0e

File tree

9 files changed

+24
-22
lines changed

9 files changed

+24
-22
lines changed

lib/matplotlib/axes/_base.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ def set_adjustable(self, adjustable, share=False):
13251325
"""
13261326
if adjustable == 'box-forced':
13271327
warnings.warn("The 'box-forced' keyword argument is deprecated"
1328-
" since 2.2.", cbook.mplDeprecation)
1328+
" since 2.2.", cbook.mplDeprecation, stacklevel=2)
13291329
if adjustable not in ('box', 'datalim', 'box-forced'):
13301330
raise ValueError("argument must be 'box', or 'datalim'")
13311331
if share:
@@ -1476,7 +1476,7 @@ def apply_aspect(self, position=None):
14761476
if aspect != "auto":
14771477
warnings.warn(
14781478
'aspect is not supported for Axes with xscale=%s, '
1479-
'yscale=%s' % (xscale, yscale))
1479+
'yscale=%s' % (xscale, yscale), stacklevel=2)
14801480
aspect = "auto"
14811481
else: # some custom projections have their own scales.
14821482
pass
@@ -2267,7 +2267,8 @@ def margins(self, *margins, x=None, y=None, tight=True):
22672267

22682268
if x is None and y is None:
22692269
if tight is not True:
2270-
warnings.warn('ignoring tight=%r in get mode' % (tight,))
2270+
warnings.warn('ignoring tight=%r in get mode' % (tight,),
2271+
stacklevel=2)
22712272
return self._xmargin, self._ymargin
22722273

22732274
if x is not None:
@@ -3091,21 +3092,21 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
30913092
warnings.warn(
30923093
('Attempting to set identical left==right results\n'
30933094
'in singular transformations; automatically expanding.\n'
3094-
'left=%s, right=%s') % (left, right))
3095+
'left=%s, right=%s') % (left, right), stacklevel=2)
30953096
left, right = mtransforms.nonsingular(left, right, increasing=False)
30963097

30973098
if self.get_xscale() == 'log':
30983099
if left <= 0:
30993100
warnings.warn(
31003101
'Attempted to set non-positive left xlim on a '
31013102
'log-scaled axis.\n'
3102-
'Invalid limit will be ignored.')
3103+
'Invalid limit will be ignored.', stacklevel=2)
31033104
left = old_left
31043105
if right <= 0:
31053106
warnings.warn(
31063107
'Attempted to set non-positive right xlim on a '
31073108
'log-scaled axis.\n'
3108-
'Invalid limit will be ignored.')
3109+
'Invalid limit will be ignored.', stacklevel=2)
31093110
right = old_right
31103111

31113112
left, right = self.xaxis.limit_range_for_scale(left, right)
@@ -3428,7 +3429,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
34283429
warnings.warn(
34293430
('Attempting to set identical bottom==top results\n'
34303431
'in singular transformations; automatically expanding.\n'
3431-
'bottom=%s, top=%s') % (bottom, top))
3432+
'bottom=%s, top=%s') % (bottom, top), stacklevel=2)
34323433

34333434
bottom, top = mtransforms.nonsingular(bottom, top, increasing=False)
34343435

@@ -3437,13 +3438,13 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
34373438
warnings.warn(
34383439
'Attempted to set non-positive bottom ylim on a '
34393440
'log-scaled axis.\n'
3440-
'Invalid limit will be ignored.')
3441+
'Invalid limit will be ignored.', stacklevel=2)
34413442
bottom = old_bottom
34423443
if top <= 0:
34433444
warnings.warn(
34443445
'Attempted to set non-positive top ylim on a '
34453446
'log-scaled axis.\n'
3446-
'Invalid limit will be ignored.')
3447+
'Invalid limit will be ignored.', stacklevel=2)
34473448
top = old_top
34483449
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
34493450

@@ -3847,7 +3848,7 @@ def _set_view_from_bbox(self, bbox, direction='in',
38473848
# should be len 3 or 4 but nothing else
38483849
warnings.warn(
38493850
"Warning in _set_view_from_bbox: bounding box is not a tuple "
3850-
"of length 3 or 4. Ignoring the view change.")
3851+
"of length 3 or 4. Ignoring the view change.", stacklevel=2)
38513852
return
38523853

38533854
# Just grab bounding box
@@ -4034,7 +4035,7 @@ def format_deltas(key, dx, dy):
40344035
result = (mtransforms.Bbox(newpoints)
40354036
.transformed(p.trans_inverse))
40364037
except OverflowError:
4037-
warnings.warn('Overflow while panning')
4038+
warnings.warn('Overflow while panning', stacklevel=2)
40384039
return
40394040
else:
40404041
return

lib/matplotlib/backends/__init__.py

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def _save(self, fo, fmt, **kwargs):
633633
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
634634
surface = cairo.SVGSurface(fo, width_in_points, height_in_points)
635635
else:
636-
warnings.warn("unknown format: %s" % fmt)
636+
warnings.warn("unknown format: %s" % fmt, stacklevel=2)
637637
return
638638

639639
# surface.set_dpi() can be used

lib/matplotlib/backends/backend_pdf.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1559,10 +1559,11 @@ def is_date(x):
15591559
'Trapped': check_trapped}
15601560
for k in self.infoDict:
15611561
if k not in keywords:
1562-
warnings.warn('Unknown infodict keyword: %s' % k)
1562+
warnings.warn('Unknown infodict keyword: %s' % k, stacklevel=2)
15631563
else:
15641564
if not keywords[k](self.infoDict[k]):
1565-
warnings.warn('Bad value for infodict keyword %s' % k)
1565+
warnings.warn('Bad value for infodict keyword %s' % k,
1566+
stacklevel=2)
15661567

15671568
self.infoObject = self.reserveObject('info')
15681569
self.writeObject(self.infoObject, self.infoDict)

lib/matplotlib/backends/backend_pgf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def __init__(self, figure, fh, dummy=False):
403403
if not hasattr(fh, 'name') or not os.path.exists(fh.name):
404404
warnings.warn("streamed pgf-code does not support raster "
405405
"graphics, consider using the pgf-to-pdf option",
406-
UserWarning)
406+
UserWarning, stacklevel=2)
407407
self.__dict__["draw_image"] = lambda *args, **kwargs: None
408408

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

lib/matplotlib/backends/backend_webagg_core.py

+1-1
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

+2-2
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

+1-1
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

+2-2
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=3)
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)