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

Skip to content

Commit f245996

Browse files
committed
rcsetup cleanups.
- Deprecate passing text.latex.unicode/pgf.preamble as None, "None", or a list of strings -- it's really just a plain string. - In validators that take "lists", disable passing sets/frozensets, which are unordered, but not dicts, which have deterministic iteration order now -- making the code consistent with the comment immediately above. - Correctly set `__name__` and `__qualname__` on some more validators, which helps e.g. troubleshooting test failures.
1 parent 160f711 commit f245996

File tree

7 files changed

+31
-17
lines changed

7 files changed

+31
-17
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,9 @@ Passing both singular and plural *colors*, *linewidths*, *linestyles* to `.Axes.
8282
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8383
Passing e.g. both *linewidth* and *linewidths* will raise a TypeError in the
8484
future.
85+
86+
Setting :rc:`text.latex.preamble` or :rc:`pdf.preamble` to non-strings
87+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88+
These rcParams should be set to string values. Support for None (meaning the
89+
empty string) and lists of strings (implicitly joined with newlines) is
90+
deprecated.

lib/matplotlib/rcsetup.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"""
1616

1717
import ast
18-
from collections.abc import Iterable, Mapping
1918
from functools import partial, reduce
2019
import logging
2120
from numbers import Number
@@ -86,11 +85,9 @@ def f(s):
8685
for v in s if v.strip()]
8786
else:
8887
raise
89-
# We should allow any generic sequence type, including generators,
90-
# Numpy ndarrays, and pandas data structures. However, unordered
91-
# sequences, such as sets, should be allowed but discouraged unless the
92-
# user desires pseudorandom behavior.
93-
elif isinstance(s, Iterable) and not isinstance(s, Mapping):
88+
# Allow any ordered sequence type -- generators, np.ndarray, pd.Series
89+
# -- but not sets, whose iteration order is non-deterministic.
90+
elif np.iterable(s) and not isinstance(s, (set, frozenset)):
9491
# The condition on this list comprehension will preserve the
9592
# behavior of filtering out any empty strings (behavior was
9693
# from the original validate_stringlist()), while allowing
@@ -104,6 +101,7 @@ def f(s):
104101
f.__name__ = "{}list".format(scalar_validator.__name__)
105102
except AttributeError: # class instance.
106103
f.__name__ = "{}List".format(type(scalar_validator).__name__)
104+
f.__qualname__ = f.__qualname__.rsplit(".", 1)[0] + "." + f.__name__
107105
f.__doc__ = doc if doc is not None else scalar_validator.__doc__
108106
return f
109107

@@ -151,12 +149,19 @@ def validate_bool_maybe_none(b):
151149

152150

153151
def _validate_tex_preamble(s):
152+
message = (
153+
f"Support for setting the 'text.latex.unicode' and 'pdf.preamble' "
154+
f"rcParams to {s!r} is deprecated since %(since)s and will be "
155+
f"removed %(removal)s; please set them to plain (possibly empty) "
156+
f"strings instead.")
154157
if s is None or s == 'None':
158+
cbook.warn_deprecated("3.3", message=message)
155159
return ""
156160
try:
157161
if isinstance(s, str):
158162
return s
159-
elif isinstance(s, Iterable):
163+
elif np.iterable(s):
164+
cbook.warn_deprecated("3.3", message=message)
160165
return '\n'.join(s)
161166
else:
162167
raise TypeError
@@ -202,6 +207,11 @@ def validator(s):
202207
except ValueError:
203208
raise ValueError(f'Could not convert {s!r} to {cls.__name__}')
204209

210+
validator.__name__ = f"validate_{cls.__name__}"
211+
if allow_none:
212+
validator.__name__ += "_or_None"
213+
validator.__qualname__ = (
214+
validator.__qualname__.rsplit(".", 1)[0] + "." + validator.__name__)
205215
return validator
206216

207217

lib/matplotlib/tests/test_axes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from decimal import Decimal
1414
import pytest
1515

16-
import warnings
17-
1816
import matplotlib
1917
import matplotlib as mpl
2018
from matplotlib.testing.decorators import (

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ def test_pdflatex():
112112
rc_pdflatex = {'font.family': 'serif',
113113
'pgf.rcfonts': False,
114114
'pgf.texsystem': 'pdflatex',
115-
'pgf.preamble': ['\\usepackage[utf8x]{inputenc}',
116-
'\\usepackage[T1]{fontenc}']}
115+
'pgf.preamble': ('\\usepackage[utf8x]{inputenc}'
116+
'\\usepackage[T1]{fontenc}')}
117117
mpl.rcParams.update(rc_pdflatex)
118118
create_figure()
119119

@@ -137,9 +137,9 @@ def test_rcupdate():
137137
'lines.markersize': 20,
138138
'pgf.rcfonts': False,
139139
'pgf.texsystem': 'pdflatex',
140-
'pgf.preamble': ['\\usepackage[utf8x]{inputenc}',
141-
'\\usepackage[T1]{fontenc}',
142-
'\\usepackage{sfmath}']}]
140+
'pgf.preamble': ('\\usepackage[utf8x]{inputenc}'
141+
'\\usepackage[T1]{fontenc}'
142+
'\\usepackage{sfmath}')}]
143143
tol = [6, 0]
144144
for i, rc_set in enumerate(rc_sets):
145145
with mpl.rc_context(rc_set):

lib/matplotlib/tests/test_cycles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_invalid_input_forms():
145145
with pytest.raises((TypeError, ValueError)):
146146
ax.set_prop_cycle('linewidth', 1)
147147
with pytest.raises((TypeError, ValueError)):
148-
ax.set_prop_cycle('linewidth', {'1': 1, '2': 2})
148+
ax.set_prop_cycle('linewidth', {1, 2})
149149
with pytest.raises((TypeError, ValueError)):
150150
ax.set_prop_cycle(linewidth=1, color='r')
151151

lib/matplotlib/tests/test_rcparams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def generate_validator_testcases(valid):
218218
((1, 2), ['1', '2']),
219219
(np.array([1, 2]), ['1', '2']),
220220
),
221-
'fail': ((dict(), ValueError),
221+
'fail': ((set(), ValueError),
222222
(1, ValueError),
223223
)
224224
},

lib/matplotlib/tests/test_texmanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_fontconfig_preamble():
1111
tm1 = TexManager()
1212
font_config1 = tm1.get_font_config()
1313

14-
plt.rcParams['text.latex.preamble'] = ['\\usepackage{txfonts}']
14+
plt.rcParams['text.latex.preamble'] = '\\usepackage{txfonts}'
1515
tm2 = TexManager()
1616
font_config2 = tm2.get_font_config()
1717

0 commit comments

Comments
 (0)