3 ms·
The type that gets Unpack[]'d needs to inherit from TypeDict, unless I'm misunderstanding what you mean.
by squeaky-clean 3y ago
The type that gets Unpack[]'d needs to inherit from TypeDict, unless I'm misunderstanding what you mean.
- dagmx 3y agoBasically I was hoping to use it for functions that wrap others. E.g setup something and then pass the kwargs through to the other function.
- benkuykendall 3y agoIf I understand you correctly, I think ParamSpec (since 3.10) is what you are looking for, especially if you want to be generic over the type of the inner function. The example from the docs (https://docs.python.org/3/library/typing.html#typing.ParamSpec https://docs.python.org/3/library/typing.html#typing.ParamSp...): from collections.abc import Callable from typing import TypeVar, ParamSpec import logging T = TypeVar('T') P = ParamSpec('P') def add_logging(f: Callable[P, T]) -> Callable[P, T]: '''A type-safe decorator to add logging to a function.''' def inner(*args: P.args, **kwargs: P.kwargs) -> T: logging.info(f'{f.__name__} was called') return f(*args, **kwargs) return inner @add_logging def add_two(x: float, y: float) -> float: '''Add two numbers together.''' return x + y
- dagmx 3y agoHmm thanks. I think that might be slightly different but I’ll try it out and see.