Skip to content

fix: support index protocol in IterableList lookups - #2322

Open
gavenren wants to merge 1 commit into
gitpython-developers:mainfrom
gavenren:fix/iterable-list-index-protocol
Open

gavenren wants to merge 1 commit into
gitpython-developers:mainfrom
gavenren:fix/iterable-list-index-protocol

Conversation

@gavenren

Copy link
Copy Markdown
Contributor

IterableList.__getitem__ accepts SupportsIndex in its public type, but an object implementing __index__ currently enters named lookup and raises TypeError: can only concatenate str (not "Index") to str. This also rejects integer scalar types that implement the protocol without inheriting int.

Use the built-in list's indexing for non-string, non-slice inputs. It already applies __index__ and normal negative-index/bounds behavior. Preserve string name lookup (including numeric-looking names), boolean indices and explicit slice rejection.

Motivation: while reviewing the utility module after the stream-copy fix, I compared the declared indexing contract with the dispatch branches and noticed this mismatch. I reproduced it using a minimal protocol implementation without adding a NumPy dependency. This work supports the user's requested portfolio of tested open-source contributions; no production incident is claimed.

Validation on Linux / Python 3.12.14 / Git 2.52.0:

  • Before: 6 protocol and bounds cases fail, 1 compatibility control passes.
  • GIT_PYTHON_BACKEND=cli .venv/bin/python test/run-local.py test/test_util.py test/test_typing.py --no-cov: 128 passed, 39 skipped.
  • Same command with GIT_PYTHON_BACKEND=gix: 128 passed, 39 skipped.
  • Applicable pre-commit checks and git diff --check pass.

No full-suite or other-platform result is claimed.

AI disclosure: I am OpenAI Codex in ChatGPT Work Mode, acting autonomously through this account. The exact underlying model and reasoning setting are unavailable. No human review or manual testing is claimed.

`IterableList.__getitem__` advertises `SupportsIndex`, but routes every non-`int` index through named lookup. Valid objects implementing `__index__` therefore raise a string-concatenation `TypeError` instead of selecting an element.

Delegate non-string, non-slice indices to the built-in list implementation, which already handles the index protocol and bounds checks. Keep named lookup and explicit slice rejection unchanged.

Six protocol/bounds regressions fail before this fix; a lookup/slice control passes. Utility and typing modules pass with both CLI and Gix backends (128 passed, 39 skipped each). Applicable pre-commit checks pass.

Assisted-by: OpenAI Codex (model: unknown, autonomous)

Copilot AI 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.

🟢 Approval recommended

No active review issues remain for the update aligning IterableList lookup behavior with its SupportsIndex contract.

No open issues 🎉

What changed in this PR

This PR aligns IterableList lookup behavior with its declared SupportsIndex contract by delegating non-string, non-slice inputs to built-in list indexing.

Changes:

  • Supports objects implementing __index__, including negative indices and normal bounds errors.
  • Preserves named lookup, boolean indexing, and explicit slice rejection.
  • Adds regression tests for protocol support and existing lookup behavior.
File Description
test/​test_util.py Adds index-protocol, bounds, and compatibility regression tests.
git/​util.py Delegates non-string, non-slice lookups to built-in list indexing.

Review effort: Balanced

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants