Skip to content

How to Type Hint part of *args #1779

Answered by erictraut
hmc-cs-mdrissi asked this question in Q&A
Discussion options

You must be logged in to vote

If I understand you correctly, the signature would be this:

def f(_p0: int, *args: Any, **kwargs: Any) -> int:
    ...

Or are you asking how to specify this as a Callable type annotation? Callable doesn't allow this combination. (You can specify Callable[..., int], but there's no way to indicate that the first parameter should be an int.)

Although Callable is somewhat limiting, you can escape those limits by using a callback protocol. It would look like this:

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

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@hmc-cs-mdrissi
Comment options

@erictraut
Comment options

Answer selected by hmc-cs-mdrissi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants