From 79b8fe5bf99536fb4573c68a438bc26aa54e0359 Mon Sep 17 00:00:00 2001 From: Thomas Robitaille Date: Mon, 3 Apr 2023 11:12:48 +0100 Subject: [PATCH] Set deterministic to True for vector graphics and warn about change to True in future for PNG --- pytest_mpl/plugin.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/pytest_mpl/plugin.py b/pytest_mpl/plugin.py index 625bf99..7b29084 100644 --- a/pytest_mpl/plugin.py +++ b/pytest_mpl/plugin.py @@ -639,12 +639,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 @@ -654,8 +687,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':