python / python/typeshed

Typing for callable inputs in itertools.starmap

Ouverte
#14,892 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
Python
Étoiles
5.1k
Forks
2.1k
Merge moyen
1 j 19 h
PR mergées (30 j)
82

Description

The type annotations for itertools.starmap allow any iterables for any function arguments:
https://github.com/python/typeshed/blob/11c7821a79a8ab7e1982f3ab506db16f1c4a22a9/stdlib/itertools.pyi#L112-L116

This means I can do something like:

def myfunc(x: int, y: int):
    return x + y

itertools.starmap(myfunc, [["foo, "bar"]])

and type checkers won't complain.

map gets around this to some extent with overloads for callables with up to 5 arguments (but if you need 6 or more, you're out of luck!)
https://github.com/python/typeshed/blob/11c7821a79a8ab7e1982f3ab506db16f1c4a22a9/stdlib/builtins.pyi#L1629-L1670

I couldn't figure out a way to ge that to work with starmap, though, because as far as I can tell the python type system doesn't have any way of specifying an iterable that returns a particular sequence of types. The best you can do is something like def __new__(cls, function: Callable[[_T1, _T2], T], iterable: Iterable[Iterable[_T1 | _T2]], /), which is still better in my opinion (it prevents invalid argument types), but it doesn't stop you from swapping function arguments.

Is there a way to do this? Or does it just need a new language feature? It would be great if we could do something like

P = ParamSpec("P")
def __new__(cls, function: Callable[P, T], iterable: Iterable[P.args], /)

Guide de contribution

Ouvrir le guide de contribution

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’annotation de itertools.starmap dans stdlib/itertools.pyi autour des lignes 112-116, puis comparez-la aux surcharges de callable pour map dans stdlib/builtins.pyi autour des lignes 1629-1670. Déterminez si la relation demandée entre callable et les entrées peut être exprimée avec le typage actuel de Python, et définissez ce qu’une annotation correcte devrait garantir.

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

Évaluation

Stack technique
python
Domaine
tooling
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.