defslotted(# noqa: C901_cls:_ClsT|None=None,*,dict:bool=False,weakref:bool=True,)->Callable[[_ClsT],_ClsT]|_ClsT:"""Decorator to create a "slotted" version of the provided class. Args: _cls: The class to decorate. dict: Whether to add a slot for `__dict__`. weakref: Whether to add a slot for `__weakref__`. Warning: This function returns new class object as it's not possible to add `__slots__` after class creation. See Also: - [dataslots](https://github.com/starhel/dataslots/blob/master/src/dataslots/__init__.py) """def_slots_setstate(self,state):forparam_dictinfilter(None,state):forslot,valueinparam_dict.items():object.__setattr__(self,slot,value)defwrap(cls):key=repr(cls)ifkeyin_stack:# pragma: no coverraiseTypeError(f"{cls!r} uses a custom metaclass {cls.__class__!r} ""which is not compatible with automatic slots. ""See Issue !typical#104 on GitHub for more information.")fromNone_stack.add(key)if(sys.version_info>=(3,10)andconstants.PKG_NAMEnotincls.__module__):# pragma: no coverwarnings.warn(f"You are using Python {sys.version}. ""Python 3.10 introduced native support for slotted dataclasses. ""This is the preferred method for adding slots.",stacklevel=2,)cls_dict={**cls.__dict__}# Create only missing slotsinherited_slots=set().union(*(getattr(c,"__slots__",())forcincls.mro()))field_names={f.name:...forfindataclasses.fields(cls)iff.name}ifdict:field_names["__dict__"]=...ifweakref:field_names["__weakref__"]=...cls_dict["__slots__"]=(*(fforfinfield_namesiffnotininherited_slots),)# Erase filed names from class __dict__forfinfield_names:cls_dict.pop(f,None)# Erase __dict__ and __weakref__cls_dict.pop("__dict__",None)cls_dict.pop("__weakref__",None)# Pickle fix for frozen dataclass as mentioned in https://bugs.python.org/issue36424# Use only if __getstate__ and __setstate__ are not declared and frozen=Trueif(all(paramnotincls_dictforparamin["__getstate__","__setstate__"])andcls.__dataclass_params__.frozen):cls_dict["__setstate__"]=_slots_setstate# Prepare new class with slotsnew_cls=cls.__class__(cls.__name__,cls.__bases__,cls_dict)new_cls.__qualname__=cls.__qualname__new_cls.__module__=cls.__module___stack.clear()returnnew_clsreturnwrapif_clsisNoneelsewrap(_cls)