title: How to type-annotate a decorator that has an optional parameter?
body: <p>Here's the exact function that I'm trying to type-annotate correctly:</p>
  
  <pre><code>F = TypeVar('F', bound=Callable[..., Any])
  
  def throttle(_func: Optional[F] = None, *, rate: float = 1) -&gt; Union[F, Callable[[F], F]]:
      """Throttles a function call, so that at minimum it can be called every `rate` seconds.
  
      Usage::
  
          # this will enforce the default minimum time of 1 second between function calls
          @throttle
          def ...
  
      or::
  
          # this will enforce a custom minimum time of 2.5 seconds between function calls
          @throttle(rate=2.5)
          def ...
  
      This will raise an error, because `rate=` needs to be specified::
  
          @throttle(5)
          def ...
      """
  
      def decorator(func: F) -&gt; F:
          @functools.wraps(func)
          def wrapper(*args: Any, **kwargs: Any) -&gt; Any:
              time.sleep(rate)
              return func(*args, **kwargs)
  
          return cast(F, wrapper)
  
      if _func is None:
          return decorator
      else:
          return decorator(_func)
  </code></pre>
  
  <p>While I'm not getting any error when putting it through mypy, I'm not convinced that I did the right thing, nor am I sure how I could go about proving it.</p>