Skip to content

Commit 326ec0e

Browse files
committed
Get default params from matplotlibrc.template.
This makes matplotlibrc.template a valid style file representing the default style (modulo things that styles do not control, such as the backend), avoids having to duplicate the defaults in rcsetup, removes the need to test that the default matplotlibrc is up to date (if not, matplotlib fails to import), and makes it easier to fit lines within 79 characters in rcsetup.py. The only tricky points are: - datapath is not really an rcparam anyways, so just hack `__getitem__` to always return the correct value. - the entry for path.effects was incorrect, as [] would be validated as the "[]" list which is not a valid list of patheffects. In fact this rcParam cannot be meaningfully set from matplotlibrc; one would need to do an eval() like for axes.prop_cycle but let's not get there... I changed the validator to validate_anylist, which at least works for the default of an empty list... - we need to be a bit more careful when constructing the global rcParams instance as well as rcParamsOrig, rcParamsDefault, to not resolve _auto_backend_sentinel too early. One can check that the rcParams are unchanged by printing them out -- that catches a typo: two entries in font.fantasy should be "Impact", "Western"; not "ImpactWestern".
1 parent 385dd94 commit 326ec0e

File tree

6 files changed

+1133
-1209
lines changed

6 files changed

+1133
-1209
lines changed

.flake8

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ per-file-ignores =
3535
lib/matplotlib/_mathtext_data.py: E203, E261
3636
lib/matplotlib/font_manager.py: E203, E221, E251, E501
3737
lib/matplotlib/mathtext.py: E221, E251
38-
lib/matplotlib/rcsetup.py: E501
3938
lib/matplotlib/tests/test_mathtext.py: E501
4039
lib/matplotlib/transforms.py: E201, E202, E203
4140
lib/matplotlib/tri/triinterpolate.py: E201, E221

lib/matplotlib/__init__.py

+27-22
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
from matplotlib.cbook import (
140140
MatplotlibDeprecationWarning, dedent, get_label, sanitize_sequence)
141141
from matplotlib.cbook import mplDeprecation # deprecated
142-
from matplotlib.rcsetup import defaultParams, validate_backend, cycler
142+
from matplotlib.rcsetup import validate_backend, cycler
143143

144144
import numpy
145145

@@ -613,7 +613,8 @@ def get_cachedir():
613613
return _get_config_or_cache_dir(_get_xdg_cache_dir())
614614

615615

616-
def _get_data_path():
616+
@_logged_cached('matplotlib data path: %s')
617+
def get_data_path():
617618
"""Return the path to matplotlib data."""
618619

619620
if 'MATPLOTLIBDATA' in os.environ:
@@ -654,13 +655,6 @@ def get_candidate_paths():
654655
raise RuntimeError('Could not find the matplotlib data files')
655656

656657

657-
@_logged_cached('matplotlib data path: %s')
658-
def get_data_path():
659-
if defaultParams['datapath'][0] is None:
660-
defaultParams['datapath'][0] = _get_data_path()
661-
return defaultParams['datapath'][0]
662-
663-
664658
@cbook.deprecated("3.1")
665659
def get_py2exe_datafiles():
666660
data_path = Path(get_data_path())
@@ -744,9 +738,7 @@ class RcParams(MutableMapping, dict):
744738
:mod:`matplotlib.rcsetup`.
745739
"""
746740

747-
validate = {key: converter
748-
for key, (default, converter) in defaultParams.items()
749-
if key not in _all_deprecated}
741+
validate = rcsetup._validators
750742

751743
# validate values on the way in
752744
def __init__(self, *args, **kwargs):
@@ -802,6 +794,9 @@ def __getitem__(self, key):
802794
from matplotlib import pyplot as plt
803795
plt.switch_backend(rcsetup._auto_backend_sentinel)
804796

797+
elif key == "datapath":
798+
return get_data_path()
799+
805800
return dict.__getitem__(self, key)
806801

807802
def __repr__(self):
@@ -912,7 +907,7 @@ def _rc_params_in_file(fname, fail_on_error=False):
912907
config = RcParams()
913908

914909
for key, (val, line, line_no) in rc_temp.items():
915-
if key in defaultParams:
910+
if key in rcsetup._validators:
916911
if fail_on_error:
917912
config[key] = val # try to convert to proper type or raise
918913
else:
@@ -957,11 +952,8 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
957952
if not use_default_template:
958953
return config_from_file
959954

960-
iter_params = defaultParams.items()
961955
with cbook._suppress_matplotlib_deprecation_warning():
962-
config = RcParams([(key, default) for key, (default, _) in iter_params
963-
if key not in _all_deprecated])
964-
config.update(config_from_file)
956+
config = RcParams({**rcParamsDefault, **config_from_file})
965957

966958
if config['datapath'] is None:
967959
config['datapath'] = get_data_path()
@@ -979,15 +971,28 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
979971
return config
980972

981973

982-
# this is the instance used by the matplotlib classes
983-
rcParams = rc_params()
974+
# When constructing the global instances, we need to perform certain updates
975+
# by explicitly calling the superclass (dict.update, dict.items) to avoid
976+
# triggering resolution of _auto_backend_sentinel.
984977

978+
rcParamsDefault = _rc_params_in_file(
979+
cbook._get_data_path("matplotlibrc"), fail_on_error=True)
980+
with cbook._suppress_matplotlib_deprecation_warning():
981+
dict.update(rcParamsDefault, rcsetup._hardcoded_defaults)
982+
983+
rcParams = RcParams() # The global instance.
984+
with cbook._suppress_matplotlib_deprecation_warning():
985+
dict.update(rcParams, dict.items(rcParamsDefault))
986+
dict.update(rcParams, _rc_params_in_file(matplotlib_fname()))
985987

986988
with cbook._suppress_matplotlib_deprecation_warning():
989+
defaultParams = rcsetup.defaultParams = { # Left only for backcompat.
990+
# We want to resolve deprecated rcParams, but not backend...
991+
key: [(rcsetup._auto_backend_sentinel if key == "backend" else
992+
rcParamsDefault[key]),
993+
validator]
994+
for key, validator in rcsetup._validators.items()}
987995
rcParamsOrig = RcParams(rcParams.copy())
988-
rcParamsDefault = RcParams([(key, default) for key, (default, converter) in
989-
defaultParams.items()
990-
if key not in _all_deprecated])
991996

992997
if rcParams['axes.formatter.use_locale']:
993998
locale.setlocale(locale.LC_ALL, '')

lib/matplotlib/cbook/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def get_sample_data(fname, asfileobj=True):
439439
440440
If the filename ends in .gz, the file is implicitly ungzipped.
441441
"""
442-
path = Path(matplotlib._get_data_path(), 'sample_data', fname)
442+
path = _get_data_path('sample_data', fname)
443443
if asfileobj:
444444
suffix = path.suffix.lower()
445445
if suffix == '.gz':

0 commit comments

Comments
 (0)