Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created December 11, 2024 15:53
Show Gist options
  • Save mypy-play/950f9a8265f17e47fa35de5785208bab to your computer and use it in GitHub Desktop.
Save mypy-play/950f9a8265f17e47fa35de5785208bab to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import Protocol, ParamSpec, Callable, Concatenate
P = ParamSpec('P')
class FuncType(Protocol[P]):
def __call__(self, *args: P.args, x: str, **kwargs: P.kwargs) -> str: ...
def wrapper(fp: Callable[P, str]) -> FuncType[P]:
def inner(*args: P.args, x: str, **kwargs: P.kwargs) -> str:
print(x)
return fp(*args, **kwargs)
return inner
def my_f(s: str, d: float) -> str:
return f"{s}, {d}"
wrapped = wrapper(my_f)
wrapped(1.2, x="works?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment