"""Matchers for general object types."""importtypingastpfromjoythief.coreimportMatcherT=tp.TypeVar("T")Type=tp.Union[type[T],tuple[type[T],...]]
[docs]classInstanceOf(Matcher[T]):"""Matches any instance of the specified type(s). This matcher compares the received value using :py:func:`isinstance`, so accepts either a single type or a tuple of types. With :code:`nullable` set to :py:const:`True`, the received value can also be :py:const:`None`. Originally formulated for `this answer`_. .. _this answer: https://stackoverflow.com/a/64973325/3001761 """_nullable:bool_type:Type[T]def__init__(self,type_:Type[T],*,nullable:bool=False):super().__init__()self._nullable=nullableself._type=type_defcompare(self,other:tp.Any)->bool:type_:tuple[type[tp.Any],...]=(self._typeifisinstance(self._type,tuple)else(self._type,))ifself._nullable:type_=type_+(type(None),)returnisinstance(other,type_)defrepresent(self)->str:return(f"InstanceOf({self._type!r}"f"{', nullable=True'ifself._nullableelse''})")