Example #1
0
        def apply_interface(
            iface_arg: Expression,
            class_info: TypeInfo,
            api: SemanticAnalyzerPluginInterface,
            context: Context,
        ) -> None:
            if not isinstance(iface_arg, RefExpr):
                api.fail("Argument to implementer should be a ref expression",
                         iface_arg)
                return
            iface_name = iface_arg.fullname
            if iface_name is None:
                # unknown interface, probably from stubless package
                return

            iface_type = iface_arg.node
            if iface_type is None:
                return
            if not isinstance(iface_type, TypeInfo):
                # Possibly an interface from unimported package, ignore
                return

            if not self._is_interface(iface_type):
                api.fail(
                    f"zope.interface.implementer accepts interface, "
                    f"not {iface_name}.",
                    iface_arg,
                )
                api.fail(
                    f"Make sure you have stubs for all packages that "
                    f"provide interfaces for {iface_name} class hierarchy.",
                    iface_arg,
                )
                return

            # print("CLASS INFO", class_info)
            md = self._get_metadata(class_info)
            if "implements" not in md:
                md["implements"] = []
            # impl_list = cast(List[str], md['implements'])
            md["implements"].append(iface_type.fullname)
            self.log(f"Found implementation of "
                     f"{iface_type.fullname}: {class_info.fullname}")

            # Make sure implementation is treated as a subtype of an interface. Pretend
            # there is a decorator for the class that will create a "type promotion",
            # but ensure this only gets applied a single time per interface.
            promote = Instance(iface_type, [])
            if not any(ti._promote == promote for ti in class_info.mro):
                faketi = TypeInfo(SymbolTable(), iface_type.defn,
                                  iface_type.module_name)
                faketi._promote = promote
                class_info.mro.append(faketi)
Example #2
0
    def _apply_interface(self, impl: TypeInfo, iface: TypeInfo) -> None:

        md = self._get_metadata(impl)
        if "implements" not in md:
            md["implements"] = []

        md["implements"].append(iface.fullname)
        self.log(f"Found implementation of "
                 f"{iface.fullname}: {impl.fullname}")

        # Make sure implementation is treated as a subtype of an interface. Pretend
        # there is a decorator for the class that will create a "type promotion",
        # but ensure this only gets applied a single time per interface.
        promote = Instance(iface, [])
        if not any(ti._promote == promote for ti in impl.mro):
            faketi = TypeInfo(SymbolTable(), iface.defn, iface.module_name)
            faketi._promote = promote
            impl.mro.append(faketi)