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

Question: Partial callable signature #6077

Closed
mehdigmira opened this issue Dec 17, 2018 · 4 comments
Closed

Question: Partial callable signature #6077

mehdigmira opened this issue Dec 17, 2018 · 4 comments

Comments

@mehdigmira
Copy link

Hello,

When using decorators, we often know that the function that will be decorated should take param x and y, but we don't care about the rest of the parameters. Is there currently a way to do this ?
Something like Callable[[str,...], Any] would be perfect. It would work for f(x: str) but also for f(x: str, y: int) and f(x: str, *args, **kwargs)

@JelleZijlstra
Copy link
Member

You can do this with callable protocols (https://mypy.readthedocs.io/en/latest/protocols.html#callback-protocols).

@mehdigmira
Copy link
Author

I was not able to make it work with callable protocols either

from typing import Any

from typing_extensions import Protocol


class DecoratedF(Protocol):
    def __call__(self, a: str, *args: Any) -> None: ...


def g(f: DecoratedF): ...


@g
def f(a: str, b: str) -> None: ...

Am I doing smth wrong ?

@ilevkivskyi
Copy link
Member

This is actually very similar to #5876 (see also a decorator example there). Which is opposite to the idea of callback protocols in some sense. Callback protocols allow to express signatures that are impossible to express using Callable syntax. However, there is something that can be expressed using Callable that can't be expressed as a signature, for example Callable[..., int] (with literal ...).

Potentially we can just also allow special meaning for *args: Any, **kwds: Any at the end of a signature to mean "I don't care" instead of "Should allow all possible calls".

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 29, 2020

We don't want to support a non-standard extension to the callable syntax, so proposed changes should go through the typing issue tracker or typing-sig@.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants