python / python/mypy

Union of ParamSpec?

Ouverte
#17,481 1 commentaire 16 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

feature
Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Métriques de merge des PR
Métriques de PR en attente

Description

Feature

Currently it's possible to concatenate a type to a paramspec to get a new paramspec.

I think I may need the ability to concatenate two paramspecs, like:

P = ParamSpec('P')
Q = ParamSpec('Q')
R = TypeVar("R")
Callable[Concatenate[P, Q], R]

Pitch

I'm trying to describe partial-like functions, I spotted https://github.com/python/mypy/pull/16939 which solves if for functools.partial (which is good) but still not giving a way to describe other partial-like functions.

Currently I can successfully describe the "removal" of one or multiple parameters:

from collections.abc import Callable
from typing import Concatenate, Generic, ParamSpec, TypeVar

P = ParamSpec('P')
Q = ParamSpec('Q')
S = TypeVar('S')
T = TypeVar('T')
U = TypeVar('U')


def substract_one_parameter(given: T, func: Callable[Concatenate[T, P], U]) -> Callable[P, U]:
    def inner(*args: P.args, **kwargs: P.kwargs) -> U:
        return func(given, *args, **kwargs)
    return inner


def substract_two_parameters(first: S, second: T, func: Callable[Concatenate[S, T, P], U]) -> Callable[P, U]:
    def inner(*args: P.args, **kwargs: P.kwargs) -> U:
        return func(first, second, *args, **kwargs)
    return inner


def int_int_int(a: int, b: int) -> int:
    return a + b


c = substract_one_parameter(1, int_int_int)
reveal_type(c)  # def (b: builtins.int) -> builtins.int
print(c(2))


d = substract_two_parameters(1, 2, int_int_int)
reveal_type(d)  # def () -> builtins.int
print(d())

But what I think I need is:

# P are the "removed" parameters
# Q are the "kept" parameters
def substract_n_parameters(func: Callable[Concatenate[P, Q], U], *outer_args: P.args, **outer_kwargs: P.kwargs) -> Callable[Q, U]:
    def inner(*args: Q.args, **kwargs: Q.kwargs) -> U:
        return func(*outer_args, *args, **outer_kwargs, **kwargs)
    return inner

I feel that would cleanly express the "removal" of P from [P, Q], without the need to introduce a new function like Difference[P, Q].

I feel like this could also be used for the real functools.partial, maybe simplifying the current (working, yeah) implementation.

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

Aucun fichier source ni test n’est indiqué. Commencez par comparer le comportement existant de ParamSpec et Concatenate avec les exemples fournis, puis examinez l’implémentation référencée de functools.partial et la pull request. Le travail est terminé lorsqu’il est déterminé si deux ParamSpecs peuvent être concaténés et que le comportement de typage proposé de substract_n_parameters est validé.

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

Évaluation

Stack technique
python
Domaine
developer-experience, devtools
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

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