diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index d73a7b2722fa..1b86cf07a0b1 100644 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -1457,9 +1457,10 @@ def get_locator(self, dmin, dmax): else: locator = MicrosecondLocator(interval, tz=self.tz) if dmin.year > 20 and interval < 1000: - _log.warning('Plotting microsecond time intervals is not well ' - 'supported. Please see the MicrosecondLocator ' - 'documentation for details.') + cbook._warn_external( + 'Plotting microsecond time intervals is not well ' + 'supported; please see the MicrosecondLocator ' + 'documentation for details.') locator.set_axis(self.axis) diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index 6ec118b40d87..8e9cb55f3bbd 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -1,4 +1,8 @@ import datetime +try: + from contextlib import nullcontext +except ImportError: + from contextlib import ExitStack as nullcontext # Py 3.6. import dateutil.tz import dateutil.rrule @@ -369,7 +373,9 @@ def _create_auto_date_locator(date1, date2): for t_delta, expected in results: d2 = d1 + t_delta locator = _create_auto_date_locator(d1, d2) - assert list(map(str, mdates.num2date(locator()))) == expected + with (pytest.warns(UserWarning) if t_delta.microseconds + else nullcontext()): + assert list(map(str, mdates.num2date(locator()))) == expected def test_auto_date_locator_intmult(): @@ -444,7 +450,9 @@ def _create_auto_date_locator(date1, date2): for t_delta, expected in results: d2 = d1 + t_delta locator = _create_auto_date_locator(d1, d2) - assert list(map(str, mdates.num2date(locator()))) == expected + with (pytest.warns(UserWarning) if t_delta.microseconds + else nullcontext()): + assert list(map(str, mdates.num2date(locator()))) == expected def test_concise_formatter():