Skip to content

fix(tree): support slices and reverse iteration - #2320

Open
gavenren wants to merge 1 commit into
gitpython-developers:mainfrom
gavenren:fix/tree-sequence-access
Open

gavenren wants to merge 1 commit into
gitpython-developers:mainfrom
gavenren:fix/tree-sequence-access

Conversation

@gavenren

Copy link
Copy Markdown

Both tree[:2] and reversed(tree) raise TypeError on Python 3. Slice handling exists only in the legacy __getslice__ method, while __reversed__ tries to reverse a generator.

Handle slices in __getitem__ using the existing cached-entry conversion, and reverse the cached entries before converting them lazily. Add precise return-type overloads, document both operations, and verify object types, paths, modes, IDs and unchanged source-tree contents. This groups the two broken Tree sequence operations in one change.

Motivation: Found while inspecting Tree's sequence helpers during caller validation for #2319, as part of @gavenren's requested open-source contribution work. The implementation reuses the existing conversion of Git/Gix results; it adds no independent Git storage behavior.

Validation (Linux, Python 3.12.14, Git 2.52.0, GixPython 0.3.0):

  • All eight new regression cases fail with the original CLI implementation.
  • GIT_PYTHON_BACKEND=cli .venv/bin/python test/run-local.py test/test_tree.py test/test_typing.py --no-cov: 110 passed.
  • The same complete modules with GIT_PYTHON_BACKEND=gix: 110 passed.
  • Covers whole/partial/strided/negative/out-of-range slices, zero-step rejection, independent result lists, reverse ordering and nested repository-relative paths.
  • mypy: no issues in 40 source files.
  • basedpyright: zero errors or warnings, including the new slice/index result-type assignments.
  • Repository pre-commit hooks for all three changed files: passed, including Ruff check/format.
  • git diff --check: passed.
  • Full suites and other platforms were not run.

Agent identification: This PR, implementation, tests, commit and description were prepared autonomously by OpenAI Codex in ChatGPT Work Mode, communicating through @gavenren's account. The exact underlying model and reasoning setting are unavailable. No human review, approval or manual testing is claimed.

Python 3 sends slices to `Tree.__getitem__`, but its legacy slice implementation only exists in `__getslice__`. `tree[:2]` therefore raises `TypeError`. `reversed(tree)` also raises because it applies `reversed` to a generator.

Handle slices through the existing cache-to-object conversion and reverse cached entries before lazy conversion. Add return-type overloads, public API documentation and regressions for ordering, stride, negative/empty ranges, zero steps, independent results and nested paths.

Validation: all eight new cases fail on the original CLI implementation. Afterward, the complete tree and typing modules pass 110 tests on each CLI/Gix backend with Python 3.12.14, Git 2.52.0 and GixPython 0.3.0 on Linux. Mypy (40 source files), basedpyright and applicable repository pre-commit hooks pass. Full suites and other platforms were not run.

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 block approval of the Tree slicing and reverse-iteration changes.

No open issues 🎉

What changed in this PR

This PR fixes two broken Tree sequence operations on Python 3, reusing the existing cached-entry conversion.

Changes:

  • Support slicing with independent list results and reverse iteration with lazy object conversion.
  • Document both operations and add precise return-type overloads.
  • Add regression coverage for slice behavior, object metadata, nested paths, and unchanged source-tree contents.

Reviewer: GitHub Copilot.

File Description
test/​test_typing.py Adds type-checking assignments for indexing, slicing, and reverse iteration.
test/​test_tree.py Tests slice semantics, zero-step rejection, reverse order, and object preservation.
git/​objects/​tree.py Implements slicing and fixes reverse iteration; adds documentation and overloads.

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