Skip to content

How to require the first parameter of a Paramspec is a specific type #9678

Answered by erictraut
mike-barry-gmo asked this question in Q&A
Discussion options

You must be logged in to vote

The type system includes a special form called Concatenate. You can use it to prepend positional parameters for a Callable. For documentation, refer to this section of the typing spec.

Here's an updated sample using Concatenate.

from typing import Any, Callable, Concatenate, ParamSpec, TypeVar

TIndex = TypeVar("TIndex")
TReturn = TypeVar("TReturn")
P = ParamSpec("P")

def model(
    reindex_function: Callable[[TIndex], TIndex],
) -> Callable[[Callable[Concatenate[TIndex, P], TReturn]], Callable[Concatenate[TIndex, P], TReturn]]: ...

def sample_reindex_function(index: str) -> str: ...

@model(reindex_function=sample_reindex_function)
def decorated_method(index: str, pos_arg2: int, *, kwarg1

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mike-barry-gmo
Comment options

Answer selected by mike-barry-gmo
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