Add tests for remap_palette()'s explicit source_palette argument - #9867
Add tests for remap_palette()'s explicit source_palette argument#9867pranjalm37 wants to merge 1 commit into
Conversation
The existing remap_palette tests only cover the source_palette=None path, so the length-based palette-mode detection for an explicitly passed source_palette had no coverage at all -- including at the 768-byte boundary between a full 256-entry RGB palette and an RGBA one. Covers both sides of that boundary, pinning the current (correct) behaviour: 768 bytes stays RGB, 1024 bytes is detected as RGBA.
I think the issue wanted us to write tests to check the poster's understanding. If their understanding was incorrect, which we've established it was, I don't think they necessarily felt a need to update our test suite. Do you yourself feel this is a valuable addition?
There is currently no direct test for If you go to https://app.codecov.io/gh/python-pillow/Pillow/blob/main/src%2FPIL%2FImage.py#L2204, you will see that codecov identifies all code within this method as already covered. |
| # length, so a full 256-entry RGB palette (exactly 768 bytes) must stay | ||
| # RGB while a 256-entry RGBA one (1024 bytes) is detected as RGBA. | ||
| source_palette = bytes( | ||
| (entry + channel * 17) % 256 |
There was a problem hiding this comment.
Could you explain this line? It seems like any palette would have sufficed.
|
You're right on both counts, and I should have checked branch coverage instead of assuming from the absence of a direct test. I ran the existing suite (without this PR) under
Honestly: less than I claimed, now that I've seen it's indirectly covered. What it still adds is a direct, boundary-pinned test of the public Your call — happy to close this if you'd rather not carry it, no hard feelings either way. |
Fixes #9865.
#9865 suggests that
remap_palette()'s palette-mode detection is off by one and should use>=instead of>:Pillow/src/PIL/Image.py
Lines 2234 to 2236 in c8c74c8
I looked into it, and the current
>is correct — changing it to>=would be a regression. The issue asked for tests, so this PR adds them rather than changing the behaviour.Why
>is rightA full 256-entry palette is 768 bytes in RGB and 1024 bytes in RGBA, so the boundary sits at 768, not above it:
> 768With
>=, the first row flips to RGBA and a completely ordinary 256-colour RGB palette gets re-read 4 bytes at a time, shifting every colour. Verified against 12.3.0:There is a real ambiguity underneath the report — a 192-entry RGBA palette is also exactly 768 bytes, and is read as RGB. But that's inherent to inferring the mode from a bare
byteslength, and no choice of comparison operator fixes it; distinguishing those two cases would need the caller to say which mode it means. That seems like a separate API question, so I've left it alone here.Changes proposed in this pull request
test_remap_palette_source_palette, parametrized over RGB/RGBA, coveringremap_palette()'ssource_paletteargument. The existingtest_remap_paletteonly exercises thesource_palette=Nonepath, so this branch had no coverage at all.>=(confirmed: the RGB case fails with that edit, and passes onmainas-is).Full
Tests/test_image.pypasses (185 passed, 1 skipped — IPython not installed locally).ruff check/ruff format --checkclean;mypyreports no new errors (the 4 it emits are pre-existing missing-IPython-stub errors, identical on a clean tree).🤖 Generated with Claude Code