Skip to content

Commit 6c0aa84

Browse files
anntzertimhoffm
authored andcommitted
Be a bit more stringent on invalid inputs. (#12627)
and give the user fewer ways to shoot themselves in the foot. The change in backend_cairo is on private API; _save is only ever called by the various print_foo methods, with a valid fmt.
1 parent e1baba4 commit 6c0aa84

File tree

7 files changed

+50
-42
lines changed

7 files changed

+50
-42
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Invalid inputs
2+
``````````````
3+
4+
Passing invalid locations to `legend` and `table` used to fallback on a default
5+
location. This behavior is deprecated and will throw an exception in a future
6+
version.
7+
8+
`offsetbox.AnchoredText` is unable to handle the ``horizontalalignment`` or
9+
``verticalalignment`` kwargs, and used to ignore them with a warning. This
10+
behavior is deprecated and will throw an exception in a future version.
11+
12+
Passing steps less than 1 or greater than 10 to `MaxNLocator` used to result in
13+
undefined behavior. It now throws a ValueError.

lib/matplotlib/backends/backend_cairo.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import copy
1010
import gzip
11-
import warnings
1211

1312
import numpy as np
1413

@@ -602,8 +601,7 @@ def _save(self, fo, fmt, **kwargs):
602601
fo = gzip.GzipFile(None, 'wb', fileobj=fo)
603602
surface = cairo.SVGSurface(fo, width_in_points, height_in_points)
604603
else:
605-
warnings.warn("unknown format: %s" % fmt, stacklevel=2)
606-
return
604+
raise ValueError("Unknown format: {!r}".format(fmt))
607605

608606
# surface.set_dpi() can be used
609607
renderer = RendererCairo(self.figure.dpi)

lib/matplotlib/cbook/deprecation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _generate_deprecation_message(
2424
obj_type='attribute', addendum='', *, removal=''):
2525

2626
if removal == "":
27-
removal = {"2.2": "in 3.1", "3.0": "in 3.2"}.get(
27+
removal = {"2.2": "in 3.1", "3.0": "in 3.2", "3.1": "in 3.3"}.get(
2828
since, "two minor releases later")
2929
elif removal:
3030
if pending:

lib/matplotlib/legend.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import numpy as np
2828

2929
from matplotlib import rcParams
30-
from matplotlib import docstring
30+
from matplotlib import cbook, docstring
3131
from matplotlib.artist import Artist, allow_rasterization
3232
from matplotlib.cbook import silent_list, is_hashable, warn_deprecated
3333
from matplotlib.font_manager import FontProperties
@@ -491,22 +491,26 @@ def __init__(self, parent, handles, labels,
491491
if isinstance(loc, str):
492492
if loc not in self.codes:
493493
if self.isaxes:
494-
warnings.warn('Unrecognized location "%s". Falling back '
495-
'on "best"; valid locations are\n\t%s\n'
496-
% (loc, '\n\t'.join(self.codes)))
494+
cbook.warn_deprecated(
495+
"3.1", message="Unrecognized location {!r}. Falling "
496+
"back on 'best'; valid locations are\n\t{}\n"
497+
"This will raise an exception %(removal)s."
498+
.format(loc, '\n\t'.join(self.codes)))
497499
loc = 0
498500
else:
499-
warnings.warn('Unrecognized location "%s". Falling back '
500-
'on "upper right"; '
501-
'valid locations are\n\t%s\n'
502-
% (loc, '\n\t'.join(self.codes)))
501+
cbook.warn_deprecated(
502+
"3.1", message="Unrecognized location {!r}. Falling "
503+
"back on 'upper right'; valid locations are\n\t{}\n'"
504+
"This will raise an exception %(removal)s."
505+
.format(loc, '\n\t'.join(self.codes)))
503506
loc = 1
504507
else:
505508
loc = self.codes[loc]
506509
if not self.isaxes and loc == 0:
507-
warnings.warn('Automatic legend placement (loc="best") not '
508-
'implemented for figure legend. '
509-
'Falling back on "upper right".')
510+
cbook.warn_deprecated(
511+
"3.1", message="Automatic legend placement (loc='best') not "
512+
"implemented for figure legend. Falling back on 'upper "
513+
"right'. This will raise an exception %(removal)s.")
510514
loc = 1
511515

512516
self._mode = mode

lib/matplotlib/offsetbox.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,17 @@
1818

1919
import numpy as np
2020

21-
import matplotlib.transforms as mtransforms
21+
from matplotlib import cbook, docstring, rcParams
2222
import matplotlib.artist as martist
23-
import matplotlib.text as mtext
2423
import matplotlib.path as mpath
25-
from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
26-
24+
import matplotlib.text as mtext
25+
import matplotlib.transforms as mtransforms
2726
from matplotlib.font_manager import FontProperties
28-
from matplotlib.patches import FancyBboxPatch, FancyArrowPatch
29-
from matplotlib import rcParams
30-
31-
from matplotlib import docstring
32-
3327
from matplotlib.image import BboxImage
34-
35-
from matplotlib.patches import bbox_artist as mbbox_artist
28+
from matplotlib.patches import (
29+
FancyBboxPatch, FancyArrowPatch, bbox_artist as mbbox_artist)
3630
from matplotlib.text import _AnnotationBase
31+
from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
3732

3833

3934
DEBUG = False
@@ -1249,8 +1244,10 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5, prop=None, **kwargs):
12491244
prop = {}
12501245
badkwargs = {'ha', 'horizontalalignment', 'va', 'verticalalignment'}
12511246
if badkwargs & set(prop):
1252-
warnings.warn("Mixing horizontalalignment or verticalalignment "
1253-
"with AnchoredText is not supported.")
1247+
cbook.warn_deprecated(
1248+
"3.1", "Mixing horizontalalignment or verticalalignment with "
1249+
"AnchoredText is not supported, deprecated since %(version)s, "
1250+
"and will raise an exception %(removal)s.")
12541251

12551252
self.txt = TextArea(s, textprops=prop, minimumdescent=False)
12561253
fp = self.txt._text.get_fontproperties()

lib/matplotlib/table.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
Author : John Gill <jng@europe.renre.com>
1818
Copyright : 2004 John Gill and John Hunter
1919
License : matplotlib license
20-
2120
"""
22-
import warnings
2321

2422
from . import artist, cbook, docstring
2523
from .artist import Artist, allow_rasterization
@@ -243,9 +241,11 @@ def __init__(self, ax, loc=None, bbox=None, **kwargs):
243241

244242
if isinstance(loc, str):
245243
if loc not in self.codes:
246-
warnings.warn('Unrecognized location %s. Falling back on '
247-
'bottom; valid locations are\n%s\t' %
248-
(loc, '\n\t'.join(self.codes)))
244+
cbook.warn_deprecated(
245+
"3.1", message="Unrecognized location {!r}. Falling back "
246+
"on 'bottom'; valid locations are\n\t{}\n"
247+
"This will raise an exception %(removal)s."
248+
.format(loc, '\n\t'.join(self.codes)))
249249
loc = 'bottom'
250250
loc = self.codes[loc]
251251
self.set_figure(ax.figure)

lib/matplotlib/ticker.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -1878,16 +1878,12 @@ def __init__(self, *args, **kwargs):
18781878
@staticmethod
18791879
def _validate_steps(steps):
18801880
if not np.iterable(steps):
1881-
raise ValueError('steps argument must be a sequence of numbers '
1882-
'from 1 to 10')
1881+
raise ValueError('steps argument must be an increasing sequence '
1882+
'of numbers between 1 and 10 inclusive')
18831883
steps = np.asarray(steps)
1884-
if np.any(np.diff(steps) <= 0):
1885-
raise ValueError('steps argument must be uniformly increasing')
1886-
if steps[-1] > 10 or steps[0] < 1:
1887-
warnings.warn('Steps argument should be a sequence of numbers\n'
1888-
'increasing from 1 to 10, inclusive. Behavior with\n'
1889-
'values outside this range is undefined, and will\n'
1890-
'raise a ValueError in future versions of mpl.')
1884+
if np.any(np.diff(steps) <= 0) or steps[-1] > 10 or steps[0] < 1:
1885+
raise ValueError('steps argument must be an increasing sequence '
1886+
'of numbers between 1 and 10 inclusive')
18911887
if steps[0] != 1:
18921888
steps = np.hstack((1, steps))
18931889
if steps[-1] != 10:

0 commit comments

Comments
 (0)