Skip to content

converted assert into exception #3060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Mar 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
873e91d
converted assert into exception
montefra May 13, 2014
b297f41
Exception type modified according to @pelson comment
montefra May 26, 2014
187f584
removed assert in `draw_artist` and `redraw_in_frame`
montefra May 26, 2014
34c6a5f
most asserts modified to ValueError
montefra May 26, 2014
0c1f9f7
All asserts substituted by ValueError
montefra May 28, 2014
4cfa781
method name explicitly written in the message
montefra May 28, 2014
aba9d99
Most asserts changed to ValueErrors. Two changed to warning
montefra May 28, 2014
8fbb652
c* files done
montefra May 30, 2014
5a45046
assert removed
montefra May 30, 2014
33fdabe
Asserts removed. Side effect: function to check that the inputs have …
montefra May 30, 2014
67bdfea
Asserts removed throughout the files m*
montefra Jun 3, 2014
531004c
Asserts removed throughout the files r*
montefra Jun 3, 2014
eaca138
SyntaxError from Travis corrected
montefra Jun 3, 2014
9e4b911
assert removed from files s*
montefra Jun 3, 2014
d12fbb8
asserts removed from t* file. test and tri directories ignored
montefra Jun 4, 2014
7f80628
asserts removed from [u-z]* files
montefra Jun 4, 2014
4fc36c6
the only assert in a python public function removed
montefra Jun 4, 2014
617b622
Bug introduced while getting rid of the asserts fixed
montefra Jun 18, 2014
2cbb326
typo fixed (broke building documentation)
montefra Mar 5, 2015
20966e9
pep8 fixed. plot_day_summary2 removed (retained by error when rebasing)
montefra Mar 5, 2015
96c733e
PEP8 fixed
montefra Mar 5, 2015
140210f
PEP8 fixed - image.py
montefra Mar 5, 2015
a2abc7b
PEP 8 fixed - mlab.py
montefra Mar 5, 2015
25cec22
PEP 8 fixed - patches.py
montefra Mar 5, 2015
06aedf7
PEP8 fixed - path.py
montefra Mar 5, 2015
77da3b6
PEP8 fixed - sankey.py
montefra Mar 5, 2015
95ae2cd
PEP8 fixed - spines.py, table.py
montefra Mar 5, 2015
b167c0c
test adapted to code change (AssertionError -> ValueError)
montefra Mar 5, 2015
f9792a9
fixed according to #3060 comment
montefra Mar 5, 2015
850178e
Two bugs in assert -> exception transformation fixed
montefra Mar 5, 2015
c0ebd4f
Typo fixed
montefra Mar 5, 2015
6b6d2de
Bug in assert -> exception transformation fixed
montefra Mar 5, 2015
088542e
Typo fixed
montefra Mar 5, 2015
41bf6b5
Modified according to @tacaswell comments
montefra Mar 16, 2015
7ea5c1a
fixed pep8 Travis failure
montefra Mar 16, 2015
d6c3c32
python2.6 string formatting. style more uniform
montefra Mar 22, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1978,19 +1978,19 @@ def make_iterable(x):
if len(edgecolor) < nbars:
edgecolor *= nbars

# FIXME: convert the following to proper input validation
# raising ValueError; don't use assert for this.
assert len(left) == nbars, ("incompatible sizes: argument 'left' must "
"be length %d or scalar" % nbars)
assert len(height) == nbars, ("incompatible sizes: argument 'height' "
"must be length %d or scalar" %
nbars)
assert len(width) == nbars, ("incompatible sizes: argument 'width' "
"must be length %d or scalar" %
nbars)
assert len(bottom) == nbars, ("incompatible sizes: argument 'bottom' "
"must be length %d or scalar" %
nbars)
# input validation
if len(left) != nbars:
raise ValueError("incompatible sizes: argument 'left' must "
"be length %d or scalar" % nbars)
if len(height) != nbars:
raise ValueError("incompatible sizes: argument 'height' "
"must be length %d or scalar" % nbars)
if len(width) != nbars:
raise ValueError("incompatible sizes: argument 'width' "
"must be length %d or scalar" % nbars)
if len(bottom) != nbars:
raise ValueError("incompatible sizes: argument 'bottom' "
"must be length %d or scalar" % nbars)

patches = []

Expand Down Expand Up @@ -2428,8 +2428,10 @@ def pie(self, x, explode=None, labels=None, colors=None,
labels = [''] * len(x)
if explode is None:
explode = [0] * len(x)
assert(len(x) == len(labels))
assert(len(x) == len(explode))
if len(x) != len(labels):
raise ValueError("'label' must be of length 'x'")
if len(x) != len(explode):
raise ValueError("'explode' must be of length 'x'")
if colors is None:
colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w')

Expand Down Expand Up @@ -3686,8 +3688,9 @@ def scatter(self, x, y, s=20, c=None, marker='o', cmap=None, norm=None,
collection.update(kwargs)

if colors is None:
if norm is not None:
assert(isinstance(norm, mcolors.Normalize))
if norm is not None and not isinstance(norm, mcolors.Normalize):
msg = "'norm' must be an instance of 'mcolors.Normalize'"
raise ValueError(msg)
collection.set_array(np.asarray(c))
collection.set_cmap(cmap)
collection.set_norm(norm)
Expand Down Expand Up @@ -4057,8 +4060,9 @@ def hexbin(self, x, y, C=None, gridsize=100, bins=None,
bins = np.sort(bins)
accum = bins.searchsorted(accum)

if norm is not None:
assert(isinstance(norm, mcolors.Normalize))
if norm is not None and not isinstance(norm, mcolors.Normalize):
msg = "'norm' must be an instance of 'mcolors.Normalize'"
raise ValueError(msg)
collection.set_array(accum)
collection.set_cmap(cmap)
collection.set_norm(norm)
Expand Down Expand Up @@ -4673,8 +4677,9 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
if not self._hold:
self.cla()

if norm is not None:
assert(isinstance(norm, mcolors.Normalize))
if norm is not None and not isinstance(norm, mcolors.Normalize):
msg = "'norm' must be an instance of 'mcolors.Normalize'"
raise ValueError(msg)
if aspect is None:
aspect = rcParams['image.aspect']
self.set_aspect(aspect)
Expand Down Expand Up @@ -5000,8 +5005,9 @@ def pcolor(self, *args, **kwargs):

collection.set_alpha(alpha)
collection.set_array(C)
if norm is not None:
assert(isinstance(norm, mcolors.Normalize))
if norm is not None and not isinstance(norm, mcolors.Normalize):
msg = "'norm' must be an instance of 'mcolors.Normalize'"
raise ValueError(msg)
collection.set_cmap(cmap)
collection.set_norm(norm)
collection.set_clim(vmin, vmax)
Expand Down Expand Up @@ -5149,8 +5155,9 @@ def pcolormesh(self, *args, **kwargs):
antialiased=antialiased, shading=shading, **kwargs)
collection.set_alpha(alpha)
collection.set_array(C)
if norm is not None:
assert(isinstance(norm, mcolors.Normalize))
if norm is not None and not isinstance(norm, mcolors.Normalize):
msg = "'norm' must be an instance of 'mcolors.Normalize'"
raise ValueError(msg)
collection.set_cmap(cmap)
collection.set_norm(norm)
collection.set_clim(vmin, vmax)
Expand Down Expand Up @@ -5274,8 +5281,9 @@ def pcolorfast(self, *args, **kwargs):
cmap = kwargs.pop('cmap', None)
vmin = kwargs.pop('vmin', None)
vmax = kwargs.pop('vmax', None)
if norm is not None:
assert(isinstance(norm, mcolors.Normalize))
if norm is not None and not isinstance(norm, mcolors.Normalize):
msg = "'norm' must be an instance of 'mcolors.Normalize'"
raise ValueError(msg)

C = args[-1]
nr, nc = C.shape
Expand Down
10 changes: 8 additions & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,10 @@ def draw_artist(self, a):
caches the renderer. It is used to efficiently update Axes
data (axis ticks, labels, etc are not updated)
"""
assert self._cachedRenderer is not None
if self._cachedRenderer is None:
msg = ('draw_artist can only be used after an initial draw which'
' caches the render')
raise AttributeError(msg)
a.draw(self._cachedRenderer)

def redraw_in_frame(self):
Expand All @@ -2113,7 +2116,10 @@ def redraw_in_frame(self):
caches the renderer. It is used to efficiently update Axes
data (axis ticks, labels, etc are not updated)
"""
assert self._cachedRenderer is not None
if self._cachedRenderer is None:
msg = ('redraw_in_frame can only be used after an initial draw'
' which caches the render')
raise AttributeError(msg)
self.draw(self._cachedRenderer, inframe=True)

def get_renderer_cache(self):
Expand Down
19 changes: 12 additions & 7 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1728,11 +1728,13 @@ def set_label_position(self, position):

ACCEPTS: [ 'top' | 'bottom' ]
"""
assert position == 'top' or position == 'bottom'
if position == 'top':
self.label.set_verticalalignment('baseline')
else:
elif position == 'bottom':
self.label.set_verticalalignment('top')
else:
msg = "Position accepts only [ 'top' | 'bottom' ]"
raise ValueError(msg)
self.label_position = position

def _update_label_position(self, bboxes, bboxes2):
Expand Down Expand Up @@ -2032,13 +2034,15 @@ def set_label_position(self, position):

ACCEPTS: [ 'left' | 'right' ]
"""
assert position == 'left' or position == 'right'
self.label.set_rotation_mode('anchor')
self.label.set_horizontalalignment('center')
if position == 'left':
self.label.set_verticalalignment('bottom')
else:
elif position == 'right':
self.label.set_verticalalignment('top')
else:
msg = "Position accepts only [ 'left' | 'right' ]"
raise ValueError(msg)
self.label_position = position

def _update_label_position(self, bboxes, bboxes2):
Expand Down Expand Up @@ -2083,13 +2087,14 @@ def _update_offset_text_position(self, bboxes, bboxes2):
)

def set_offset_position(self, position):
assert position == 'left' or position == 'right'

x, y = self.offsetText.get_position()
if position == 'left':
x = 0
else:
elif position == 'right':
x = 1
else:
msg = "Position accepts only [ 'left' | 'right' ]"
raise ValueError(msg)

self.offsetText.set_ha(position)
self.offsetText.set_position((x, y))
Expand Down
6 changes: 5 additions & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,11 @@ def set_clip_path(self, path):
Set the clip path and transformation. Path should be a
:class:`~matplotlib.transforms.TransformedPath` instance.
"""
assert path is None or isinstance(path, transforms.TransformedPath)
if path is not None and not isinstance(path,
transforms.TransformedPath):
msg = ("Path should be a matplotlib.transforms.TransformedPath"
"instance.")
raise ValueError(msg)
self._clippath = path

def set_dashes(self, dash_offset, dash_list):
Expand Down
22 changes: 13 additions & 9 deletions lib/matplotlib/blocking_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
from matplotlib.cbook import is_sequence_of_strings
import matplotlib.lines as mlines

import warnings


class BlockingInput(object):
"""
Expand All @@ -38,8 +40,8 @@ class BlockingInput(object):
"""
def __init__(self, fig, eventslist=()):
self.fig = fig
assert is_sequence_of_strings(
eventslist), "Requires a sequence of event name strings"
if not is_sequence_of_strings(eventslist):
raise ValueError("Requires a sequence of event name strings")
self.eventslist = eventslist

def on_event(self, event):
Expand Down Expand Up @@ -95,7 +97,8 @@ def __call__(self, n=1, timeout=30):
Blocking call to retrieve n events
"""

assert isinstance(n, int), "Requires an integer argument"
if not isinstance(n, int):
raise ValueError("Requires an integer argument")
self.n = n

self.events = []
Expand Down Expand Up @@ -146,9 +149,9 @@ def post_event(self):
"""
This will be called to process events
"""
assert len(self.events) > 0, "No events yet"

if self.events[-1].name == 'key_press_event':
if len(self.events) == 0:
warnings.warn("No events yet")
elif self.events[-1].name == 'key_press_event':
self.key_event()
else:
self.mouse_event()
Expand Down Expand Up @@ -359,9 +362,10 @@ def post_event(self):
"""
Determines if it is a key event
"""
assert len(self.events) > 0, "No events yet"

self.keyormouse = self.events[-1].name == 'key_press_event'
if len(self.events) == 0:
warnings.warn("No events yet")
else:
self.keyormouse = self.events[-1].name == 'key_press_event'

def __call__(self, timeout=30):
"""
Expand Down
10 changes: 6 additions & 4 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,12 @@ def __init__(self, ax, *args, **kwargs):
else:
self.logscale = False

if self.origin is not None:
assert(self.origin in ['lower', 'upper', 'image'])
if self.extent is not None:
assert(len(self.extent) == 4)
if self.origin not in [None, 'lower', 'upper', 'image']:
raise ValueError("If given, *origin* must be one of [ 'lower' |"
" 'upper' | 'image']")
if self.extent is not None and len(self.extent) != 4:
raise ValueError("If given, *extent* must be '[ *None* |"
" (x0,x1,y0,y1) ]'")
if self.colors is not None and cmap is not None:
raise ValueError('Either colors or cmap must be None')
if self.origin == 'image':
Expand Down
14 changes: 11 additions & 3 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,9 @@ def add_axes(self, *args, **kwargs):

if isinstance(args[0], Axes):
a = args[0]
assert(a.get_figure() is self)
if a.get_figure() is not self:
msg = "The Axes must have been created in the present figure"
raise ValueError(msg)
else:
rect = args[0]
projection_class, kwargs, key = process_projection_requirements(
Expand Down Expand Up @@ -946,7 +948,10 @@ def add_subplot(self, *args, **kwargs):
if isinstance(args[0], SubplotBase):

a = args[0]
assert(a.get_figure() is self)
if a.get_figure() is not self:
msg = ("The Subplot must have been created in the present"
" figure")
raise ValueError(msg)
# make a key for the subplot (which includes the axes object id
# in the hash)
key = self._make_key(*args, **kwargs)
Expand Down Expand Up @@ -1104,7 +1109,10 @@ def draw_artist(self, a):
draw :class:`matplotlib.artist.Artist` instance *a* only --
this is available only after the figure is drawn
"""
assert self._cachedRenderer is not None
if self._cachedRenderer is None:
msg = ('draw_artist can only be used after an initial draw which'
' caches the render')
raise AttributeError(msg)
a.draw(self._cachedRenderer)

def get_axes(self):
Expand Down
Loading