fromabcimportABCfromtypingimportAny,TypeVarfromtyping_extensionsimportTypedDictfromreturns.interfaces.equableimportEquablefromreturns.primitives.hktimportKind1fromreturns.primitives.typesimportImmutable_EqualType=TypeVar('_EqualType',bound=Equable)class_PickleState(TypedDict):"""Dict to represent the `BaseContainer` state to be pickled."""# TODO: Remove `__slots__` from here when `slotscheck` allow ignore classes# by using comments. We don't need the slots here since this class is just# a representation of a dictionary and should not be instantiated by any# means.# See: https://github.com/ariebovenberg/slotscheck/issues/71__slots__=('container_value',)# type: ignorecontainer_value:Any
[docs]classBaseContainer(Immutable,ABC):"""Utility class to provide all needed magic methods to the context."""__slots__=('_inner_value',)_inner_value:Any
[docs]def__init__(self,inner_value)->None:""" Wraps the given value in the Container. 'value' is any arbitrary value of any type including functions. """object.__setattr__(self,'_inner_value',inner_value)
[docs]def__repr__(self)->str:"""Used to display details of object."""return'<{}: {}>'.format(self.__class__.__qualname__.strip('_'),str(self._inner_value),)
[docs]def__eq__(self,other:object)->bool:"""Used to compare two 'Container' objects."""returncontainer_equality(self,other)# type: ignore
[docs]def__hash__(self)->int:"""Used to use this value as a key."""returnhash(self._inner_value)
[docs]def__getstate__(self)->_PickleState:"""That's how this object will be pickled."""return{'container_value':self._inner_value}# type: ignore
[docs]def__setstate__(self,state:_PickleState|Any)->None:"""Loading state from pickled data."""ifisinstance(state,dict)and'container_value'instate:object.__setattr__(self,'_inner_value',state['container_value'],)else:# backward compatibility with 0.19.0 and earlierobject.__setattr__(self,'_inner_value',state)
[docs]defcontainer_equality(self:Kind1[_EqualType,Any],other:Kind1[_EqualType,Any],)->bool:""" Function to compare similar containers. Compares both their types and their inner values. """iftype(self)!=type(other):# noqa: WPS516, E721returnFalsereturnbool(self._inner_value==other._inner_value,# type: ignore # noqa: SLF001)