Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc28615

Browse files
authored
Merge pull request #20311 from anntzer/rcs
FIX: Warn if a non-str is passed to an rcParam requiring a str.
2 parents 9091ba9 + 7ebad92 commit cc28615

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
rcParams will no longer cast inputs to str
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
After a deprecation period, rcParams that expect a (non-pathlike) str will no
4+
longer cast non-str inputs using `str`. This will avoid confusing errors in
5+
subsequent code if e.g. a list input gets implicitly cast to a str.

lib/matplotlib/rcsetup.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from functools import lru_cache, reduce
1818
from numbers import Number
1919
import operator
20+
import os
2021
import re
2122

2223
import numpy as np
@@ -198,6 +199,11 @@ def validator(s):
198199
if (allow_none and
199200
(s is None or isinstance(s, str) and s.lower() == "none")):
200201
return None
202+
if cls is str and not isinstance(s, str):
203+
_api.warn_deprecated(
204+
"3.5", message="Support for setting an rcParam that expects a "
205+
"str value to a non-str value is deprecated since %(since)s "
206+
"and support will be removed %(removal)s.")
201207
try:
202208
return cls(s)
203209
except (TypeError, ValueError) as e:
@@ -224,6 +230,15 @@ def validator(s):
224230
validate_float, doc='return a list of floats')
225231

226232

233+
def _validate_pathlike(s):
234+
if isinstance(s, (str, os.PathLike)):
235+
# Store value as str because savefig.directory needs to distinguish
236+
# between "" (cwd) and "." (cwd, but gets updated by user selections).
237+
return os.fsdecode(s)
238+
else:
239+
return validate_string(s) # Emit deprecation warning.
240+
241+
227242
def validate_fonttype(s):
228243
"""
229244
Confirm that this is a Postscript or PDF font type that we know how to
@@ -1154,7 +1169,7 @@ def _convert_validator_spec(key, conv):
11541169
"savefig.bbox": validate_bbox, # "tight", or "standard" (= None)
11551170
"savefig.pad_inches": validate_float,
11561171
# default directory in savefig dialog box
1157-
"savefig.directory": validate_string,
1172+
"savefig.directory": _validate_pathlike,
11581173
"savefig.transparent": validate_bool,
11591174

11601175
"tk.window_focus": validate_bool, # Maintain shell focus for TkAgg
@@ -1224,15 +1239,15 @@ def _convert_validator_spec(key, conv):
12241239
# Additional arguments for HTML writer
12251240
"animation.html_args": validate_stringlist,
12261241
# Path to ffmpeg binary. If just binary name, subprocess uses $PATH.
1227-
"animation.ffmpeg_path": validate_string,
1242+
"animation.ffmpeg_path": _validate_pathlike,
12281243
# Additional arguments for ffmpeg movie writer (using pipes)
12291244
"animation.ffmpeg_args": validate_stringlist,
12301245
# Path to AVConv binary. If just binary name, subprocess uses $PATH.
1231-
"animation.avconv_path": validate_string,
1246+
"animation.avconv_path": _validate_pathlike,
12321247
# Additional arguments for avconv movie writer (using pipes)
12331248
"animation.avconv_args": validate_stringlist,
12341249
# Path to convert binary. If just binary name, subprocess uses $PATH.
1235-
"animation.convert_path": validate_string,
1250+
"animation.convert_path": _validate_pathlike,
12361251
# Additional arguments for convert movie writer (using pipes)
12371252
"animation.convert_args": validate_stringlist,
12381253

lib/matplotlib/tests/test_rcparams.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ def generate_validator_testcases(valid):
225225
(('a', 'b'), ['a', 'b']),
226226
(iter(['a', 'b']), ['a', 'b']),
227227
(np.array(['a', 'b']), ['a', 'b']),
228-
((1, 2), ['1', '2']),
229-
(np.array([1, 2]), ['1', '2']),
230228
),
231229
'fail': ((set(), ValueError),
232230
(1, ValueError),
231+
((1, 2), _api.MatplotlibDeprecationWarning),
232+
(np.array([1, 2]), _api.MatplotlibDeprecationWarning),
233233
)
234234
},
235235
{'validator': _listify_validator(validate_int, n=2),

0 commit comments

Comments
 (0)