Skip to content

attrs/dataclasses-style decorator helper #324

@kurtbrose

Description

@kurtbrose

There's probably a better name for this, but the idea is to write decorator + kwargs -- so you don't have to do @decorator() if there aren't any arguments being passed.

def maybe_arg_decorator(callable):
  """
  There's this fun trick that dataclasses.dataclass decorator does,
  where you can use it both directly as a decorator, or call it with kwargs and use
  that as the decorator.

  This helper converts a function with 1 positional argument and a bunch of kwargs
  into one of those.


  @maybe_arg_decorator
  def simple_decorator(decorated, a=1, b=2):
     pass

  @simple_decorator
  def thing(): pass

  @simple_decorator(a=3)
  def other_thing(): pass
  """
  @wraps(callable)
  def wrapper(decorated=None, /, **kwargs):
    if decorated:
      return callable(decorated)

    def double_wrapper(decorated, /):
      return callable(decorated, **kwargs)

    return double_wrapper

  return wrapper

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions