Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variadic Callback Protocols for Non-Variadic Functions (I think?) #10657

Closed
akern40 opened this issue Jun 16, 2021 · 3 comments
Closed

Variadic Callback Protocols for Non-Variadic Functions (I think?) #10657

akern40 opened this issue Jun 16, 2021 · 3 comments
Labels
feature topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@akern40
Copy link

akern40 commented Jun 16, 2021

Feature

Currently, a function with multiple typed arguments does not match a protocol that defines typed variadic arguments. I think many people have a use for typing these functions with a variadic callback protocol, especially when writing wrappers. I believe this is related to #5876, but extends the use of Any to other types.

Pitch

Many times it is useful to define a wrapper function with the signature wrapper(*args: T, **kwargs: Any) -> T, or something similar. This allows you to create a wrapper that is agnostic to the number of positional arguments, as long as they are all of the same type, with optional keyword arguments. This wrapper can then be applied to any number of functions that take in multiple positional arguments of the same type. For example:

from functools import wraps
from typing import Any, Protocol


class IntTransformer(Protocol):
    def __call__(self, *args: int, **kwargs: Any) -> int:
        ...


def incrementer(func: IntTransformer) -> IntTransformer:
    @wraps(func)
    def wrapper(*args: int, **kwargs: Any) -> int:
        for a in args:
            a += 1
        return func(*args, **kwargs)

    return wrapper

# Argument 1 to "incrementer" has incompatible type "Callable[[int, int], int]";  expected "IntTransformer"
@incrementer
def summation(a: int, b: int) -> int:
    return a + b

This capability would extend the proposal in #5876 to still check for the argument types of the non-variadic functions, instead of just looking for Any. I think this would help alleviate typing headaches when trying to implement general wrappers and decorators.

@lsmenicucci
Copy link

I think this can now be achived using ParamSpec right?

@akern40
Copy link
Author

akern40 commented Aug 16, 2021

Yes, I believe that feature would cover it. I think the implementation is discussed in #8645, so once that's ready this issue should be irrelevant.

@JelleZijlstra JelleZijlstra added the topic-paramspec PEP 612, ParamSpec, Concatenate label Mar 19, 2022
@JelleZijlstra
Copy link
Member

Don't think this issue is still relevant, we have specific issues for implementing PEP 612 support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

No branches or pull requests

3 participants