Feature Request: Type hint for Fixed Length Homogeneous Sequences
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Anfängerfreundlichkeit
- 20/100
Rechercherichtung
Im Issue wird keine Implementierungsdatei, kein Test und kein Einstiegspunkt angegeben. Beginne damit, das bestehende Verhalten des Moduls typing für Tuple, TypeVar und generische Sequenzen zu überprüfen, und vergleiche dann die vorgeschlagene Syntax mit dem referenzierten PEP; abgeschlossen wäre die Arbeit, wenn ein abgestimmtes Design für die Typprüfung, ein Implementierungsort und Tests für homogene Sequenzen fester Länge vorhanden sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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/
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/typing
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
-
topic: conformance tests topic: typing spec
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 72/100
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
xinnan-tech/xiaozhi-fde-talk#263 ·
-
rules
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
huggingface/Repo2RLEnv#163 · 1 Kommentar ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 95/100
huggingface/sentence-transformers#4074 ·
-
comp/dashboard invalid P3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
NousResearch/hermes-agent#121143 ·