Skip to content

Commit

Permalink
Be a bit more stringent on invalid inputs.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
anntzer committed Oct 25, 2018
1 parent cfc01ce commit 8ce71f0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 30 deletions.
13 changes: 13 additions & 0 deletions doc/api/next_api_changes/2018-10-25-AL.rst
@@ -0,0 +1,13 @@
Invalid inputs
``````````````

Passing invalid locations to `legend` and `table` used to fallback on a default
location. This behavior is deprecated and will throw an exception in a future
version.

`offsetbox.AnchoredText` is unable to handle the ``horizontalalignment`` or
``verticalalignment`` kwargs, and used to ignore them with a warning. This
behavior is deprecated and will throw an exception in a future version.

Passing steps less than 1 or greater than 10 to `MaxNLocator` used to result in
undefined behavior. It now throws a ValueError.
4 changes: 1 addition & 3 deletions lib/matplotlib/backends/backend_cairo.py
Expand Up @@ -8,7 +8,6 @@

import copy
import gzip
import warnings

import numpy as np

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

# surface.set_dpi() can be used
renderer = RendererCairo(self.figure.dpi)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/cbook/deprecation.py
Expand Up @@ -24,7 +24,7 @@ def _generate_deprecation_message(
obj_type='attribute', addendum='', *, removal=''):

if removal == "":
removal = {"2.2": "in 3.1", "3.0": "in 3.2"}.get(
removal = {"2.2": "in 3.1", "3.0": "in 3.2", "3.1": "in 3.3"}.get(
since, "two minor releases later")
elif removal:
if pending:
Expand Down
24 changes: 14 additions & 10 deletions lib/matplotlib/legend.py
Expand Up @@ -491,22 +491,26 @@ def __init__(self, parent, handles, labels,
if isinstance(loc, str):
if loc not in self.codes:
if self.isaxes:
warnings.warn('Unrecognized location "%s". Falling back '
'on "best"; valid locations are\n\t%s\n'
% (loc, '\n\t'.join(self.codes)))
cbook.warn_deprecated(
"3.1", message="Unrecognized location {!r}. Falling "
"back on 'best'; valid locations are\n\t{}\n"
"This will raise an exception %(removal)s."
.format(loc, '\n\t'.join(self.codes)))
loc = 0
else:
warnings.warn('Unrecognized location "%s". Falling back '
'on "upper right"; '
'valid locations are\n\t%s\n'
% (loc, '\n\t'.join(self.codes)))
cbook.warn_deprecated(
"3.1", message="Unrecognized location {!r}. Falling "
"back on 'upper right'; valid locations are\n\t{}\n'
"This will raise an exception %(removal)s."
.format(loc, '\n\t'.join(self.codes)))
loc = 1
else:
loc = self.codes[loc]
if not self.isaxes and loc == 0:
warnings.warn('Automatic legend placement (loc="best") not '
'implemented for figure legend. '
'Falling back on "upper right".')
cbook.warn_deprecated(
"3.1", message="Automatic legend placement (loc='best') not "
"implemented for figure legend. Falling back on 'upper "
"right'. This will raise an exception %(removal)s.")
loc = 1

self._mode = mode
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/offsetbox.py
Expand Up @@ -1249,8 +1249,10 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5, prop=None, **kwargs):
prop = {}
badkwargs = {'ha', 'horizontalalignment', 'va', 'verticalalignment'}
if badkwargs & set(prop):
warnings.warn("Mixing horizontalalignment or verticalalignment "
"with AnchoredText is not supported.")
cbook.warn_deprecated(
"3.1", "Mixing horizontalalignment or verticalalignment with "
"AnchoredText is not supported, deprecated since %(version)s, "
"and will raise an exception %(removal)s.")

self.txt = TextArea(s, textprops=prop, minimumdescent=False)
fp = self.txt._text.get_fontproperties()
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/table.py
Expand Up @@ -17,9 +17,7 @@
Author : John Gill <jng@europe.renre.com>
Copyright : 2004 John Gill and John Hunter
License : matplotlib license
"""
import warnings

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

if isinstance(loc, str):
if loc not in self.codes:
warnings.warn('Unrecognized location %s. Falling back on '
'bottom; valid locations are\n%s\t' %
(loc, '\n\t'.join(self.codes)))
cbook.warn_deprecated(
"3.1", message="Unrecognized location {!r}. Falling back "
"on 'bottom'; valid locations are\n\t{}\n"
"This will raise an exception %(removal)s."
.format(loc, '\n\t'.join(self.codes)))
loc = 'bottom'
loc = self.codes[loc]
self.set_figure(ax.figure)
Expand Down
14 changes: 5 additions & 9 deletions lib/matplotlib/ticker.py
Expand Up @@ -1878,16 +1878,12 @@ def __init__(self, *args, **kwargs):
@staticmethod
def _validate_steps(steps):
if not np.iterable(steps):
raise ValueError('steps argument must be a sequence of numbers '
'from 1 to 10')
raise ValueError('steps argument must be an increasing sequence '
'of numbers between 1 and 10 inclusive')
steps = np.asarray(steps)
if np.any(np.diff(steps) <= 0):
raise ValueError('steps argument must be uniformly increasing')
if steps[-1] > 10 or steps[0] < 1:
warnings.warn('Steps argument should be a sequence of numbers\n'
'increasing from 1 to 10, inclusive. Behavior with\n'
'values outside this range is undefined, and will\n'
'raise a ValueError in future versions of mpl.')
if np.any(np.diff(steps) <= 0) or steps[-1] > 10 or steps[0] < 1:
raise ValueError('steps argument must be an increasing sequence '
'of numbers between 1 and 10 inclusive')
if steps[0] != 1:
steps = np.hstack((1, steps))
if steps[-1] != 10:
Expand Down

0 comments on commit 8ce71f0

Please sign in to comment.