Skip to content

fix: Image[...] with a list of planes raises, and a plane index works on a greyscale image - #309

Open
petercorke wants to merge 3 commits into
mainfrom
bug/image-getitem-keys
Open

petercorke wants to merge 3 commits into
mainfrom
bug/image-getitem-keys

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Problems found while documenting Image.__getitem__:

  • img[[0, 2]] returned a 1x1 image with no error, because a list was read as the pair of keys (0, 2). It now raises ValueError and points to plane().
  • An int key on a greyscale image failed (g[0] gave "bad ndarray passed to Image constructor"). An int key now goes through plane(), so g[0] returns the image.
  • An out-of-range plane index raised NumPy's IndexError; it now raises ValueError, as plane() does.
  • NumPy integers such as np.int64(1) are accepted. True and False are refused, not read as planes 1 and 0.
  • A negative index still counts from the last plane, so img[-1] works as before.

Behavior change to be aware of: img[[0, 2]] and an out-of-range img[5] now raise ValueError. The first used to give a wrong answer.

Nine new tests in tests/test_image_core.py. The full suite passes (2418 passed, 17 skipped) and mypy is clean.

An AI assistant (Claude Code) wrote this change.

🤖 Generated with Claude Code

… on a greyscale image

img[[0, 2]] returned a 1x1 image, because a list was read as a pair of keys.
It now raises ValueError and points to plane().  An int key goes through
plane(), so a greyscale image accepts img[0] and an out-of-range index raises
ValueError, not NumPy's IndexError.  NumPy integers are accepted, a negative
index still counts from the last plane, and True and False are refused.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 20 complexity · 0 duplication

Metric Results
Complexity 20
Duplication 0

View in Codacy

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 branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant