def __setup_magic_methods()

in testslide/strict_mock.py [0:0]


    def __setup_magic_methods(self) -> None:
        """
        Populate all template's magic methods with expected default behavior.
        This is important as things such as bool() depend on they existing
        on the object's class __dict__.
        https://github.com/facebook/TestSlide/issues/23
        """
        if not self._template:
            return

        implemented_magic_methods = []
        for klass in type(self).mro():
            if klass is object:
                continue
            for name in klass.__dict__:
                if name.startswith("__") and name.endswith("__"):
                    implemented_magic_methods.append(name)

        for klass in self._template.mro():
            if klass is object:
                continue
            for name in klass.__dict__:
                if name in type(self).__dict__:
                    continue
                if name == "__hash__":
                    if klass.__dict__["__hash__"] is None:
                        setattr(self, name, None)
                    else:
                        setattr(self, name, lambda: id(self))
                    continue
                if (
                    callable(klass.__dict__[name])
                    and name in self.__SETTABLE_MAGICS
                    and name not in self.__UNSETTABLE_MAGICS
                    and name not in implemented_magic_methods
                ):
                    setattr(self, name, _DefaultMagic(self, name))