Skip to content

Commit 2e81f56

Browse files
committed
Set deterministic to True for vector graphics and warn about change to True in future for PNG
1 parent e6c1308 commit 2e81f56

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

pytest_mpl/plugin.py

+34-3
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,45 @@ def save_figure(self, item, fig, filename):
551551
filename = str(filename)
552552
compare = get_compare(item)
553553
savefig_kwargs = compare.kwargs.get('savefig_kwargs', {})
554-
deterministic = compare.kwargs.get('deterministic', False)
554+
deterministic = compare.kwargs.get('deterministic', None)
555555

556556
original_source_date_epoch = os.environ.get('SOURCE_DATE_EPOCH', None)
557557

558558
extra_rcparams = {}
559559

560+
ext = self._file_extension(item)
561+
562+
if deterministic is None:
563+
564+
# The deterministic option should only matter for hash-based tests,
565+
# so we first check if a hash library is being used
566+
567+
if self.hash_library or compare.kwargs.get('hash_library', None):
568+
569+
if ext == 'png':
570+
if 'metadata' not in savefig_kwargs or 'Software' not in savefig_kwargs['metadata']:
571+
warnings.warn("deterministic option not set (currently defaulting to False), "
572+
"in future this will default to True to give consistent "
573+
"hashes across Matplotlib versions. To suppress this warning, "
574+
"set deterministic to True if you are happy with the future "
575+
"behavior or to False if you want to preserve the old behavior.",
576+
FutureWarning)
577+
else:
578+
# Set to False but in practice because Software is set to a constant value
579+
# by the caller, the output will be deterministic (we don't want to change
580+
# Software to None if the caller set it to e.g. 'test')
581+
deterministic = False
582+
else:
583+
deterministic = True
584+
585+
else:
586+
587+
# We can just default to True since it shouldn't matter and in
588+
# case generated images are somehow used in future to compute
589+
# hashes
590+
591+
deterministic = True
592+
560593
if deterministic:
561594

562595
# Make sure we don't modify the original dictionary in case is a common
@@ -566,8 +599,6 @@ def save_figure(self, item, fig, filename):
566599
if 'metadata' not in savefig_kwargs:
567600
savefig_kwargs['metadata'] = {}
568601

569-
ext = self._file_extension(item)
570-
571602
if ext == 'png':
572603
extra_metadata = {"Software": None}
573604
elif ext == 'pdf':

0 commit comments

Comments
 (0)