def test_verify_no_duplicate(): with pytest.raises(errors.TypeError): verify_no_duplicate([1, 1, 2, 3], "%s") with pytest.raises(errors.TypeError): verify_no_duplicate([ types.StructType(name="test", fields=(("a", types.BoolType()), )), types.StructType(name="test", fields=(("a", types.BoolType()), )), ], "%s") assert verify_no_duplicate([1, 2, 3], "%s") is None
def test_structtype_verify(): typ = types.StructType("test_struct", fields=( ("a", types.IntType(8)), ("b", types.IntType(16)), )) assert verify(typ) is None, "Struct with unique field names is ok" typ = types.StructType("test_struct", fields=( ("a", types.IntType(8)), ("a", types.IntType(16)), )) with pytest.raises(errors.TypeError): verify(typ)
def test_scopedtype_verify(): typ = types.StructType(name="test_struct", type_parameters=(types.TypeVariable("T"), types.TypeVariable("U")), fields=(("a", types.IntType(32)), )) assert verify(typ) is None, "ScopedType with unique parameters is ok" typ = types.StructType(name="duplicate_type_variable", type_parameters=(types.TypeVariable("T"), types.TypeVariable("T")), fields=(("a", types.IntType(32)), )) with pytest.raises(errors.TypeError): verify(typ) typ = types.StructType(name="unbound_type_variable", fields=(("a", types.TypeVariable("T")), )) with pytest.raises(errors.ReferenceError): verify(typ)
def visit_StructTypeDeclaration(self, node: ast.StructTypeDeclaration): type_parameters = tuple( map(types.TypeVariable, node.generic_parameters)) fields = [] for field in node.fields: fields.append((field.name, generate_type(field.type))) self.add_type( node.name, types.StructType(name=node.name, type_parameters=type_parameters, fields=tuple(fields)))
def instantiate_structtype(self: types.StructType, arguments: Iterable[types.Type], scopes: Scopes) -> types.Type: arguments = tuple(arguments) zipped = zipped_typevariables(self, arguments) new_fields = [] for name, field_type in self.fields: new_fields.append((name, instantiate(field_type, zipped, scopes))) return types.StructType(name=self.name, fields=tuple(new_fields), type_parameters=self.type_parameters, type_arguments=arguments)