code: def effectful(
      fn: Optional[Callable[_P, _T]] = None, type: Optional[str] = None
  ) -> Callable:
      """
      :param fn: function or callable that performs an effectful computation
      :param str type: the type label of the operation, e.g. `"sample"`
  
      Wrapper for calling :func:`~pyro.poutine.runtime.apply_stack` to apply any active effects.
      """
      if fn is None:
          return functools.partial(effectful, type=type)
  
      if getattr(fn, "_is_effectful", None):
          return fn
  
      assert (
          type is not None
      ), f"must provide a type label for operation {fn}"
      assert type != "message", "cannot use 'message' as keyword"
  
      @functools.wraps(fn)
      def _fn(
          *args: _P.args,
          name: Optional[str] = None,
          infer: Optional[InferDict] = None,
          obs: Optional[_T] = None,
          **kwargs: _P.kwargs,
      ) -> Optional[_T]:
          is_observed = obs is not None
  
          if not am_i_wrapped():
              return fn(*args, **kwargs)
          else:
              msg = Message[_P, _T](
                  type=type,
                  name=name,
                  fn=fn,
                  is_observed=is_observed,
                  args=args,
                  kwargs=kwargs,
                  value=obs,
                  scale=1.0,
                  mask=None,
                  cond_indep_stack=(),
                  done=False,
                  stop=False,
                  continuation=None,
                  infer=infer if infer is not None else {},
              )
              # apply the stack and return its return value
              apply_stack(msg)
              return msg["value"]
  
      _fn._is_effectful = True  # type: ignore[attr-defined]
      return _fn
  
imports: import functools
  from typing import TYPE_CHECKING, Callable, Dict, Generic, List, Optional, Set, Tuple, TypeVar, Union, overload
  import torch
  from typing_extensions import Literal, ParamSpec, TypedDict
  from pyro.params.param_store import _MODULE_NAMESPACE_DIVIDER, ParamStoreDict
label_desc: Use image node_id 6 for calling a global function with example usage: effectful(fn, type) and returns: _fn, functools, fn, fn, msg