Skip to content

TST: Use explicit style in all image_comparison calls#31148

Open
QuLogic wants to merge 1 commit intomatplotlib:mainfrom
QuLogic:image-comparison-style
Open

TST: Use explicit style in all image_comparison calls#31148
QuLogic wants to merge 1 commit intomatplotlib:mainfrom
QuLogic:image-comparison-style

Conversation

@QuLogic
Copy link
Copy Markdown
Member

@QuLogic QuLogic commented Feb 13, 2026

PR summary

This makes it so that future tests must use the new style (or explicitly opt in to the old), and in the future, we may return to a default of that one instead.

I used a libcst transform to add the kwarg in alphabetical order, so some bits of the diff might be suboptimal in their succinctness, but they should be all consistent.

Fixes #24716

This is based on #31137.

PR checklist


@image_comparison(['agg_filter.png'], remove_text=True)
@image_comparison(['agg_filter.png'], remove_text=True,
style=('classic', '_classic_test_patch'))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create a new style _classic_test? This feels overly verbose.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that we have a way to create a kind of inheritance that would work like that. If we made a completely new one, we'd be duplicating most of classic with the 2 or 3 lines of _classic_test_patch differing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we dynamically create _classic_test.mplstyle by merging the two files at test startup? Or check it in and write a test that it's just the merge of the two?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the former seems quite plausible.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QuLogic I thought about how to document this for debugging (=I want to manually create the same image as in a test). We basically have to state: When running manually, use ('classic', '_classic_test_patch') instead of '_classic_test'.

That brought me to the idea that instead of doing magic at test setup, we could alternatively rewrite the value '_classic_test' to ('classic', '_classic_test_patch'). Not sure which one is better.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's more than just the style; we do a few other things as part of test setup:

def setup():
# The baseline images are created in this locale, so we should use
# it during all of the tests.
try:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
except locale.Error:
try:
locale.setlocale(locale.LC_ALL, 'English_United States.1252')
except locale.Error:
_log.warning(
"Could not set locale to English/United States. "
"Some date-related tests may fail.")
mpl.use('Agg')
with _api.suppress_matplotlib_deprecation_warning():
mpl.rcdefaults() # Start with all defaults
# These settings *must* be hardcoded for running the comparison tests and
# are not necessarily the default values as specified in rcsetup.py.
set_font_settings_for_testing()
set_reproducibility_for_testing()

The best way to create a test image is by running an image_comparison and looking at the result image.

@QuLogic
Copy link
Copy Markdown
Member Author

QuLogic commented Mar 11, 2026

Note, I've decided to wait until the text-overhaul branch is merged before updating this one, as it will change a lot of tests to use mpl20.

This makes it so that future tests *must* use the new style (or
explicitly opt in to the old), and in the future, we may return to a
default of that one instead.

As a shortcut, the `_classic_test` style is generated from `classic` and
`_classic_test_patch` at test startup.

Fixes matplotlib#24716
@QuLogic QuLogic force-pushed the image-comparison-style branch from 3f4bc2c to de1e5d8 Compare April 10, 2026 19:58
@QuLogic QuLogic added this to the v3.11.0 milestone Apr 10, 2026
@QuLogic
Copy link
Copy Markdown
Member Author

QuLogic commented Apr 10, 2026

Sorry, followup question, is image_comparison public API? Should I write a release note about the style parameter, use our deprecation machinery, or revert that part entirely now that we know all tests have been tagged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TST]: Add classic style to all old image tests.

5 participants