{"record":{"id":"df2829b4eb58b6a5","repo":"dotnet/machinelearning","slug":"bad-type-in-columntypeextensions-numbertypefromtyp","errorCode":null,"errorMessage":"Bad type in ColumnTypeExtensions.NumberTypeFromType: {type}","messagePattern":"Bad type in ColumnTypeExtensions\\.NumberTypeFromType: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Core/Data/ColumnTypeExtensions.cs","lineNumber":136,"sourceCode":"        if (kind == InternalDataKind.TS)\n            return TimeSpanDataViewType.Instance;\n        if (kind == InternalDataKind.DT)\n            return DateTimeDataViewType.Instance;\n        if (kind == InternalDataKind.DZ)\n            return DateTimeOffsetDataViewType.Instance;\n        if (kind == InternalDataKind.UG)\n            return RowIdDataViewType.Instance;\n        return NumberTypeFromKind(kind);\n    }\n\n    public static NumberDataViewType NumberTypeFromType(Type type)\n    {\n        InternalDataKind kind;\n        if (type.TryGetDataKind(out kind))\n            return NumberTypeFromKind(kind);\n\n        Contracts.Assert(false);\n        throw new InvalidOperationException($\"Bad type in {nameof(ColumnTypeExtensions)}.{nameof(NumberTypeFromType)}: {type}\");\n    }\n\n    private static NumberDataViewType NumberTypeFromKind(InternalDataKind kind)\n    {\n        switch (kind)\n        {\n            case InternalDataKind.I1:\n                return NumberDataViewType.SByte;\n            case InternalDataKind.U1:\n                return NumberDataViewType.Byte;\n            case InternalDataKind.I2:\n                return NumberDataViewType.Int16;\n            case InternalDataKind.U2:\n                return NumberDataViewType.UInt16;\n            case InternalDataKind.I4:\n                return NumberDataViewType.Int32;\n            case InternalDataKind.U4:\n                return NumberDataViewType.UInt32;","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Core/Data/ColumnTypeExtensions.cs#L118-L154","documentation":"ColumnTypeExtensions.NumberTypeFromType converts a CLR numeric type to a NumberDataViewType. It first tries TryGetDataKind; if that fails, it asserts the internal invariant and throws InvalidOperationException 'Bad type in ColumnTypeExtensions.NumberTypeFromType: {type}', meaning the caller passed a CLR type that is not a recognized numeric data kind (e.g. bool, string, DateTime, Guid).","triggerScenarios":"Calling NumberTypeFromType(typeof(bool)) / typeof(string) / typeof(DateTime) / typeof(char) / typeof(Guid) directly; or PrimitiveTypeFromType receiving a non-primitive-numeric type and delegating to NumberTypeFromType.","commonSituations":"Building custom IDataView pipelines or schema builders where a column's CLR type isn't numeric but the code assumes it is; mapping user data types to DataView types without a prior whitelist check.","solutions":["Verify the type is a supported numeric primitive (byte/sbyte/short/ushort/int/uint/long/ulong/float/double/decimal) before calling NumberTypeFromType.","Use PrimitiveTypeFromType instead, which handles non-numeric primitives (bool, string, DateTime, etc.).","Map non-numeric types explicitly (e.g. TextDataViewType.Instance for string) before conversion.","Catch InvalidOperationException and fall back to a type-lookup table of your own."],"exampleFix":"// before\nvar numberType = ColumnTypeExtensions.NumberTypeFromType(typeof(bool));\n// after\nvar viewType = typeof(bool) == type ? BooleanDataViewType.Instance\n    : ColumnTypeExtensions.PrimitiveTypeFromType(type);","handlingStrategy":"type-guard","validationCode":"private static readonly HashSet<Type> NumericTypes = new HashSet<Type> {\n    typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int),\n    typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double) };\nif (!NumericTypes.Contains(type))\n    throw new ArgumentException($\"{type} is not a numeric DataView type\");","typeGuard":"static bool IsNumericDataViewType(Type t) =>\n    t == typeof(byte) || t == typeof(sbyte) || t == typeof(short) ||\n    t == typeof(ushort) || t == typeof(int) || t == typeof(uint) ||\n    t == typeof(long) || t == typeof(ulong) || t == typeof(float) ||\n    t == typeof(double);","tryCatchPattern":"try\n{\n    numberType = ColumnTypeExtensions.NumberTypeFromType(clrType);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"NumberTypeFromType\"))\n{\n    numberType = null; // fall back to PrimitiveTypeFromType or a lookup table\n}","preventionTips":["Whitelist numeric CLR types before calling NumberTypeFromType.","Prefer PrimitiveTypeFromType when the type may be bool/string/DateTime.","Keep a CLR-type-to-DataViewType lookup table in your schema-building code.","Unit test schema building against all column types in your data sources."],"tags":["mlnet","dataview","type-mapping","invalid-operation"],"backgroundTag":"unsupported-dtype","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}