Support for alternative generic inference algorithms
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 1.8k
- Fork
- 302
- Merge medio
- 23h
- PR unite (30g)
- 8
Descrizione
I believe there is a use case for generic inference that doesn't get as wide as possible.
Look at this example of an assertion function, you would never want to do an assertion between two different types, but there is currently no way to type this:
from typing import TypeVar
T = TypeVar("T")
def assert_something(expected: T, actual: T) -> None:
...
assert_something(1, "") # no error, SUS alert!, T is inferred as `object`
Here are some behaviors from other languages
TypeScript
In Typescript generic inference is narrowed to type level types(not down to instance level types) and is never widened:
function assertSomething<T>(expected: T, actual: T): void { ... }
assertSomething(1, "") // Argument of type 'string' is not assignable to parameter of type 'number'.
Kotlin
Kotlin by default acts the same as Python, but there are annotations for changing the behavior of inference.
NoInfer will exclude that usage from inferring the type.
fun <T> assertSomething(expected: T, actual: @NoInfer T) { }
assertSomething(1, "") // Type mismatch: inferred type is String but Int was expected
Exact will require the type of the parameter is equal at a type level (Number != Int)
fun <T> foo(x: @Exact T) { }
foo<Number>(1) // Type mismatch. Required: Number, Found: Int
OnlyInputTypes will require a type annotation if there is any difference in types between the usages of the generic:
fun <@OnlyInputTypes T> doSomething(a: T, b: T) { }
val a = doSomething("a", "b")
val b = doSomething("a", 1) // Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.
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.
Direzione di ricerca
Inizia confrontando l’esempio Python con i comportamenti di inferenza di TypeScript e Kotlin descritti nell’issue. Definisci quali algoritmi di inferenza alternativi o annotazioni debbano essere supportati e come dovrebbero comportarsi i tipi degli argomenti misti, i tipi esatti e l’inferenza esclusa; il completamento richiederebbe un design condiviso e la relativa specifica di tipizzazione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100