python / python/typing

allow `...` in place of generic parameters

Offen
#912 27 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic: feature
Vorherrschende Sprache
Python
Sterne
1.8k
Forks
302
Ø Merge
23 Std.
Gemergte PRs (30 T.)
8

Beschreibung

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)

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Lies zunächst das verlinkte mypy-Issue und die Kotlin-Dokumentation zu star-projection. Vergleiche die vorgeschlagene Syntax ... mit den Alternativen Any, object und Never und bestimme anschließend die Typisierungssemantik und den Implementierungsumfang. Die Aufgabe ist abgeschlossen, wenn eine abgestimmte Spezifikation und entsprechende Unterstützung in den relevanten Werkzeugen zur Typprüfung vorhanden sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.