def exit_method(return_type=None): if return_type is None: return_type = types.none_type return types.func( [ types.union(types.exception_meta_type, types.none_type), types.union(types.exception_type, types.none_type), types.union(types.traceback_type, types.none_type) ], return_type )
def type_of_type_union_is_metatype_of_unioned_types(): type_bindings = { "str": types.meta_type(types.str_type), "int": types.meta_type(types.int_type), } node = nodes.type_union([nodes.ref("str"), nodes.ref("int")]) inferred_type = infer(node, type_bindings=type_bindings) assert types.is_meta_type(inferred_type) assert_equal(types.union(types.str_type, types.int_type), inferred_type.type)
def covariant_type_parameter_is_substituted_with_common_super_type_of_actual_type_params(self): covariant_type_param = types.covariant("T") first_class_type = types.class_type("User") second_class_type = types.class_type("Role") generic_class = types.generic_class("Pair", [covariant_type_param, covariant_type_param]) type_map = types.is_sub_type( # TODO: need a reliable way of getting the underlying type (but as an instantiated type) generic_class(covariant_type_param, covariant_type_param), generic_class(first_class_type, second_class_type), unify=[covariant_type_param] ) assert_equal(types.union(first_class_type, second_class_type), type_map[covariant_type_param])
def argument_type_in_signature_is_unioned_with_none_if_argument_is_optional(): signature = nodes.signature( args=[nodes.signature_arg(nodes.ref("int"))], returns=nodes.ref("int") ) args = nodes.arguments([nodes.argument("x", optional=True)]) body = [nodes.ret(nodes.ref("x"))] node = nodes.func("f", args, body, type=signature) try: _infer_func_type(node) assert False, "Expected error" except errors.UnexpectedValueTypeError as error: assert_equal(types.int_type, error.expected) assert_equal(types.union(types.int_type, types.none_type), error.actual)
def duplicate_types_are_collapsed_in_type_union(self): assert_equal(types.int_type, types.union(types.int_type, types.int_type))
def common_super_type_of_unrelated_types_is_union_of_those_types(self): assert_equal( types.union(types.int_type, types.str_type), types.common_super_type([types.int_type, types.str_type]), )
def union_of_union_is_treated_the_same_as_flat_union(self): nested_union_type = types.union(types.int_type, types.union(types.none_type, types.str_type)) flat_union_type = types.union(types.int_type, types.none_type, types.str_type) assert types.is_sub_type(nested_union_type, flat_union_type) assert types.is_sub_type(flat_union_type, nested_union_type)
def instantiated_union_type_is_subtype_of_other_instantiated_union_type_if_its_types_are_a_subset(self): smaller_union_type = types.unnamed_generic(["T"], lambda T: types.union(T, types.none_type)) larger_union_type = types.unnamed_generic(["T"], lambda T: types.union(T, types.none_type, types.str_type)) assert types.is_sub_type(larger_union_type(types.int_type), smaller_union_type(types.int_type)) assert not types.is_sub_type(smaller_union_type(types.int_type), larger_union_type(types.int_type))
def union_type_is_subtype_of_other_union_type_if_its_types_are_a_subset(self): smaller_union_type = types.union(types.int_type, types.none_type) larger_union_type = types.union(types.int_type, types.none_type, types.str_type) assert types.is_sub_type(larger_union_type, smaller_union_type) assert not types.is_sub_type(smaller_union_type, larger_union_type)
def type_is_subtype_of_union_type_if_it_appears_in_union_type(self): class_type = types.class_type("User") union_type = types.union(class_type, types.none_type) assert types.is_sub_type(union_type, class_type) assert not types.is_sub_type(class_type, union_type)