Skip to content

Commit b710367

Browse files
authored
Merge pull request #21035 from meeseeksmachine/auto-backport-of-pr-21031-on-v3.5.x
Backport PR #21031 on branch v3.5.x (Make date.{converter,interval_multiples} rcvalidators side-effect free.)
2 parents 77625ce + 21b8a81 commit b710367

File tree

4 files changed

+31
-61
lines changed

4 files changed

+31
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Setting invalid ``rcParams["date.converter"]`` now raises ValueError
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, invalid values would be ignored with a UserWarning.

lib/matplotlib/dates.py

+25-37
Original file line numberDiff line numberDiff line change
@@ -1890,44 +1890,32 @@ def axisinfo(self, unit, axis):
18901890
default_limits=(datemin, datemax))
18911891

18921892

1893-
class _rcParam_helper:
1893+
class _SwitchableDateConverter:
18941894
"""
1895-
This helper class is so that we can set the converter for dates
1896-
via the validator for the rcParams `date.converter` and
1897-
`date.interval_multiples`. Never instatiated.
1895+
Helper converter-like object that generates and dispatches to
1896+
temporary ConciseDateConverter or DateConverter instances based on
1897+
:rc:`date.converter` and :rc:`date.interval_multiples`.
18981898
"""
18991899

1900-
conv_st = 'auto'
1901-
int_mult = True
1902-
1903-
@classmethod
1904-
def set_converter(cls, s):
1905-
"""Called by validator for rcParams date.converter"""
1906-
if s not in ['concise', 'auto']:
1907-
raise ValueError('Converter must be one of "concise" or "auto"')
1908-
cls.conv_st = s
1909-
cls.register_converters()
1910-
1911-
@classmethod
1912-
def set_int_mult(cls, b):
1913-
"""Called by validator for rcParams date.interval_multiples"""
1914-
cls.int_mult = b
1915-
cls.register_converters()
1916-
1917-
@classmethod
1918-
def register_converters(cls):
1919-
"""
1920-
Helper to register the date converters when rcParams `date.converter`
1921-
and `date.interval_multiples` are changed. Called by the helpers
1922-
above.
1923-
"""
1924-
if cls.conv_st == 'concise':
1925-
converter = ConciseDateConverter
1926-
else:
1927-
converter = DateConverter
1900+
@staticmethod
1901+
def _get_converter():
1902+
converter_cls = {
1903+
"concise": ConciseDateConverter, "auto": DateConverter}[
1904+
mpl.rcParams["date.converter"]]
1905+
interval_multiples = mpl.rcParams["date.interval_multiples"]
1906+
return converter_cls(interval_multiples=interval_multiples)
1907+
1908+
def axisinfo(self, *args, **kwargs):
1909+
return self._get_converter().axisinfo(*args, **kwargs)
1910+
1911+
def default_units(self, *args, **kwargs):
1912+
return self._get_converter().default_units(*args, **kwargs)
1913+
1914+
def convert(self, *args, **kwargs):
1915+
return self._get_converter().convert(*args, **kwargs)
1916+
19281917

1929-
interval_multiples = cls.int_mult
1930-
convert = converter(interval_multiples=interval_multiples)
1931-
units.registry[np.datetime64] = convert
1932-
units.registry[datetime.date] = convert
1933-
units.registry[datetime.datetime] = convert
1918+
units.registry[np.datetime64] = \
1919+
units.registry[datetime.date] = \
1920+
units.registry[datetime.datetime] = \
1921+
_SwitchableDateConverter()

lib/matplotlib/rcsetup.py

+2-23
Original file line numberDiff line numberDiff line change
@@ -149,26 +149,6 @@ def validate_bool(b):
149149
raise ValueError('Could not convert "%s" to bool' % b)
150150

151151

152-
def _validate_date_converter(s):
153-
if s is None:
154-
return
155-
s = validate_string(s)
156-
if s not in ['auto', 'concise']:
157-
_api.warn_external(f'date.converter string must be "auto" or '
158-
f'"concise", not "{s}". Check your matplotlibrc')
159-
return
160-
import matplotlib.dates as mdates
161-
mdates._rcParam_helper.set_converter(s)
162-
163-
164-
def _validate_date_int_mult(s):
165-
if s is None:
166-
return
167-
s = validate_bool(s)
168-
import matplotlib.dates as mdates
169-
mdates._rcParam_helper.set_int_mult(s)
170-
171-
172152
def validate_axisbelow(s):
173153
try:
174154
return validate_bool(s)
@@ -1036,10 +1016,9 @@ def _convert_validator_spec(key, conv):
10361016
"date.autoformatter.second": validate_string,
10371017
"date.autoformatter.microsecond": validate_string,
10381018

1039-
# 'auto', 'concise', 'auto-noninterval'
1040-
'date.converter': _validate_date_converter,
1019+
'date.converter': ['auto', 'concise'],
10411020
# for auto date locator, choose interval_multiples
1042-
'date.interval_multiples': _validate_date_int_mult,
1021+
'date.interval_multiples': validate_bool,
10431022

10441023
# legend properties
10451024
"legend.fancybox": validate_bool,

lib/matplotlib/tests/test_dates.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ def test_change_converter():
11521152
fig.canvas.draw()
11531153
assert ax.get_xticklabels()[0].get_text() == 'Jan 01 2020'
11541154
assert ax.get_xticklabels()[1].get_text() == 'Jan 15 2020'
1155-
with pytest.warns(UserWarning) as rec:
1155+
with pytest.raises(ValueError):
11561156
plt.rcParams['date.converter'] = 'boo'
11571157

11581158

0 commit comments

Comments
 (0)