def overload_call_target(self, arg_types, is_var_arg, overload, context): """Infer the correct overload item to call with given argument types. The return value may be Callable or any (if an unique item could not be determined). If is_var_arg is True, the caller uses varargs. """ # TODO for overlapping signatures we should try to get a more precise # result than 'any' match = None # Callable, Any or None for typ in overload.items(): if self.matches_signature(arg_types, is_var_arg, typ): if match and (isinstance(match, Any) or not is_same_type((match).ret_type, typ.ret_type)): # Ambiguous return type. Either the function overload is # overlapping (which results in an error elsewhere) or the # caller has provided some dynamic argument types; in # either case can only infer the type to be any, as it is # not an error to use any types in calls. # TODO overlapping overloads should be possible in some # cases match = Any() else: match = typ if not match: self.msg.no_variant_matches_arguments(overload, context) return Any() else: return match
def is_valid_inferred_type(self, typ): """Is an inferred type invalid? Examples include the None type or a type with a None component. """ if is_same_type(typ, NoneTyp()): return False elif isinstance(typ, Instance): for arg in (typ).args: if not self.is_valid_inferred_type(arg): return False elif isinstance(typ, TupleType): for item in (typ).items: if not self.is_valid_inferred_type(item): return False return True
def visit_intersection(self, t): # Only support very rudimentary meets between intersection types. if is_same_type(self.s, t): return self.s else: return self.default(self.s)
Typ overload_call_target(self, Typ[] arg_types, bool is_var_arg, Overloaded overload, Context context): """Infer the correct overload item to call with given argument types. The return value may be Callable or any (if an unique item could not be determined). If is_var_arg is True, the caller uses varargs. """ # TODO for overlapping signatures we should try to get a more precise # result than 'any' Typ match = None # Callable, Any or None for typ in overload.items(): if self.matches_signature(arg_types, is_var_arg, typ): if match and (isinstance(match, Any) or not is_same_type(((Callable)match).ret_type, typ.ret_type)): # Ambiguous return type. Either the function overload is # overlapping (which results in an error elsewhere) or the # caller has provided some dynamic argument types; in # either case can only infer the type to be any, as it is # not an error to use any types in calls. # TODO overlapping overloads should be possible in some # cases match = Any() else: match = typ if not match: self.msg.no_variant_matches_arguments(overload, context) return Any() else:
Compare to the original method in the superclass of info. """ # If this is not an override, this can't be a simple override either. # Generic inheritance is not currently supported, since we need to map # type variables between types; in the future this restriction can be # lifted. if info.base is None or info.base.type_vars != []: return False orig = info.base.get_method(fdef.name()) # Ignore the first argument (self) when determining type sameness. # TODO overloads newtype = (Callable)function_type(fdef) newtype = replace_self_type(newtype, Any()) origtype = (Callable)function_type(orig) origtype = replace_self_type(origtype, Any()) return is_same_type(newtype, origtype) str tvar_slot_name(int n, any is_alt=False): """Return the name of the member that holds the runtime value of the given type variable slot. """ if is_alt != BOUND_VAR: if n == 0: return '__tv' else: return '__tv{}'.format(n + 1) else: # Greatest lower bound if n == 0: return '__btv'
((Instance)source_type).erased) or (isinstance(res, CoerceExpr) and isinstance(target_type, Instance))): res = JavaCast(res, target_type) return res bool is_trivial_coercion(Type target_type, Type source_type, bool is_java): """Is an implicit coercion from source_type to target_type a no-op? Note that we omit coercions of form any <= C, unless C is a primitive that may have a special representation. """ # FIX: Replace type vars in source type with any? if isinstance(source_type, Void) or is_same_type(target_type, source_type): return True # Coercions from a primitive type to any other type are non-trivial, since # we may have to change the representation. if not is_java and is_special_primitive(source_type): return False return (is_proper_subtype(source_type, target_type) or isinstance(source_type, NoneTyp) or isinstance(target_type, Any)) bool is_special_primitive(Type type): """Is type a primitive with a special runtime representation?