code: def _dataclass(
      cls,
      init=True,
      repr=True,
      eq=True,
      order=False,
      unsafe_hash=False,
      frozen=True,
  ):
      class Config:
          arbitrary_types_allowed = True
  
      # make `cls` a dataclass
      pydantic_dataclass(
          init=init,
          repr=repr,
          eq=eq,
          order=order,
          unsafe_hash=unsafe_hash,
          frozen=frozen,
          config=Config,
      )(cls)
  
      model_fields = {}
      orelse_fields = {}
      eventual_fields = set()
  
      # Collect init args to pass to pydantic model
      # Also, collect eventual and orelse fields
      for name, field in cls.__dataclass_fields__.items():
          if not field.init:
              continue
  
          if field.default is EVENTUAL:
              eventual_fields.add(name)
              continue
  
          elif isinstance(field.default, OrElse):
              orelse_fields[name] = field.default
              continue
  
          # now let's look at the type
          ty = field.type
  
          # skip ClassVar
          if (
              ty is ClassVar
              # `ClassVar[X].__origin__ is ClassVar`
              or getattr(ty, "__origin__", None) is ClassVar
          ):
              continue
  
          # if InitVar is used, we extract the inner value
          if ty is dataclasses.InitVar:
              ty = Any
          elif isinstance(ty, dataclasses.InitVar):
              ty = ty.type
  
          # do we have a default value set
          if not isinstance(field.default, dataclasses._MISSING_TYPE):
              model_fields[name] = ty, pydantic.Field(
                  default=field.default
              )
          # do we have a default_factory set
          elif not isinstance(
              field.default_factory, dataclasses._MISSING_TYPE
          ):
              model_fields[name] = ty, pydantic.Field(
                  default_factory=field.default_factory
              )
          # no default
          else:
              model_fields[name] = ty, ...
  
      # figure out in which order to resolve `OrElse`, since they can depend on
      # each other -- however, no cycles!
      orelse_order = {}
  
      if orelse_fields:
          available_fields = set(cls.__dataclass_fields__)
  
          unhandled = dict(orelse_fields)
  
          while unhandled:
              for orelse_name, orelse in unhandled.items():
                  params = orelse.parameters
  
                  if params <= available_fields:
                      available_fields.add(orelse_name)
                      orelse_order[orelse_name] = orelse
                      del unhandled[orelse_name]
                      break
              else:
                  # we made one pass over unhandled without resolving anything
                  raise ValueError(
                      f"Can not resolve dependencies of {unhandled}"
                  )
  
      model = pydantic.create_model(
          f"{cls.__name__}Model", __config__=Config, **model_fields
      )
  
      orig_init = cls.__init__
  
      def _init_(self, *args, **input_kwargs):
          if args:
              raise TypeError("serde.dataclass is always `kw_only`.")
  
          validated_model = model(**input_kwargs)
          # .dict() turns its values into dicts as well, so we just get the
          # attributes directly from the model
          init_kwargs = {
              key: getattr(validated_model, key)
              for key in validated_model.dict()
          }
  
          for orelse_name, orelse in orelse_order.items():
              value = orelse._call(init_kwargs)
              init_kwargs[orelse_name] = value
  
          if eventual_fields:
              eventual_kwargs = {
                  key: Eventual(input_kwargs.get(key, EVENTUAL))
                  for key in eventual_fields
              }
  
              self.__class__.__eventually__(
                  SimpleNamespace(**init_kwargs),
                  **eventual_kwargs,
              )
  
              for name, value in eventual_kwargs.items():
                  eventual_kwargs[name] = value.unwrap()
  
              init_kwargs.update(eventual_kwargs)
  
          self.__init_kwargs__ = init_kwargs
  
          self.__init_passed_kwargs__ = select(
              input_kwargs,
              init_kwargs,
              # We want to ignore additional kwargs passed, which are not used by
              # the class.
              ignore_missing=True,
          )
  
          orig_init(self, **init_kwargs)
  
      cls.__init__ = _init_
      return cls
  
imports: import dataclasses
  import inspect
  from types import SimpleNamespace
  from typing import TYPE_CHECKING, cast, Any, Callable, ClassVar, Generic, TypeVar
  from gluonts.itertools import select
  from gluonts.pydantic import pydantic, dataclass
label_desc: Use image node_id 2 for calling a global function with example usage: _dataclass(cls, init, repr, eq, order, unsafe_hash, frozen) and returns: cls