python / python/mypy

Tuple-like Sequence

Open
#8,441 4 comments 19 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature needs discussion priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

There are situations where a variable can be either a list or a tuple (or more generally, a sequence), but it is used/unpacked in a tuple-like way. That is, the length is fixed and types are possibly heterogeneous. A practical example of this is mpi4py's buffer specifications.

I don't believe it's possible to express a fixed-length heterogeneous sequence without using Tuple and forcing a specific type/inheritance. It'd be useful to have an abstract type that expresses tuple semantics without the inheritance bounds. So the analogy is roughly: List is to Sequence as Tuple is to FixedSequence (or whatever name we choose).

def foo(x: FixedSequence[int, str]) -> int:
    return x[0]

foo((0, ''))
foo([0, ''])

Currently I think it's necessary to do Sequence[Union[int, str]] and this loses both the fixed length and the ordering of the elements in the sequence.

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

Review the requested FixedSequence abstraction and compare its semantics with Tuple and Sequence. Done would preserve fixed length, heterogeneous element ordering, and support both tuple and list values as shown in the issue examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.