From bed898d5bb1064f07c867089ce1b8a485e2c1fec Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 7 Aug 2019 14:18:53 +0200 Subject: [PATCH] Use warnings.warn, not logging.warning, in microseconds locator warning. Otherwise something like ``` from pylab import * from matplotlib import dates; from datetime import * plt.plot([datetime(1990, 1, 1), datetime(1990, 1, 1) + timedelta(microseconds=1500)], [0, 0]) plt.show() ``` results in the same warning being emitted multiple times *for every draw* (e.g. when zooming/panning), which is pretty needless spam. --- lib/matplotlib/dates.py | 7 ++++--- lib/matplotlib/tests/test_dates.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) 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():