예제 #1
0
    def create_from(self, annotation: Any, ctx: CreationContext) -> Optional[TypeChecker]:
        if hasattr(annotation, '__origin__') and hasattr(annotation,
                                                         '__args__') and annotation.__origin__ in InterfaceMapping:
            (protocol,) = InterfaceMapping[annotation.__origin__]
            bindings = protocol.__parameters__  # args of Generic super class
            origin = annotation.__origin__

            inner_checkers = []
            for param in annotation.__args__:
                ch = ctx.find_checker(param)
                if ch is None:
                    raise UntypyAttributeError(f"Could not resolve annotation {param} inside of {annotation}")
                inner_checkers.append(ch)
            if len(inner_checkers) != len(bindings):
                raise UntypyAttributeError(f"Expected {len(bindings)} type arguments inside of {annotation}")

            name = f"{origin.__name__}[" + (', '.join(map(lambda t: t.describe(), inner_checkers))) + "]"

            bindings = dict(zip(bindings, annotation.__args__))
            ctx.with_typevars(bindings)
            template = WrappedType(protocol, ctx.with_typevars(bindings), name=name, implementation_template=origin,
                                   declared=ctx.declared_location())
            return InterfaceChecker(origin, template, name)
        else:
            return None
    def create_from(self, annotation: Any,
                    ctx: CreationContext) -> Optional[TypeChecker]:
        if annotation in InterfaceMapping:
            # Assume Any if no parameters are given
            (protocol, ) = InterfaceMapping[annotation]
            bindings = protocol.__parameters__

            if len(bindings) == 0:
                raise AssertionError(
                    f"This is a BUG. {annotation} has no generic params.")

            # handle Python inconsistency
            if hasattr(annotation, '__class_getitem__'):
                return self.create_from(
                    annotation.__class_getitem__(*([Any] * len(bindings))),
                    ctx)
            elif hasattr(annotation, '__getitem__'):
                return self.create_from(
                    annotation.__getitem__(*([Any] * len(bindings))), ctx)

        elif hasattr(annotation, '__origin__') and hasattr(
                annotation,
                '__args__') and annotation.__origin__ in InterfaceMapping:
            (protocol, ) = InterfaceMapping[annotation.__origin__]
            bindings = protocol.__parameters__  # args of Generic super class
            origin = annotation.__origin__

            inner_checkers = []
            for param in annotation.__args__:
                ch = ctx.find_checker(param)
                if ch is None:
                    raise UntypyAttributeError(
                        f"Could not resolve annotation {param} inside of {annotation}"
                    )
                inner_checkers.append(ch)
            if len(inner_checkers) != len(bindings):
                raise UntypyAttributeError(
                    f"Expected {len(bindings)} type arguments inside of {annotation}"
                )

            name = f"{origin.__name__}[" + (', '.join(
                map(lambda t: t.describe(), inner_checkers))) + "]"

            bindings = dict(zip(bindings, annotation.__args__))
            ctx = ctx.with_typevars(bindings)
            if type(origin) == type:
                template = WrappedType(protocol,
                                       ctx.with_typevars(bindings),
                                       name=name,
                                       implementation_template=origin,
                                       declared=ctx.declared_location())
                return InterfaceChecker(origin, template, name)
            else:
                # type(origin) == collection.abc.ABCMeta
                return ProtocolChecker(protocol, ctx, altname=name)
        else:
            return None
예제 #3
0
 def create_from(self, annotation: Any,
                 ctx: CreationContext) -> Optional[TypeChecker]:
     if (type(annotation) is GenericAlias and annotation.__origin__
             == list) or (type(annotation) is type(List[int])
                          and annotation.__origin__ == list):
         assert len(annotation.__args__) == 1
         inner = ctx.find_checker(annotation.__args__[0])
         if inner is None:
             return None
         return ListChecker(inner, ctx.declared_location())
     else:
         return None
 def create_from(self, annotation: Any, ctx: CreationContext) -> Optional[TypeChecker]:
     t = type(annotation)
     if (t in [SequenceTypeA, SequenceTypeB] and annotation.__origin__ in [Sequence, ABCSequence]) or \
             annotation in [Sequence, ABCSequence]:  # no args version
         try:
             args = annotation.__args__
         except AttributeError:
             args = []
         inner = []
         elemChecker = None
         if len(args) == 0:
             sf = SimpleFactory()
             inner = [sf.create_from(list, ctx),
                      sf.create_from(tuple, ctx),
                      sf.create_from(str, ctx)]
         elif len(args) == 1:
             elemChecker = ctx.find_checker(args[0])
             if elemChecker is None:
                 return None
             inner = [ListChecker(elemChecker, ctx.declared_location()),
                      VariadicTupleChecker(elemChecker)]
         return SequenceChecker(inner, ctx, elemChecker)
     else:
         return None