Example #1
0
def any_score_type(ut: Type, arg_pos: bool) -> float:
    """Generate a very made up number representing the Anyness of a type.

    Higher is better, 1.0 is max
    """
    t = get_proper_type(ut)
    if isinstance(t, AnyType) and t.type_of_any != TypeOfAny.special_form:
        return 0
    if isinstance(t, NoneType) and arg_pos:
        return 0.5
    if isinstance(t, UnionType):
        if any(isinstance(x, AnyType) for x in t.items):
            return 0.5
        if any(has_any_type(x) for x in t.items):
            return 0.25
    if has_any_type(t):
        return 0.5

    return 1.0
Example #2
0
    def score_type(self, t: Type, arg_pos: bool) -> int:
        """Generate a score for a type that we use to pick which type to use.

        Lower is better, prefer non-union/non-any types. Don't penalize optionals.
        """
        t = get_proper_type(t)
        if isinstance(t, AnyType):
            return 20
        if arg_pos and isinstance(t, NoneType):
            return 20
        if isinstance(t, UnionType):
            if any(isinstance(get_proper_type(x), AnyType) for x in t.items):
                return 20
            if any(has_any_type(x) for x in t.items):
                return 15
            if not is_optional(t):
                return 10
        if isinstance(t, CallableType) and (has_any_type(t) or is_tricky_callable(t)):
            return 10
        if self.try_text and isinstance(t, Instance) and t.type.fullname == 'builtins.str':
            return 1
        return 0
Example #3
0
def callable_has_any(t: CallableType) -> int:
    # We count a bare None in argument position as Any, since
    # pyannotate turns it into Optional[Any]
    return any(isinstance(at, NoneType)
               for at in t.arg_types) or has_any_type(t)
Example #4
0
def callable_has_any(t: CallableType) -> int:
    # We count a bare None in argument position as Any, since
    # pyannotate turns it into Optional[Any]
    return any(isinstance(at, NoneType) for at in t.arg_types) or has_any_type(t)