8000 Merge pull request #16722 from anntzer/deprecate-rcdatapath-32 · matplotlib/matplotlib@4897c4d · GitHub
[go: up one dir, main page]

Skip to content

Commit 4897c4d

Browse files
authored
Merge pull request #16722 from anntzer/deprecate-rcdatapath-32
API: Deprecate rcParams["datapath"] in favor of mpl.get_data_path().
2 parents 3ee24e5 + 7a8a6f7 commit 4897c4d

File tree

5 files changed

+46
-19
lines changed

5 files changed

+46
-19
lines changed

doc/api/matplotlib_configuration_api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Default values and styling
4343

4444
.. autofunction:: matplotlib_fname
4545

46+
.. autofunction:: get_data_path
47+
4648
Logging
4749
=======
4850

doc/api/prev_api_changes/api_changes_3.2.0/deprecations.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,10 @@ from the public API in future versions.
290290

291291
``style.core.is_style_file`` and ``style.core.iter_style_files``
292292
are deprecated.
293+
294+
The ``datapath`` rcParam
295+
~~~~~~~~~~~~~~~~~~~~~~~~
296+
Use `.get_data_path` instead. (The rcParam is deprecated because it cannot be
297+
meaningfully set by an end user.) The rcParam had no effect from 3.2.0, but
298+
was deprecated only in 3.2.1. In 3.2.1+ if ``'datapath'`` is set in a
299+
``matplotlibrc`` file it will be respected, but this behavior will be removed in 3.3.

lib/matplotlib/__init__.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
import shutil
133133
import subprocess
134134
import tempfile
135+
import warnings
135136

136137
# cbook must import matplotlib only within function
137138
# definitions, so it is safe to import from it here.
@@ -269,10 +270,10 @@ def func(): ...
269270
ret = None
270271

271272
@functools.wraps(func)
272-
def wrapper():
273+
def wrapper(**kwargs):
273274
nonlocal called, ret
274275
if not called:
275-
ret = func()
276+
ret = func(**kwargs)
276277
called = True
277278
_log.debug(fmt, ret)
278279
return ret
@@ -619,9 +620,31 @@ def get_cachedir():
619620
return _get_config_or_cache_dir(_get_xdg_cache_dir())
620621

621622

622-
def _get_data_path():
623-
"""Return the path to matplotlib data."""
623+
@_logged_cached('matplotlib data path: %s')
624+
def get_data_path(*, _from_rc=None):
625+
"""Return the path to Matplotlib data."""
626+
if _from_rc is not None:
627+
cbook.warn_deprecated(
628+
"3.2",
629+
message=("Setting the datapath via matplotlibrc is "
630+
"deprecated %(since)s and will be removed in %(removal)s. "
631+
""),
632+
removal='3.3')
633+
path = Path(_from_rc)
634+
if path.is_dir():
635+
defaultParams['datapath'][0] = str(path)
636+
return str(path)
637+
else:
638+
warnings.warn(f"You passed datapath: {_from_rc!r} in your "
639+
f"matplotribrc file ({matplotlib_fname()}). "
640+
"However this path does not exist, falling back "
641+
"to standard paths.")
642+
643+
return _get_data_path()
624644

645+
646+
@_logged_cached('(private) matplotlib data path: %s')
647+
def _get_data_path():
625648
if 'MATPLOTLIBDATA' in os.environ:
626649
path = os.environ['MATPLOTLIBDATA']
627650
if not os.path.isdir(path):
@@ -633,6 +656,7 @@ def _get_data_path():
633656

634657
path = Path(__file__).with_name("mpl-data")
635658
if path.is_dir():
659+
defaultParams['datapath'][0] = str(path)
636660
return str(path)
637661

638662
cbook.warn_deprecated(
@@ -655,18 +679,12 @@ def get_candidate_paths():
655679

656680
for path in get_candidate_paths():
657681
if path.is_dir():
682+
defaultParams['datapath'][0] = str(path)
658683
return str(path)
659684

660685
raise RuntimeError('Could not find the matplotlib data files')
661686

662687

663-
@_logged_cached('matplotlib data path: %s')
664-
def get_data_path():
665-
if defaultParams['datapath'][0] is None:
666-
defaultParams['datapath'][0] = _get_data_path()
667-
return defaultParams['datapath'][0]
668-
669-
670688
@cbook.deprecated("3.1")
671689
def get_py2exe_datafiles():
672690
data_path = Path(get_data_path())
@@ -708,7 +726,7 @@ def gen_candidates():
708726
yield matplotlibrc
709727
yield os.path.join(matplotlibrc, 'matplotlibrc')
710728
yield os.path.join(get_configdir(), 'matplotlibrc')
711-
yield os.path.join(get_data_path(), 'matplotlibrc')
729+
yield os.path.join(_get_data_path(), 'matplotlibrc')
712730

713731
for fname in gen_candidates():
714732
if os.path.exists(fname) and not os.path.isdir(fname):
@@ -736,6 +754,7 @@ def gen_candidates():
736754
'savefig.frameon': ('3.1',),
737755
'verbose.fileo': ('3.1',),
738756
'verbose.level': ('3.1',),
757+
'datapath': ('3.2.1',),
739758
}
740759

741760

@@ -973,8 +992,11 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
973992
if key not in _all_deprecated])
974993
config.update(config_from_file)
975994

976-
if config['datapath'] is None:
977-
config['datapath'] = get_data_path()
995+
with cbook._suppress_matplotlib_deprecation_warning():
996+
if config['datapath'] is None:
997+
config['datapath'] = _get_data_path()
998+
else:
999+
config['datapath'] = get_data_path(_from_rc=config['datapath'])
9781000

9791001
if "".join(config['text.latex.preamble']):
9801002
_log.info("""

lib/matplotlib/cbook/__init__.py

Lines changed: 1 addition & 1 deletion
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 = Path(matplotlib.get_data_path(), 'sample_data', fname)
443443
if asfileobj:
444444
suffix = path.suffix.lower()
445445
if suffix == '.gz':

matplotlibrc.template

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@
103103
#toolbar : toolbar2 ## {None, toolbar2}
104104
#timezone : UTC ## a pytz timezone string, e.g., US/Central or Europe/Paris
105105

106-
## Where your matplotlib data lives if you installed to a non-default
107-
## location. This is where the matplotlib fonts, bitmaps, etc reside
108-
#datapath : /home/jdhunter/mpldata
109-
110106

111107
## ***************************************************************************
112108
## * LINES *

0 commit comments

Comments
 (0)
0