def is_error_type(type_): """ Check if a type is a type error :param type_: :return: """ return type_inspection.is_error(type_)
def get_type_str(type_): """ Get the abbreviated str representation of a type for printing friendly error messages :param type_: Type :return: str """ if is_error(type_): return "TypeError" return str(type_)
def set_contained_elements_type(localization, container, elements): """ Modifies the types stored by a container, dealing with union type indexes :param localization: :param container: :param elements: :return: """ if type(elements) is tuple: if type_inspection.is_error(elements[0]): return elements[0] if type_inspection.is_union_type(elements[0]): errors = [] # For each type of the union, set elements for t in elements[0].types: # Special case for dictionaties if len(elements) > 1: result = __set_contained_elements_type(localization, container, (t, elements[1])) if type_inspection.is_error(result): errors.append(result) else: result = __set_contained_elements_type(localization, container, t) if type_inspection.is_error(result): errors.append(result) # Everything is an error if len(errors) == len(elements[0].types): # Delete errors an produce a single one for e in errors: StypyTypeError.remove_error_msg(e) return StypyTypeError(localization, "Indexes of indexable containers must be Integers or instances that " "implement the __index__ method") else: for e in errors: e.turn_to_warning() return return __set_contained_elements_type(localization, container, elements)
def setdefault(localization, proxy_obj, arguments): ret_type = TypeModifiers.get(localization, proxy_obj, arguments) if len(arguments) > 1: t2 = arguments[1] else: t2 = types.NoneType # Type do not exist if is_error(ret_type): t1 = arguments[0] set_contained_elements_type_for_key(get_self(proxy_obj), t1, t2) else: if len(arguments) > 1: t1 = arguments[0] set_contained_elements_type_for_key(get_self(proxy_obj), t1, t2) t2 = union_type.UnionType.add(ret_type, t2) else: t2 = ret_type return t2
def print_type(obj): """ Prints a type of an object :param obj: :return: """ if obj is None: return "None" type_to_print = type(obj).__name__ if isinstance(obj, TypeWrapper): type_to_print = str(obj) if is_error(obj): type_to_print = type(obj).__name__ + "(\"" + obj.message + "\")" if obj is UndefinedType: type_to_print = "UndefinedType" else: if type(obj) is types.TypeType: type_to_print = str(obj) return type_to_print
def __getitem__(localization, proxy, member): r = get_member(localization, proxy, member) StypyTypeError.remove_error_msg(r) return not is_error(r)