python / python/typing

allow `...` in place of generic parameters

Ouverte
#912 27 commentaires 3 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

topic: feature
Langage dominant
Python
Étoiles
1.8k
Forks
302
Merge moyen
23 h
PR mergées (30 j)
8

Description

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)

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par lire l’issue mypy liée et la documentation de Kotlin sur star-projection. Comparez la syntaxe ... proposée avec les alternatives Any, object et Never, puis déterminez la sémantique du typage et la portée de l’implémentation. La tâche est terminée lorsqu’une spécification approuvée et la prise en charge correspondante dans les outils pertinents de vérification de types sont disponibles.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
devtools
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.