Skip to content

Add parameter checks to DayLocator initiator #6955

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 4 commits into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 2 additions & 13 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,19 +821,6 @@ def tick_values(self, vmin, vmax):

self.rule.set(dtstart=start, until=stop)

# estimate the number of ticks very approximately so we don't
# have to do a very expensive (and potentially near infinite)
# 'between' calculation, only to find out it will fail.
nmax, nmin = date2num((vmax, vmin))
estimate = (nmax - nmin) / (self._get_unit() * self._get_interval())
# This estimate is only an estimate, so be really conservative
# about bailing...
if estimate > self.MAXTICKS * 2:
raise RuntimeError(
'RRuleLocator estimated to generate %d ticks from %s to %s: '
'exceeds Locator.MAXTICKS * 2 (%d) ' % (estimate, vmin, vmax,
self.MAXTICKS * 2))

dates = self.rule.between(vmin, vmax, True)
if len(dates) == 0:
return date2num([vmin, vmax])
Expand Down Expand Up @@ -1254,6 +1241,8 @@ def __init__(self, bymonthday=None, interval=1, tz=None):

Default is to tick every day of the month: ``bymonthday=range(1,32)``
"""
if not interval == int(interval) or interval < 1:
raise ValueError("interval must be an integer greater than 0")
if bymonthday is None:
bymonthday = range(1, 32)
elif isinstance(bymonthday, np.ndarray):
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,14 @@ def tz_convert(*args):
_test_date2num_dst(pd.date_range, tz_convert)


def test_DayLocator():
assert_raises(ValueError, mdates.DayLocator, interval=-1)
assert_raises(ValueError, mdates.DayLocator, interval=-1.5)
assert_raises(ValueError, mdates.DayLocator, interval=0)
assert_raises(ValueError, mdates.DayLocator, interval=1.3)
mdates.DayLocator(interval=1.0)


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)