Skip to content

Simplify test code - #9967

Merged
hugovk merged 1 commit into
python-pillow:mainfrom
radarhere:assert
Sep 7, 2026
Merged

Simplify test code#9967
hugovk merged 1 commit into
python-pillow:mainfrom
radarhere:assert

Conversation

@radarhere

@radarhere radarhere commented Sep 7, 2026

Copy link
Copy Markdown
Member

Given

Pillow/Tests/helper.py

Lines 87 to 90 in d3b755d

def assert_image_equal(a: Image.Image, b: Image.Image, msg: str | None = None) -> None:
assert a.mode == b.mode, msg or f"got mode {repr(a.mode)}, expected {repr(b.mode)}"
assert a.size == b.size, msg or f"got size {repr(a.size)}, expected {repr(b.size)}"
if a.tobytes() != b.tobytes():

Pillow/Tests/helper.py

Lines 112 to 116 in d3b755d

def assert_image_similar(
a: Image.Image, b: Image.Image, epsilon: float, msg: str | None = None
) -> None:
assert a.mode == b.mode, msg or f"got mode {repr(a.mode)}, expected {repr(b.mode)}"
assert a.size == b.size, msg or f"got size {repr(a.size)}, expected {repr(b.size)}"

looking at
with pytest.raises(pytest.fail.Exception):
assert_image_equal(ref, im)
assert_image_similar(ref, im, epsilon)

assert_image_similar asserts that the mode and size are the same, meaning that assert_image_equal should only fail when comparing im.tobytes().

So let's just simplify things by testing im.tobytes() directly. I find this clearer.

assert_image_similar(ref, im, epsilon)
assert ref.tobytes() != im.tobytes()

@akx akx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It does read simpler, yep!

I wonder how much Image.__eq__ is being used in real code in the wild. 53c9a80 changed it to short-circuit but it's still (like this code) paying for serializing both images to bytes (#9938 😉) even if, say, the first byte differs.

@hugovk hugovk changed the title Simplify code Simplify test code Sep 7, 2026
@hugovk
hugovk merged commit e51fe05 into python-pillow:main Sep 7, 2026
48 checks passed
@radarhere
radarhere deleted the assert branch September 7, 2026 06:47
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.

3 participants