Skip to content

Feature Request: Type hint for Fixed Length Homogeneous Sequences #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
craigh92 opened this issue Feb 11, 2021 · 7 comments
Open

Feature Request: Type hint for Fixed Length Homogeneous Sequences #786

craigh92 opened this issue Feb 11, 2021 · 7 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@craigh92
Copy link

craigh92 commented Feb 11, 2021

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/

@jp-larose
Copy link

Experimenting with this idea, I found a possible solution using the existing API.

Summary

Create type-hint for a fixed-length homogeneous tuple directly with types.GenericAlias.

from types import GenericAlias
from typing import TypeVar
T = TypeVar('T')
EightTuple = GenericAlias(tuple, (T,)*8)
eight_ints: EightTuple[int] = (1, 2, 3, 4, 5, 6, 7, 8)
eight_strs: EightTuple[str] = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')

How I got there

At first, I tried manipulating tuples directly:

>>> (int,)*10
(<class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>, <class 'int'>)
>>> from typing import TypeVar
>>> T = TypeVar('T')
>>> TenTuple = (T,)*10
>>> TenTuple
(~T, ~T, ~T, ~T, ~T, ~T, ~T, ~T, ~T, ~T)

However, I got stuck at this point because:

>>> TenTuple[int]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: tuple indices must be integers or slices, not type

I tried other variations on this theme with typing.Generic, and kept getting the same error.

I also made a desperate attempt at a tuple metatype, but that failed miserably.

Then I looked at

>>> type(tuple[int])
<class 'types.GenericAlias'>

Which got me thinking, maybe we can just create our own alias type directly:

>>> from types import GenericAlias
>>> EightTuple = GenericAlias(tuple, (T,)*8)
>>> EightTuple
tuple[~T, ~T, ~T, ~T, ~T, ~T, ~T, ~T]
>>> EightTuple[int]
tuple[int, int, int, int, int, int, int, int]

@srittau srittau added the topic: feature Discussions about new features for Python's type annotations label Nov 4, 2021
@garrison
Copy link

In Julia, this type alias is called NTuple:

julia> NTuple{3,Int}
Tuple{Int64, Int64, Int64}

@Ricyteach
Copy link

Experimenting with this idea, I found a possible solution using the existing API.

Summary

Create type-hint for a fixed-length homogeneous tuple directly with types.GenericAlias.

from types import GenericAlias
from typing import TypeVar
T = TypeVar('T')
EightTuple = GenericAlias(tuple, (T,)*8)
eight_ints: EightTuple[int] = (1, 2, 3, 4, 5, 6, 7, 8)
eight_strs: EightTuple[str] = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')

Did something change? I am getting:

error: Variable "test.EightTuple" is not valid as a type  [valid-type]

(mypy 1.4.1)

@jp-larose
Copy link

Experimenting with this idea, I found a possible solution using the existing API.

Summary

Create type-hint for a fixed-length homogeneous tuple directly with types.GenericAlias.

from types import GenericAlias
from typing import TypeVar
T = TypeVar('T')
EightTuple = GenericAlias(tuple, (T,)*8)
eight_ints: EightTuple[int] = (1, 2, 3, 4, 5, 6, 7, 8)
eight_strs: EightTuple[str] = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')

Did something change? I am getting:

error: Variable "test.EightTuple" is not valid as a type  [valid-type]

(mypy 1.4.1)

This was two years a go, so I don't remember exactly how deep I got with this. I seem to recall only playing around with GenericAlias, but not validating the resulting type in mypy. It would be nice for these to validate though. I suspect the issue is that the code has to execute, and mypy only looks at static code, but I'm not 100% sure on that.

@jacktose
Copy link

A thought on syntax: I just stumbled on the similar foo: tuple[int, int, int] problem, and my naive/intuitive guess was foo: tuple[int]*3 or foo: tuple[int*3], based on the familiar (7,)*3 syntax.

@lou1306
Copy link

lou1306 commented Aug 23, 2024

Given that T1 | T2 | ... | Tn is now valid syntax for union types, I think the natural extension for product types (i.e., tuples) would be T1 & T2 & ... & Tn. For very long homogeneous tuples, I'd say either (T,) * n or T ** n would make sense.

@Shaked-Q
Copy link

Shaked-Q commented Aug 25, 2024

Experimenting with this idea, I found a possible solution using the existing API.

Summary

Create type-hint for a fixed-length homogeneous tuple directly with types.GenericAlias.

from types import GenericAlias
from typing import TypeVar
T = TypeVar('T')
EightTuple = GenericAlias(tuple, (T,)*8)
eight_ints: EightTuple[int] = (1, 2, 3, 4, 5, 6, 7, 8)
eight_strs: EightTuple[str] = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')

This doesn't work for me. This code does not issue a typing warning:

from types import GenericAlias
from typing import TypeVar

T = TypeVar('T')
Pair = GenericAlias(tuple, (T,)*2)

def foo(a: Pair[int]) -> None:
    print(a[0])

foo((1, 3, 3))  
# Expected a typing warning ("Expected type 'tuple[int, int]', got 'tuple[int, int, int]' instead"), but did not get one.
# Changing the type hint of `a` in `foo` to 'tuple[int, int]' does issue this warning, as expected.

I'm using Python 3.10, mypy==1.11.2, PyCharm community 2024.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

8 participants