Skip to content

Commit

Permalink
Set deterministic to True for vector graphics and warn about change t…
Browse files Browse the repository at this point in the history
…o True in future for PNG
  • Loading branch information
astrofrog committed Apr 3, 2023
1 parent e6c1308 commit 2e81f56
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions pytest_mpl/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,45 @@ def save_figure(self, item, fig, filename):
filename = str(filename)
compare = get_compare(item)
savefig_kwargs = compare.kwargs.get('savefig_kwargs', {})
deterministic = compare.kwargs.get('deterministic', False)
deterministic = compare.kwargs.get('deterministic', None)

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

extra_rcparams = {}

ext = self._file_extension(item)

if deterministic is None:

# The deterministic option should only matter for hash-based tests,
# so we first check if a hash library is being used

if self.hash_library or compare.kwargs.get('hash_library', None):

if ext == 'png':
if 'metadata' not in savefig_kwargs or 'Software' not in savefig_kwargs['metadata']:
warnings.warn("deterministic option not set (currently defaulting to False), "
"in future this will default to True to give consistent "
"hashes across Matplotlib versions. To suppress this warning, "
"set deterministic to True if you are happy with the future "
"behavior or to False if you want to preserve the old behavior.",
FutureWarning)
else:
# Set to False but in practice because Software is set to a constant value
# by the caller, the output will be deterministic (we don't want to change
# Software to None if the caller set it to e.g. 'test')
deterministic = False
else:
deterministic = True

else:

# We can just default to True since it shouldn't matter and in
# case generated images are somehow used in future to compute
# hashes

deterministic = True

if deterministic:

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

ext = self._file_extension(item)

if ext == 'png':
extra_metadata = {"Software": None}
elif ext == 'pdf':
Expand Down

0 comments on commit 2e81f56

Please sign in to comment.