python / python/typing

allow `...` in place of generic parameters

Aperta
#912 27 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic: feature
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Descrizione

from https://github.com/python/mypy/issues/11389

Feature

in kotlin, you can omit a generic from a type annotation when you don't care what its value is:

class Foo<T: Number>

fun foo(value: Foo<*>) {}

more info:

Pitch

  • it's especially useful for types that have multiple bounded generics

    i think this could be accomplished by simply allowing ... to be used in place of the generics

    Thing1 = TypeVar("Thing1", bound=Base1, covariant=True)
    Thing2 = TypeVar("Thing2", bound=Base2, covariant=True)
    Thing3 = TypeVar("Thing3", bound=Base3, covariant=True)
    
    
    class ThingWithLotsOfGenerics(Generic[Thing1, Thing2, Thing3]):
        ...
    
    
    def foo(value: ThingWithLotsOfGenerics[..., ..., ...]) -> None:
        ...
    
  • Another usage is to ignore variance issues when you don't care about accessing the values.

    @dataclass
    class Box(Generic[T]):
        t: T
    
    def foo(b: Box[...]):
        print(b)
    def bar(b: Box[object]):
        print(b)
    
    b = Box(1)
    foo(b)  # no error
    bar(b)  # error, Box[int] incompatible with Box[object]
    

Alternatives

Use Any

Any removes all type safety so is not a good solution

T = TypeVar("T")

class Foo(Generic[T]):
    a: T

def foo(f: Foo[Any]):
    f.a = "AMONGUS😳"

f = Foo[int]()
foo(f)
Use object/Never

This doesn't work if your TypeVar is bound, you have to specify the bound, which is non-optimal for many reasons.

class Foo: ...

T = TypeVar("T", bound=Foo, covariant=True)


class Bar(Generic[T]):
    ...


# error: Type argument "object" of "Bar" must be a subtype of "Foo"  [type-var]
def foo(value: Bar[object]) -> None:
    ...

(also being tracked in KotlinIsland/basedmypy#30 and https://github.com/DetachHead/basedpyright/issues/18)

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia leggendo l’issue di mypy collegata e la documentazione di Kotlin su star-projection. Confronta la sintassi proposta ... con le alternative Any, object e Never, quindi determina la semantica del tipaggio e l’ambito dell’implementazione. L’attività è completata quando sono disponibili una specifica concordata e il supporto corrispondente negli strumenti pertinenti di type checking.

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
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.