def __template_has_attr()

in testslide/strict_mock.py [0:0]


    def __template_has_attr(self, name: str) -> bool:
        def get_class_init(klass: type) -> Callable:
            import testslide.mock_constructor  # Avoid cyclic dependencies

            if not testslide.mock_constructor._is_mocked_class(klass):
                return klass.__init__  # type: ignore

            # If klass is the mocked subclass, pull the original version of
            # __init__ so we can introspect into its implementation (and
            # not the __init__ wrapper at the mocked class).
            mocked_class = klass
            original_class = mocked_class.mro()[1]
            return testslide.mock_constructor._get_original_init(
                original_class, instance=None, owner=mocked_class
            )

        def is_runtime_attr() -> bool:
            if self._template:
                for klass in self._template.mro():
                    template_init = get_class_init(klass)
                    if not inspect.isfunction(template_init):
                        continue
                    for instruction in dis.get_instructions(template_init):
                        if (
                            instruction.opname == "STORE_ATTR"
                            and name == instruction.argval
                        ):
                            return True
            return False

        return (
            hasattr(self._template, name)
            or name in self._runtime_attrs  # type: ignore
            or name in getattr(self._template, "__slots__", [])
            or is_runtime_attr()
        )