Skip to content

Commit 2bcec28

Browse files
tacaswellQuLogic
authored andcommitted
MNT: improve error messages on bad pdf metadata input
Tell the user what keys and types are expected.
1 parent f4982f1 commit 2bcec28

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,24 @@ def check_trapped(x):
195195
return x in ('True', 'False', 'Unknown')
196196

197197
keywords = {
198-
'Title': is_string_like,
199-
'Author': is_string_like,
200-
'Subject': is_string_like,
201-
'Keywords': is_string_like,
202-
'Creator': is_string_like,
203-
'Producer': is_string_like,
204-
'CreationDate': is_date,
205-
'ModDate': is_date,
206-
'Trapped': check_trapped,
198+
'Title': (is_string_like, 'an instance of str'),
199+
'Author': (is_string_like, 'an instance of str'),
200+
'Subject': (is_string_like, 'an instance of str'),
201+
'Keywords': (is_string_like, 'an instance of str'),
202+
'Creator': (is_string_like, 'an instance of str'),
203+
'Producer': (is_string_like, 'an instance of str'),
204+
'CreationDate': (is_date, 'an instance of datetime.datetime'),
205+
'ModDate': (is_date, 'an instance of datetime.datetime'),
206+
'Trapped': (check_trapped, 'one of {"True", "False", "Unknown"}'),
207207
}
208208
for k in info:
209209
if k not in keywords:
210-
cbook._warn_external(f'Unknown infodict keyword: {k}')
211-
elif not keywords[k](info[k]):
212-
cbook._warn_external(f'Bad value for infodict keyword {k}')
210+
cbook._warn_external(f'Unknown infodict keyword: {k!r}. '
211+
f'Must be one of {set(keywords)!r}.')
212+
elif not keywords[k][0](info[k]):
213+
cbook._warn_external(f'Bad value for infodict keyword {k}. '
214+
f'Got {info[k]!r} which is not '
215+
f'{keywords[k][1]}.')
213216
if 'Trapped' in info:
214217
info['Trapped'] = Name(info['Trapped'])
215218

0 commit comments

Comments
 (0)