@functools.wraps(func, assigned=('__module__', '__name__', '__doc__')) async def wrapper(*args, **kwargs): return await func(*args, **kwargs)
Decorators & Metaclasses

Meaning

A decorator factory that creates an async wrapper preserving the original function's __module__, __name__, and __doc__ attributes via functools.wraps.

Primary Function

Preserves metadata of the original async function when used as a decorator.

Communicative Purpose

Signals that the wrapper is transparent and preserves the original function's identity for introspection and debugging.

Pattern

@functools.wraps(func, assigned=('__module__', '__name__', '__doc__')) async def wrapper(*args, **kwargs): return await func(*args, **kwargs)

Core Structure

@functools.wraps(func, assigned=('__module__', '__name__', '__doc__')) async def wrapper(*args, **kwargs): return await func(*args, **kwargs)

Função primária

Preserves metadata of the original async function when used as a decorator.

Propósito comunicativo

Signals that the wrapper is transparent and preserves the original function's identity for introspection and debugging.

Situações de gatilho

When creating an async decorator that needs to maintain the wrapped function's metadata for debugging, introspection, or framework compatibility.

Contextos

Used inside async decorator definitions, e.g., @decorator async def wrapper(*args, **kwargs): return await func(*args, **kwargs)

Padrão

@functools.wraps(func, assigned=('__module__', '__name__', '__doc__')) async def wrapper(*args, **kwargs): return await func(*args, **kwargs)

Estrutura central

@functools.wraps(func, assigned=('__module__', '__name__', '__doc__')) async def wrapper(*args, **kwargs): return await func(*args, **kwargs)

Slots de substituição

func, *args, **kwargs

Colocados típicos

  • functools
  • wraps
  • async
  • def
  • wrapper
  • await
  • func
  • args
  • kwargs

Substituições comuns

  • assigned=('__module__'
  • '__name__'
  • '__doc__'
  • '__qualname__')
  • assigned=('__module__'
  • '__name__'
  • '__doc__'
  • '__annotations__')

Erros comuns

Forgetting to await the wrapped function (returning func(*args, **kwargs) instead of await func(*args, **kwargs)); omitting *args/**kwargs; forgetting to import functools.

Similar / contraste

Sync wrapper using functools.wraps without async; using functools.wraps without assigned tuple (defaults to __module__, __name__, __doc__, __annotations__); using wraps without assigned argument.

Interferências

Forgetting to await the wrapped function results in returning a coroutine object instead of awaiting its result, causing runtime errors.

Família do chunk

  • functools.wraps
  • async decorator
  • decorator pattern

Nuance

Preserves only the specified attributes; other attributes like __annotations__ or __qualname__ are not preserved unless added to assigned.

Efeito pragmático

Signals that the decorator is transparent and does not alter the wrapped function's identity, aiding introspection and debugging.

Dica de memória

@functools.wraps with assigned module name doc

Upgrade path

functools.wraps with assigned tuple including __qualname__ and __annotations__

Tipo de construção: idiomTag de espaçamento: Medium-termIdioma?: Sim

Log in to save chunks.