python / python/cpython

Lacking description of how to correctly implement indexing syntax support.

Open
#120,802 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Documentation

During a discussion on idea of minimizing requirements for children of collections.abc.Sequence, it was pointed out, that documentation is currently lacking sufficient description on expected behaviour of method __getitem__.

The part in question starts with first note after __length_hint__ description. I started by writing, to see what's missing. Non-trivial changes include:.

  1. Translation of a[1:2, 3:5] into a[(slice(1, 2, None), slice(3, 5, None))].
  2. Because that translation exist, custom behaviour types (like matrix) are also supported.
  3. Slicing should return object of the same type, or at least similar.
  4. If description is good, then note about for loops is unnecessary. (Plus it's outdated, for loops call iter and next, the iterator calls __getitem__).
  5. One object can implement both protocols correctly (at the same time).
  6. Setting __iter__ to None makes iter not fallback to sequence protocol. (And similar point for reversed).
  7. Having ABC as parent, or explicitly assigning methods from them, isn't worst than not defining them at all.
  8. (After coping fallback description from __reversed__, correct that) iter don't need full sequence protocol, just __getitem__ method.
Linked PRs
  • gh-120942
  • gh-122178
  • gh-141124

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Python data model documentation section after the length_hint description, following the linked reference in the issue. Compare the proposed changes with linked PRs gh-120942, gh-122178, and gh-141124; done means the listed getitem, slicing, iteration, and reversed-protocol behaviors are accurately documented and the outdated loop note is resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.