Feature Request: Type hint for Fixed Length Homogeneous Sequences
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Description
Feature
A concise way to hint that a sequence of homogenous types is a set length. i.e The items in the sequence are all of the same type, the sequence is iterable, and cannot grow larger or smaller.
Pitch
Currently, the recommended way to add type hints to fixed-length sequences is to use Tuples 1. i.e
def foo(ten_floats: Tuple[float,float,float,float,float,float,float,float,float,float]):
...
For longer lists, this repetition is tedious and prone to error.
It would be nice to have a shorthand for defining homogeneous tuples. e.g
def foo(ten_floats: HomogeneousTuple[float, 10]):
...
Although the name Tuple does not signal intent very well. The real thing that we are trying to signal is that this is a sequence of floats of length 10. It does not necessarily have to be a Tuple, it just has to be iterable, and have 10 items, that cannot grow larger or smaller. If I wanted to pass the function a List, this should also pass static type checkers, as long as it has 10 floats.
Possible Implementation
I am not a core python developer, I have never even looked at the implementation of the typing module, and I'm not even that good of a python developer, so please forgive me if this is nonsense.
In Python 3.10 or newer [2] we will be able to use the unpack operator in subscript. So we might be able to do something like:
_t=[float]*10
def foo(ten_floats: Tuple[*_t]):
...
Using TypeVars, we could generalize this function to accept 10 of any type (as long as they are all the same type)
T=TypeVar('T')
TenTypeVars=[T]*10
def foo(ten_homogeneous_items: Tuple[*TenTypeVars]):
...
Following this logic, perhaps a generic type hint for Fixed Length Homogeneous Sequences could be implemented with
class FixedLengthHomogeneousSequence(Generic[T, N], Tuple[*[T]*N]):
...
FixedLengthHomogeneousSequence is a bit wordy. So perhaps FixedList would be a better name.
[2] (I think this is true). Pylance gave me an error saying this was the case. I think it's related to https://www.python.org/dev/peps/pep-0622/
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation file, test, or entry point is identified in the issue. Start by reviewing the typing module's existing Tuple, TypeVar, and generic sequence behavior, then compare the proposed syntax with the referenced PEP; done would require an agreed type-checking design, implementation location, and tests covering fixed-length homogeneous sequences.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100