refactor: split idisp() into _idisp_matplotlib()/_idisp_opencv() helpers - #97
Merged
Merged
Conversation
idisp() dispatched on matplotlib=True/False inside one 750-line function body. Split the two backend bodies out into separate private helpers so each can be read, tested and fixed in isolation -- prompted by a recent investigation (see fix/image-ctor-drops-maxintval-before-dtype-cast) that had to reason carefully about which of idisp()'s two branches a uint16 display artifact could have come from. Purely mechanical: the shared preamble (plain/block/fps handling) and the full public signature/docstring stay on idisp(), which now just dispatches to _idisp_matplotlib()/_idisp_opencv() with the branch bodies unchanged (verify with `git diff -w`: only ~100 non-whitespace lines actually changed, the rest is the extracted bodies' indentation shifting one level). Also adds tests/base/test_idisp_display_readback.py: genuine pixel-content readback tests for both backends across a dtype matrix (uint8, uint16 full-range, uint16 12-bit narrow-range, float32, bool), complementing the existing "doesn't crash" style coverage in test_io.py. Matplotlib path reads back real rendered pixels via the Agg backend's off-screen buffer; OpenCV path intercepts the array handed to cv2.imshow(), since there's no portable way to read back a native HighGUI window's content across Linux/macOS/Windows CI. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| ErrorProne | 3 high |
| Security | 1 high |
🟢 Metrics 207 complexity · 8 duplication
Metric Results Complexity 207 Duplication 8
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
idisp()dispatched onmatplotlib=True/Falseinside one ~750-line function body. Split the two backend bodies into separate private helpers (_idisp_matplotlib(),_idisp_opencv()) so each can be read, tested and fixed in isolation.idisp()'s two branches a uint16 display artifact could have come from -- turned out to be neither (the corruption was upstream, inImage.__init__), but the exercise made the case for splitting the backends apart.plain/block/fpshandling) and the full public signature/docstring stay onidisp(), which now just dispatches to the two helpers with the branch bodies unchanged. Verify withgit diff -won this PR: only ~100 non-whitespace lines actually changed -- the rest of the diff is the extracted bodies' indentation shifting one level in from the removedif matplotlib: / else:.Tests added
tests/base/test_idisp_display_readback.py-- genuine pixel-content readback tests for both backends across a dtype matrix (uint8,uint16full-range,uint1612-bit narrow-range,float32,bool), complementing the existing "doesn't crash" style coverage already intest_io.py.buffer_rgba()-- headless-safe and deterministic on every platform, no virtual display needed. Sanity-checked manually that this is genuinely sensitive (not a vacuous pass): a 0/128/255 uint8 test image reads back as exactly[0,0,0,255]/[128,128,128,255]/[255,255,255,255]at the corresponding data coordinates.cv2.imshow()-- sincecv2.imshow's own internal 16-bit conversion was independently verified correct via real on-screen pixel readback (screenshot + template matching, during the fix: Image() drops maxintval when dtype= is also given, silently truncating #95 investigation), asserting the correct array reaches it is equivalent to asserting the correct image is displayed.Test plan
ruff checkon the refactored file: same 15 pre-existing findings asorigin/main, no new ones introduced.🤖 Generated with Claude Code