Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56f3234

Browse files
author
Vedant Nanda
committed
Cleaner tests and moved repeated calls to a function
1 parent e1a2745 commit 56f3234

File tree

4 files changed

+39
-75
lines changed

4 files changed

+39
-75
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,14 +2874,8 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
28742874

28752875
self._process_unit_info(xdata=(left, right))
28762876
if left is not None:
2877-
if (isinstance(left, float) and
2878-
(not np.isreal(left) or not np.isfinite(left))):
2879-
raise ValueError("NaN or Inf cannot be the argument values")
28802877
left = self.convert_xunits(left)
28812878
if right is not None:
2882-
if (isinstance(right, float) and
2883-
(not np.isreal(right) or not np.isfinite(right))):
2884-
raise ValueError("NaN or Inf cannot be the argument values")
28852879
right = self.convert_xunits(right)
28862880

28872881
if ((left is not None and not np.isfinite(left)) or
@@ -3179,14 +3173,8 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
31793173
bottom, top = bottom
31803174

31813175
if bottom is not None:
3182-
if (isinstance(bottom, float) and
3183-
(not np.isreal(bottom) or not np.isfinite(bottom))):
3184-
raise ValueError("NaN or Inf cannot be the argument values")
31853176
bottom = self.convert_yunits(bottom)
31863177
if top is not None:
3187-
if (isinstance(top, float) and
3188-
(not np.isreal(top) or not np.isfinite(top))):
3189-
raise ValueError("NaN or Inf cannot be the argument values")
31903178
top = self.convert_yunits(top)
31913179

31923180
if ((top is not None and not np.isfinite(top)) or

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4974,21 +4974,14 @@ def test_bar_single_height():
49744974
ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal')
49754975

49764976

4977-
def test_invalid_axes_limits():
4977+
@pytest.mark.parametrize('value', [np.inf, np.nan])
4978+
@pytest.mark.parametrize(('setter', 'side'), [
4979+
(plt.xlim, 'left'),
4980+
(plt.xlim, 'right'),
4981+
(plt.ylim, 'bottom'),
4982+
(plt.ylim, 'top'),
4983+
])
4984+
def test_invalid_axes_limits(setter, side, value):
4985+
limit = {side: value}
49784986
with pytest.raises(ValueError):
4979-
plt.xlim(left=np.nan)
4980-
with pytest.raises(ValueError):
4981-
plt.xlim(left=np.inf)
4982-
with pytest.raises(ValueError):
4983-
plt.xlim(right=np.nan)
4984-
with pytest.raises(ValueError):
4985-
plt.xlim(right=np.inf)
4986-
4987-
with pytest.raises(ValueError):
4988-
plt.ylim(bottom=np.nan)
4989-
with pytest.raises(ValueError):
4990-
plt.ylim(bottom=np.inf)
4991-
with pytest.raises(ValueError):
4992-
plt.ylim(top=np.nan)
4993-
with pytest.raises(ValueError):
4994-
plt.ylim(top=np.inf)
4987+
setter(**limit)

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,17 @@ def _determine_lims(self, xmin=None, xmax=None, *args, **kwargs):
585585
xmax += 0.05
586586
return (xmin, xmax)
587587

588+
def _validate_axis_limits(self, limit):
589+
"""
590+
If the axis limits being set are infinite, this function
591+
592+
raises an error.
593+
594+
"""
595+
if (isinstance(limit, float) and
596+
(not np.isreal(limit) or not np.isfinite(limit))):
597+
raise ValueError("NaN or Inf cannot be the argument values")
598+
588599
def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
589600
"""
590601
Set 3D x limits.
@@ -604,14 +615,10 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
604615

605616
self._process_unit_info(xdata=(left, right))
606617
if left is not None:
607-
if (isinstance(left, float) and
608-
(not np.isreal(left) or not np.isfinite(left))):
609-
raise ValueError("NaN or Inf cannot be the argument values")
618+
self._validate_axis_limits(left)
610619
left = self.convert_xunits(left)
611620
if right is not None:
612-
if (isinstance(right, float) and
613-
(not np.isreal(right) or not np.isfinite(right))):
614-
raise ValueError("NaN or Inf cannot be the argument values")
621+
self._validate_axis_limits(right)
615622
right = self.convert_xunits(right)
616623

617624
old_left, old_right = self.get_xlim()
@@ -664,14 +671,10 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
664671

665672
self._process_unit_info(ydata=(bottom, top))
666673
if bottom is not None:
667-
if (isinstance(bottom, float) and
668-
(not np.isreal(bottom) or not np.isfinite(bottom))):
669-
raise ValueError("NaN or Inf cannot be the argument values")
674+
self._validate_axis_limits(bottom)
670675
bottom = self.convert_yunits(bottom)
671676
if top is not None:
672-
if (isinstance(top, float) and
673-
(not np.isreal(top) or not np.isfinite(top))):
674-
raise ValueError("NaN or Inf cannot be the argument values")
677+
self._validate_axis_limits(top)
675678
top = self.convert_yunits(top)
676679

677680
old_bottom, old_top = self.get_ylim()
@@ -724,14 +727,10 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
724727

725728
self._process_unit_info(zdata=(bottom, top))
726729
if bottom is not None:
727-
if (isinstance(bottom, float) and
728-
(not np.isreal(bottom) or not np.isfinite(bottom))):
729-
raise ValueError("NaN or Inf cannot be the argument values")
730+
self._validate_axis_limits(bottom)
730731
bottom = self.convert_zunits(bottom)
731732
if top is not None:
732-
if (isinstance(top, float) and
733-
(not np.isreal(top) or not np.isfinite(top))):
734-
raise ValueError("NaN or Inf cannot be the argument values")
733+
self._validate_axis_limits(top)
735734
top = self.convert_zunits(top)
736735

737736
old_bottom, old_top = self.get_zlim()

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -493,32 +493,16 @@ def test_autoscale():
493493
assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.4, 2.4)
494494

495495

496-
def test_invalid_axes_limits():
497-
fig = plt.figure()
498-
ax = fig.add_subplot(111, projection='3d')
499-
with pytest.raises(ValueError):
500-
ax.set_xlim3d(left=np.nan)
501-
with pytest.raises(ValueError):
502-
ax.set_xlim3d(left=np.inf)
503-
with pytest.raises(ValueError):
504-
ax.set_xlim3d(right=np.nan)
505-
with pytest.raises(ValueError):
506-
ax.set_xlim3d(right=np.inf)
507-
508-
with pytest.raises(ValueError):
509-
ax.set_ylim3d(bottom=np.nan)
510-
with pytest.raises(ValueError):
511-
ax.set_ylim3d(bottom=np.inf)
512-
with pytest.raises(ValueError):
513-
ax.set_ylim3d(top=np.nan)
514-
with pytest.raises(ValueError):
515-
ax.set_ylim3d(top=np.inf)
516-
517-
with pytest.raises(ValueError):
518-
ax.set_zlim3d(bottom=np.nan)
519-
with pytest.raises(ValueError):
520-
ax.set_zlim3d(bottom=np.inf)
521-
with pytest.raises(ValueError):
522-
ax.set_zlim3d(top=np.nan)
496+
@pytest.mark.parametrize('value', [np.inf, np.nan])
497+
@pytest.mark.parametrize(('setter', 'side'), [
498+
(plt.figure().add_subplot(111, projection='3d').set_xlim3d, 'left'),
499+
(plt.figure().add_subplot(111, projection='3d').set_xlim3d, 'right'),
500+
(plt.figure().add_subplot(111, projection='3d').set_ylim3d, 'bottom'),
501+
(plt.figure().add_subplot(111, projection='3d').set_ylim3d, 'top'),
502+
(plt.figure().add_subplot(111, projection='3d').set_zlim3d, 'bottom'),
503+
(plt.figure().add_subplot(111, projection='3d').set_zlim3d, 'top'),
504+
])
505+
def test_invalid_axes_limits(setter, side, value):
506+
limit = {side: value}
523507
with pytest.raises(ValueError):
524-
ax.set_zlim3d(top=np.inf)
508+
setter(**limit)

0 commit comments

Comments
 (0)