コード例 #1
0
def make_type_checker(annotation):
    """Builds a PyTypeChecker for the given type annotation."""
    if type_annotations.is_generic_union(annotation):
        type_args = type_annotations.get_generic_type_args(annotation)
        options = [make_type_checker(t) for t in type_args]
        return _api_dispatcher.MakeUnionChecker(options)

    elif type_annotations.is_generic_list(annotation):
        type_args = type_annotations.get_generic_type_args(annotation)
        if len(type_args) != 1:
            raise AssertionError(
                "Expected List[...] to have a single type parameter")
        elt_type = make_type_checker(type_args[0])
        return _api_dispatcher.MakeListChecker(elt_type)

    elif isinstance(annotation, type):
        if annotation not in _is_instance_checker_cache:
            checker = _api_dispatcher.MakeInstanceChecker(annotation)
            _is_instance_checker_cache[annotation] = checker
        return _is_instance_checker_cache[annotation]

    elif annotation is None:
        return make_type_checker(type(None))

    else:
        raise ValueError(
            f"Type annotation {annotation} is not currently supported"
            " by dispatch.  Supported annotations: type objects, "
            " List[...], and Union[...]")
コード例 #2
0
 def contains_cls(x):
     """Returns true if `x` contains `cls`."""
     if isinstance(x, dict):
         return any(contains_cls(v) for v in x.values())
     elif x is cls:
         return True
     elif (type_annotations.is_generic_list(x)
           or type_annotations.is_generic_union(x)):
         type_args = type_annotations.get_generic_type_args(x)
         return any(contains_cls(arg) for arg in type_args)
     else:
         return False
コード例 #3
0
def make_type_checker(annotation):
    """Builds a PyTypeChecker for the given type annotation."""
    if type_annotations.is_generic_union(annotation):
        type_args = type_annotations.get_generic_type_args(annotation)

        # If the union contains two or more simple types, then use a single
        # InstanceChecker to check them.
        simple_types = [t for t in type_args if isinstance(t, type)]
        simple_types = tuple(sorted(simple_types, key=id))
        if len(simple_types) > 1:
            if simple_types not in _is_instance_checker_cache:
                checker = _api_dispatcher.MakeInstanceChecker(*simple_types)
                _is_instance_checker_cache[simple_types] = checker
            options = ([_is_instance_checker_cache[simple_types]] + [
                make_type_checker(t)
                for t in type_args if not isinstance(t, type)
            ])
            return _api_dispatcher.MakeUnionChecker(options)

        options = [make_type_checker(t) for t in type_args]
        return _api_dispatcher.MakeUnionChecker(options)

    elif type_annotations.is_generic_list(annotation):
        type_args = type_annotations.get_generic_type_args(annotation)
        if len(type_args) != 1:
            raise AssertionError(
                "Expected List[...] to have a single type parameter")
        elt_type = make_type_checker(type_args[0])
        return _api_dispatcher.MakeListChecker(elt_type)

    elif isinstance(annotation, type):
        if annotation not in _is_instance_checker_cache:
            checker = _api_dispatcher.MakeInstanceChecker(annotation)
            _is_instance_checker_cache[annotation] = checker
        return _is_instance_checker_cache[annotation]

    elif annotation is None:
        return make_type_checker(type(None))

    else:
        raise ValueError(
            f"Type annotation {annotation} is not currently supported"
            " by dispatch.  Supported annotations: type objects, "
            " List[...], and Union[...]")