Feature Request: Type hint for Fixed Length Homogeneous Sequences
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 20/100
Direzione di ricerca
Nell’issue non sono identificati alcun file di implementazione, test o punto di ingresso. Inizia esaminando il comportamento esistente del modulo typing per Tuple, TypeVar e le sequenze generiche, quindi confronta la sintassi proposta con il PEP di riferimento; il lavoro sarà completato quando saranno definiti un design condiviso per il type checking, una posizione per l’implementazione e test che coprano sequenze omogenee di lunghezza fissa.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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/
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/typing
-
topic: typing spec
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
topic: typing spec
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
topic: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
topic: documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
topic: conformance tests topic: typing spec
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
Tutte le issue di python/typing
Issue simili
-
from:qa priority:P2 reliability tech-debt
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
spec-kitty/spec-kitty#4874 ·
-
fix: inaccuracy ⚠️
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
uabrc/uabrc.github.io#1255 · 1 commento ·
-
kind:bug needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
-
docs
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
ethereum-optimism/factory#64 ·