-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathtest_rcparams.py
More file actions
703 lines (611 loc) · 26.8 KB
/
test_rcparams.py
File metadata and controls
703 lines (611 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import copy
import os
import subprocess
import sys
from unittest import mock
from cycler import cycler, Cycler
from packaging.version import parse as parse_version
import pytest
import matplotlib as mpl
from matplotlib import _api, _c_internal_utils
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
from matplotlib import rcsetup
from matplotlib.rcsetup import (
validate_bool,
validate_color,
validate_colorlist,
_validate_color_or_linecolor,
validate_cycler,
validate_float,
validate_fontstretch,
validate_fontweight,
validate_hatch,
validate_hist_bins,
validate_int,
validate_markevery,
validate_stringlist,
validate_sketch,
_validate_linestyle,
_listify_validator)
from matplotlib.testing import subprocess_run_for_testing
def test_rcparams(tmp_path):
mpl.rc('text', usetex=False)
mpl.rc('lines', linewidth=22)
usetex = mpl.rcParams['text.usetex']
linewidth = mpl.rcParams['lines.linewidth']
rcpath = tmp_path / 'test_rcparams.rc'
rcpath.write_text('lines.linewidth: 33', encoding='utf-8')
# test context given dictionary
with mpl.rc_context(rc={'text.usetex': not usetex}):
assert mpl.rcParams['text.usetex'] == (not usetex)
assert mpl.rcParams['text.usetex'] == usetex
# test context given filename (mpl.rc sets linewidth to 33)
with mpl.rc_context(fname=rcpath):
assert mpl.rcParams['lines.linewidth'] == 33
assert mpl.rcParams['lines.linewidth'] == linewidth
# test context given filename and dictionary
with mpl.rc_context(fname=rcpath, rc={'lines.linewidth': 44}):
assert mpl.rcParams['lines.linewidth'] == 44
assert mpl.rcParams['lines.linewidth'] == linewidth
# test context as decorator (and test reusability, by calling func twice)
@mpl.rc_context({'lines.linewidth': 44})
def func():
assert mpl.rcParams['lines.linewidth'] == 44
func()
func()
# test rc_file
mpl.rc_file(rcpath)
assert mpl.rcParams['lines.linewidth'] == 33
def test_RcParams_class():
rc = mpl.RcParams({'font.cursive': ['Apple Chancery',
'Textile',
'Zapf Chancery',
'cursive'],
'font.family': 'sans-serif',
'font.weight': 'normal',
'font.size': 12})
expected_repr = """
RcParams({'font.cursive': ['Apple Chancery',
'Textile',
'Zapf Chancery',
'cursive'],
'font.family': ['sans-serif'],
'font.size': 12.0,
'font.weight': 'normal'})""".lstrip()
assert expected_repr == repr(rc)
expected_str = """
font.cursive: ['Apple Chancery', 'Textile', 'Zapf Chancery', 'cursive']
font.family: ['sans-serif']
font.size: 12.0
font.weight: normal""".lstrip()
assert expected_str == str(rc)
# test the find_all functionality
assert ['font.cursive', 'font.size'] == sorted(rc.find_all('i[vz]'))
assert ['font.family'] == list(rc.find_all('family'))
def test_rcparams_update():
rc = mpl.RcParams({'figure.figsize': (3.5, 42)})
bad_dict = {'figure.figsize': (3.5, 42, 1)}
# make sure validation happens on input
with pytest.raises(ValueError):
rc.update(bad_dict)
def test_rcparams_init():
with pytest.raises(ValueError):
mpl.RcParams({'figure.figsize': (3.5, 42, 1)})
def test_nargs_cycler():
from matplotlib.rcsetup import cycler as ccl
with pytest.raises(TypeError, match='3 were given'):
# cycler() takes 0-2 arguments.
ccl(ccl(color=list('rgb')), 2, 3)
def test_Bug_2543():
# Test that it possible to add all values to itself / deepcopy
# https://github.com/matplotlib/matplotlib/issues/2543
# We filter warnings at this stage since a number of them are raised
# for deprecated rcparams as they should. We don't want these in the
# printed in the test suite.
with _api.suppress_matplotlib_deprecation_warning():
with mpl.rc_context():
_copy = mpl.rcParams.copy()
for key in _copy:
mpl.rcParams[key] = _copy[key]
with mpl.rc_context():
copy.deepcopy(mpl.rcParams)
with pytest.raises(ValueError):
validate_bool(None)
with pytest.raises(ValueError):
with mpl.rc_context():
mpl.rcParams['svg.fonttype'] = True
legend_color_tests = [
('face', {'color': 'r'}, mcolors.to_rgba('r')),
('face', {'color': 'inherit', 'axes.facecolor': 'r'},
mcolors.to_rgba('r')),
('face', {'color': 'g', 'axes.facecolor': 'r'}, mcolors.to_rgba('g')),
('edge', {'color': 'r'}, mcolors.to_rgba('r')),
('edge', {'color': 'inherit', 'axes.edgecolor': 'r'},
mcolors.to_rgba('r')),
('edge', {'color': 'g', 'axes.facecolor': 'r'}, mcolors.to_rgba('g'))
]
legend_color_test_ids = [
'same facecolor',
'inherited facecolor',
'different facecolor',
'same edgecolor',
'inherited edgecolor',
'different facecolor',
]
@pytest.mark.parametrize('color_type, param_dict, target', legend_color_tests,
ids=legend_color_test_ids)
def test_legend_colors(color_type, param_dict, target):
param_dict[f'legend.{color_type}color'] = param_dict.pop('color')
get_func = f'get_{color_type}color'
with mpl.rc_context(param_dict):
_, ax = plt.subplots()
ax.plot(range(3), label='test')
leg = ax.legend()
assert getattr(leg.legendPatch, get_func)() == target
def test_mfc_rcparams():
mpl.rcParams['lines.markerfacecolor'] = 'r'
ln = mpl.lines.Line2D([1, 2], [1, 2])
assert ln.get_markerfacecolor() == 'r'
def test_mec_rcparams():
mpl.rcParams['lines.markeredgecolor'] = 'r'
ln = mpl.lines.Line2D([1, 2], [1, 2])
assert ln.get_markeredgecolor() == 'r'
def test_axes_titlecolor_rcparams():
mpl.rcParams['axes.titlecolor'] = 'r'
_, ax = plt.subplots()
title = ax.set_title("Title")
assert title.get_color() == 'r'
def test_Issue_1713(tmp_path):
rcpath = tmp_path / 'test_rcparams.rc'
rcpath.write_text('timezone: UTC', encoding='utf-8')
with mock.patch('locale.getpreferredencoding', return_value='UTF-32-BE'):
rc = mpl.rc_params_from_file(rcpath, True, False)
assert rc.get('timezone') == 'UTC'
def test_animation_frame_formats():
# Animation frame_format should allow any of the following
# if any of these are not allowed, an exception will be raised
# test for gh issue #17908
for fmt in ['png', 'jpeg', 'tiff', 'raw', 'rgba', 'ppm',
'sgi', 'bmp', 'pbm', 'svg']:
mpl.rcParams['animation.frame_format'] = fmt
def generate_validator_testcases(valid):
validation_tests = (
{'validator': validate_bool,
'success': (*((_, True) for _ in
('t', 'y', 'yes', 'on', 'true', '1', 1, True)),
*((_, False) for _ in
('f', 'n', 'no', 'off', 'false', '0', 0, False))),
'fail': ((_, ValueError)
for _ in ('aardvark', 2, -1, [], ))
},
{'validator': validate_stringlist,
'success': (('', []),
('a,b', ['a', 'b']),
('aardvark', ['aardvark']),
('aardvark, ', ['aardvark']),
('aardvark, ,', ['aardvark']),
(['a', 'b'], ['a', 'b']),
(('a', 'b'), ['a', 'b']),
(iter(['a', 'b']), ['a', 'b']),
(np.array(['a', 'b']), ['a', 'b']),
),
'fail': ((set(), ValueError),
(1, ValueError),
)
},
{'validator': _listify_validator(validate_int, n=2),
'success': ((_, [1, 2])
for _ in ('1, 2', [1.5, 2.5], [1, 2],
(1, 2), np.array((1, 2)))),
'fail': ((_, ValueError)
for _ in ('aardvark', ('a', 1),
(1, 2, 3)
))
},
{'validator': _listify_validator(validate_float, n=2),
'success': ((_, [1.5, 2.5])
for _ in ('1.5, 2.5', [1.5, 2.5], [1.5, 2.5],
(1.5, 2.5), np.array((1.5, 2.5)))),
'fail': ((_, ValueError)
for _ in ('aardvark', ('a', 1), (1, 2, 3), (None, ), None))
},
{'validator': validate_cycler,
'success': (('cycler("color", "rgb")',
cycler("color", 'rgb')),
('cycler("color", "Dark2")',
cycler("color", mpl.color_sequences["Dark2"])),
(cycler('linestyle', ['-', '--']),
cycler('linestyle', ['-', '--'])),
("""(cycler("color", ["r", "g", "b"]) +
cycler("mew", [2, 3, 5]))""",
(cycler("color", 'rgb') +
cycler("markeredgewidth", [2, 3, 5]))),
("cycler(c='rgb', lw=[1, 2, 3])",
cycler('color', 'rgb') + cycler('linewidth', [1, 2, 3])),
("cycler('c', 'rgb') * cycler('linestyle', ['-', '--'])",
(cycler('color', 'rgb') *
cycler('linestyle', ['-', '--']))),
(cycler('ls', ['-', '--']),
cycler('linestyle', ['-', '--'])),
(cycler(mew=[2, 5]),
cycler('markeredgewidth', [2, 5])),
("2 * cycler('color', 'rgb')", 2 * cycler('color', 'rgb')),
("2 * cycler('color', 'r' + 'gb')", 2 * cycler('color', 'rgb')),
("cycler(c='r' + 'gb', lw=[1, 2, 3])",
cycler('color', 'rgb') + cycler('linewidth', [1, 2, 3])),
("cycler('color', 'rgb') * 2", cycler('color', 'rgb') * 2),
("concat(cycler('color', 'rgb'), cycler('color', 'cmk'))",
cycler('color', list('rgbcmk'))),
("cycler('color', 'rgbcmk')[:3]", cycler('color', list('rgb'))),
("cycler('color', 'rgb')[::-1]", cycler('color', list('bgr'))),
),
# validate_cycler() parses an arbitrary string using a safe
# AST-based parser (no eval). These tests verify that only valid
# cycler expressions are accepted.
'fail': ((4, ValueError), # Gotta be a string or Cycler object
('cycler("bleh, [])', ValueError), # syntax error
("cycler('color', 'rgb') * * cycler('color', 'rgb')", # syntax error
ValueError),
('Cycler("linewidth", [1, 2, 3])',
ValueError), # only 'cycler()' function is allowed
# do not allow dunder in string literals
("cycler('c', [j.__class__(j) for j in ['r', 'b']])",
ValueError),
("cycler('c', [j. __class__(j) for j in ['r', 'b']])",
ValueError),
("cycler('c', [j.\t__class__(j) for j in ['r', 'b']])",
ValueError),
("cycler('c', [j.\u000c__class__(j) for j in ['r', 'b']])",
ValueError),
("cycler('c', [j.__class__(j).lower() for j in ['r', 'b']])",
ValueError),
# list comprehensions are arbitrary code, even if "safe"
("cycler('color', [x for x in ['r', 'g', 'b']])",
ValueError),
('1 + 2', ValueError), # doesn't produce a Cycler object
('os.system("echo Gotcha")', ValueError), # os not available
('import os', ValueError), # should not be able to import
('def badjuju(a): return a; badjuju(cycler("color", "rgb"))',
ValueError), # Should not be able to define anything
# even if it does return a cycler
('cycler("waka", [1, 2, 3])', ValueError), # not a property
('cycler(c=[1, 2, 3])', ValueError), # invalid values
("cycler(lw=['a', 'b', 'c'])", ValueError), # invalid values
(cycler('waka', [1, 3, 5]), ValueError), # not a property
(cycler('color', ['C1', 'r', 'g']), ValueError) # no CN
)
},
{'validator': validate_hatch,
'success': (('--|', '--|'), ('\\oO', '\\oO'),
('/+*/.x', '/+*/.x'), ('', '')),
'fail': (('--_', ValueError),
(8, ValueError),
('X', ValueError)),
},
{'validator': validate_colorlist,
'success': (('r,g,b', ['r', 'g', 'b']),
(['r', 'g', 'b'], ['r', 'g', 'b']),
('r, ,', ['r']),
(['', 'g', 'blue'], ['g', 'blue']),
([np.array([1, 0, 0]), np.array([0, 1, 0])],
np.array([[1, 0, 0], [0, 1, 0]])),
(np.array([[1, 0, 0], [0, 1, 0]]),
np.array([[1, 0, 0], [0, 1, 0]])),
),
'fail': (('fish', ValueError),
),
},
{'validator': validate_color,
'success': (('None', 'none'),
('none', 'none'),
('AABBCC', '#AABBCC'), # RGB hex code
('AABBCC00', '#AABBCC00'), # RGBA hex code
('tab:blue', 'tab:blue'), # named color
('C12', 'C12'), # color from cycle
('(0, 1, 0)', (0.0, 1.0, 0.0)), # RGB tuple
((0, 1, 0), (0, 1, 0)), # non-string version
('(0, 1, 0, 1)', (0.0, 1.0, 0.0, 1.0)), # RGBA tuple
((0, 1, 0, 1), (0, 1, 0, 1)), # non-string version
),
'fail': (('tab:veryblue', ValueError), # invalid name
('(0, 1)', ValueError), # tuple with length < 3
('(0, 1, 0, 1, 0)', ValueError), # tuple with length > 4
('(0, 1, none)', ValueError), # cannot cast none to float
('(0, 1, "0.5")', ValueError), # last one not a float
),
},
{'validator': _validate_color_or_linecolor,
'success': (('linecolor', 'linecolor'),
('markerfacecolor', 'markerfacecolor'),
('mfc', 'markerfacecolor'),
('markeredgecolor', 'markeredgecolor'),
('mec', 'markeredgecolor')
),
'fail': (('line', ValueError),
('marker', ValueError)
)
},
{'validator': validate_hist_bins,
'success': (('auto', 'auto'),
('fd', 'fd'),
('10', 10),
('1, 2, 3', [1, 2, 3]),
([1, 2, 3], [1, 2, 3]),
(np.arange(15), np.arange(15))
),
'fail': (('aardvark', ValueError),
)
},
{'validator': validate_markevery,
'success': ((None, None),
(1, 1),
(0.1, 0.1),
((1, 1), (1, 1)),
((0.1, 0.1), (0.1, 0.1)),
([1, 2, 3], [1, 2, 3]),
(slice(2), slice(None, 2, None)),
(slice(1, 2, 3), slice(1, 2, 3))
),
'fail': (((1, 2, 3), TypeError),
([1, 2, 0.3], TypeError),
(['a', 2, 3], TypeError),
([1, 2, 'a'], TypeError),
((0.1, 0.2, 0.3), TypeError),
((0.1, 2, 3), TypeError),
((1, 0.2, 0.3), TypeError),
((1, 0.1), TypeError),
((0.1, 1), TypeError),
(('abc'), TypeError),
((1, 'a'), TypeError),
((0.1, 'b'), TypeError),
(('a', 1), TypeError),
(('a', 0.1), TypeError),
('abc', TypeError),
('a', TypeError),
(object(), TypeError)
)
},
{'validator': _validate_linestyle,
'success': (('-', '-'), ('solid', 'solid'),
('--', '--'), ('dashed', 'dashed'),
('-.', '-.'), ('dashdot', 'dashdot'),
(':', ':'), ('dotted', 'dotted'),
('', ''), (' ', ' '),
('None', 'none'), ('none', 'none'),
('DoTtEd', 'dotted'), # case-insensitive
('1, 3', (0, (1, 3))),
([1.23, 456], (0, [1.23, 456.0])),
([1, 2, 3, 4], (0, [1.0, 2.0, 3.0, 4.0])),
((0, [1, 2]), (0, [1, 2])),
((-1, [1, 2]), (-1, [1, 2])),
),
'fail': (('aardvark', ValueError), # not a valid string
(b'dotted', ValueError),
('dotted'.encode('utf-16'), ValueError),
([1, 2, 3], ValueError), # sequence with odd length
(1.23, ValueError), # not a sequence
(("a", [1, 2]), ValueError), # wrong explicit offset
((None, [1, 2]), ValueError), # wrong explicit offset
((1, [1, 2, 3]), ValueError), # odd length sequence
(([1, 2], 1), ValueError), # inverted offset/onoff
)
},
)
for validator_dict in validation_tests:
validator = validator_dict['validator']
if valid:
for arg, target in validator_dict['success']:
yield validator, arg, target
else:
for arg, error_type in validator_dict['fail']:
yield validator, arg, error_type
@pytest.mark.parametrize('validator, arg, target',
generate_validator_testcases(True))
def test_validator_valid(validator, arg, target):
res = validator(arg)
if isinstance(target, np.ndarray):
np.testing.assert_equal(res, target)
elif not isinstance(target, Cycler):
assert res == target
else:
# Cyclers can't simply be asserted equal. They don't implement __eq__
assert list(res) == list(target)
@pytest.mark.parametrize('validator, arg, exception_type',
generate_validator_testcases(False))
def test_validator_invalid(validator, arg, exception_type):
with pytest.raises(exception_type):
validator(arg)
def test_validate_cycler_bad_color_string():
msg = "'foo' is neither a color sequence name nor can it be interpreted as a list"
with pytest.raises(ValueError, match=msg):
validate_cycler("cycler('color', 'foo')")
@pytest.mark.parametrize('weight, parsed_weight', [
('bold', 'bold'),
('BOLD', ValueError), # weight is case-sensitive
(100, 100),
('100', 100),
(np.array(100), 100),
# fractional fontweights are not defined. This should actually raise a
# ValueError, but historically did not.
(20.6, 20),
('20.6', ValueError),
([100], ValueError),
])
def test_validate_fontweight(weight, parsed_weight):
if parsed_weight is ValueError:
with pytest.raises(ValueError):
validate_fontweight(weight)
else:
assert validate_fontweight(weight) == parsed_weight
@pytest.mark.parametrize('stretch, parsed_stretch', [
('expanded', 'expanded'),
('EXPANDED', ValueError), # stretch is case-sensitive
(100, 100),
('100', 100),
(np.array(100), 100),
# fractional fontweights are not defined. This should actually raise a
# ValueError, but historically did not.
(20.6, 20),
('20.6', ValueError),
([100], ValueError),
])
def test_validate_fontstretch(stretch, parsed_stretch):
if parsed_stretch is ValueError:
with pytest.raises(ValueError):
validate_fontstretch(stretch)
else:
assert validate_fontstretch(stretch) == parsed_stretch
def test_keymaps():
key_list = [k for k in mpl.rcParams if 'keymap' in k]
for k in key_list:
assert isinstance(mpl.rcParams[k], list)
def test_no_backend_reset_rccontext():
assert mpl.rcParams['backend'] != 'module://aardvark'
with mpl.rc_context():
mpl.rcParams['backend'] = 'module://aardvark'
assert mpl.rcParams['backend'] == 'module://aardvark'
def test_rcparams_reset_after_fail():
# There was previously a bug that meant that if rc_context failed and
# raised an exception due to issues in the supplied rc parameters, the
# global rc parameters were left in a modified state.
with mpl.rc_context(rc={'text.usetex': False}):
assert mpl.rcParams['text.usetex'] is False
with pytest.raises(KeyError):
with mpl.rc_context(rc={'text.usetex': True, 'test.blah': True}):
pass
assert mpl.rcParams['text.usetex'] is False
@pytest.mark.skipif(sys.platform != "linux", reason="Linux only")
def test_backend_fallback_headless_invalid_backend(tmp_path):
env = {**os.environ,
"DISPLAY": "", "WAYLAND_DISPLAY": "",
"MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
# plotting should fail with the tkagg backend selected in a headless environment
with pytest.raises(subprocess.CalledProcessError):
subprocess_run_for_testing(
[sys.executable, "-c",
"import matplotlib;"
"matplotlib.use('tkagg');"
"import matplotlib.pyplot;"
"matplotlib.pyplot.plot(42);"
],
env=env, check=True, stderr=subprocess.DEVNULL)
@pytest.mark.skipif(sys.platform != "linux", reason="Linux only")
def test_backend_fallback_headless_auto_backend(tmp_path):
# specify a headless mpl environment, but request a graphical (tk) backend
env = {**os.environ,
"DISPLAY": "", "WAYLAND_DISPLAY": "",
"MPLBACKEND": "TkAgg", "MPLCONFIGDIR": str(tmp_path)}
# allow fallback to an available interactive backend explicitly in configuration
rc_path = tmp_path / "matplotlibrc"
rc_path.write_text("backend_fallback: true")
# plotting should succeed, by falling back to use the generic agg backend
backend = subprocess_run_for_testing(
[sys.executable, "-c",
"import matplotlib.pyplot;"
"matplotlib.pyplot.plot(42);"
"print(matplotlib.get_backend());"
],
env=env, text=True, check=True, capture_output=True).stdout
assert backend.strip().lower() == "agg"
@pytest.mark.skipif(
sys.platform == "linux" and not _c_internal_utils.xdisplay_is_valid(),
reason="headless")
def test_backend_fallback_headful(tmp_path):
if parse_version(pytest.__version__) >= parse_version('8.2.0'):
pytest_kwargs = dict(exc_type=ImportError)
else:
pytest_kwargs = {}
pytest.importorskip("tkinter", **pytest_kwargs)
env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
backend = subprocess_run_for_testing(
[sys.executable, "-c",
"import matplotlib as mpl; "
"sentinel = mpl.rcsetup._auto_backend_sentinel; "
# Check that access on another instance does not resolve the sentinel.
"assert mpl.RcParams({'backend': sentinel})['backend'] == sentinel; "
"assert mpl.rcParams._get('backend') == sentinel; "
"assert mpl.get_backend(auto_select=False) is None; "
"import matplotlib.pyplot; "
"print(matplotlib.get_backend())"],
env=env, text=True, check=True, capture_output=True).stdout
# The actual backend will depend on what's installed, but at least tkagg is
# present.
assert backend.strip().lower() != "agg"
def test_deprecation(monkeypatch):
mpl.rcParams.update(mpl.rcParams.copy()) # Doesn't warn.
# Note that the warning suppression actually arises from the
# iteration over the updater rcParams being protected by
# suppress_matplotlib_deprecation_warning, rather than any explicit check.
@pytest.mark.parametrize("value", [
"best",
1,
"1",
(0.9, .7),
(-0.9, .7),
"(0.9, .7)"
])
def test_rcparams_legend_loc(value):
# rcParams['legend.loc'] should allow any of the following formats.
# if any of these are not allowed, an exception will be raised
# test for gh issue #22338
mpl.rcParams["legend.loc"] = value
@pytest.mark.parametrize("value", [
"best",
1,
(0.9, .7),
(-0.9, .7),
])
def test_rcparams_legend_loc_from_file(tmp_path, value):
# rcParams['legend.loc'] should be settable from matplotlibrc.
# if any of these are not allowed, an exception will be raised.
# test for gh issue #22338
rc_path = tmp_path / "matplotlibrc"
rc_path.write_text(f"legend.loc: {value}")
with mpl.rc_context(fname=rc_path):
assert mpl.rcParams["legend.loc"] == value
@pytest.mark.parametrize("value", [(1, 2, 3), '1, 2, 3', '(1, 2, 3)'])
def test_validate_sketch(value):
mpl.rcParams["path.sketch"] = value
assert mpl.rcParams["path.sketch"] == (1, 2, 3)
assert validate_sketch(value) == (1, 2, 3)
@pytest.mark.parametrize("value", [1, '1', '1 2 3'])
def test_validate_sketch_error(value):
with pytest.raises(ValueError, match="scale, length, randomness"):
validate_sketch(value)
with pytest.raises(ValueError, match="scale, length, randomness"):
mpl.rcParams["path.sketch"] = value
@pytest.mark.parametrize("value", ['1, 2, 3', '(1,2,3)'])
def test_rcparams_path_sketch_from_file(tmp_path, value):
rc_path = tmp_path / "matplotlibrc"
rc_path.write_text(f"path.sketch: {value}")
with mpl.rc_context(fname=rc_path):
assert mpl.rcParams["path.sketch"] == (1, 2, 3)
@pytest.mark.parametrize('group, option, alias, value', [
('lines', 'linewidth', 'lw', 3),
('lines', 'linestyle', 'ls', 'dashed'),
('lines', 'color', 'c', 'white'),
('axes', 'facecolor', 'fc', 'black'),
('figure', 'edgecolor', 'ec', 'magenta'),
('lines', 'markeredgewidth', 'mew', 1.5),
('patch', 'antialiased', 'aa', False),
('font', 'sans-serif', 'sans', ["Verdana"])
])
def test_rc_aliases(group, option, alias, value):
rc_kwargs = {alias: value,}
mpl.rc(group, **rc_kwargs)
rcParams_key = f"{group}.{option}"
assert mpl.rcParams[rcParams_key] == value
def test_all_params_defined_as_code():
assert set(p.name for p in rcsetup._params_list()) == set(mpl.rcParams.keys())
def test_validators_defined_as_code():
for param in rcsetup._params_list():
validator = rcsetup._convert_validator_spec(param.name, param.validator)
assert validator == rcsetup._validators[param.name]
def test_defaults_as_code():
for param in rcsetup._params_list():
if param.name == 'backend':
# backend has special handling and no meaningful default
continue
assert param.default == mpl.rcParamsDefault[param.name], param.name