Example #1
0
def score_type(t: Type) -> 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.
    """
    if isinstance(t, AnyType):
        return 2
    if isinstance(t, UnionType):
        if any(isinstance(x, AnyType) for x in t.items):
            return 2
        if not is_optional(t):
            return 1
    return 0
Example #2
0
def score_type(t: Type) -> 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.
    """
    if isinstance(t, AnyType):
        return 2
    if isinstance(t, UnionType):
        if any(isinstance(x, AnyType) for x in t.items):
            return 2
        if not is_optional(t):
            return 1
    return 0
Example #3
0
    def score_type(self, t: Type) -> 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.
        """
        if isinstance(t, AnyType):
            return 20
        if isinstance(t, UnionType):
            if any(isinstance(x, AnyType) for x in t.items):
                return 20
            if not is_optional(t):
                return 10
        if self.try_text and isinstance(
                t, Instance) and t.type.fullname() == 'builtins.str':
            return 1
        return 0
Example #4
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(x, AnyType) for x in t.items):
                return 20
            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 #5
0
 def visit_union_type(self, t: UnionType) -> str:
     if len(t.items) == 2 and is_optional(t):
         return "Optional[{}]".format(remove_optional(t).accept(self))
     else:
         return super().visit_union_type(t)