Hacker News new | past | comments | ask | show | jobs | submit login

If 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.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



Hmm thanks. I think that might be slightly different but I’ll try it out and see.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: