JuliaIO / JLD.jl

Saving and loading julia variables while preserving native types
MIT License
278 stars 55 forks source link

cannot append nested parametric types #297

Closed takbal closed 3 years ago

takbal commented 3 years ago

Looks like JLD cannot correctly append variables with a type that is parameterised with another parametric type. I am using the "r+" mode for appending as recommended here. The test below reproduces the error (JLD v0.12.1, Julia 1.5.3).

using JLD

function test_append(object; append=true)

    if isfile("test.jld")
        rm("test.jld")
    end

    jldopen("test.jld", "w") do file
        write(file, "original", object)
    end

    if append
        jldopen("test.jld", "r+") do file
            write(file, "appended", object)
        end
    end

    try
        test = load("test.jld")
        println("read successful")
    catch e
        @error exception=e
    end

end

struct FooType{T}
    X::T
end

struct BarType{T}
    X::T
end

# works
test_append( rand(2,2) )

# works
T1 = FooType{Int64}(1)
test_append(T1)

# works
T1 = BarType{Int64}(1)
T2 = FooType{BarType{Int64}}(T1)
test_append(T2, append=false)

# fails
T1 = BarType{Int64}(1)
T2 = FooType{BarType{Int64}}(T1)
test_append(T2)

# works
T1 = FooType{Int64}(1)
T2 = FooType{FooType{Int64}}(T1)
test_append(T2, append=false)

# fails
T1 = FooType{Int64}(1)
T2 = FooType{FooType{Int64}}(T1)
test_append(T2)

using AxisKeys

# fails
test_append( wrapdims(rand(2,2), x=1:2, y=1:2) )

Results in:

read successful
read successful
read successful
┌ Error: stored type FooType{BarType{Core.Int64}} does not match currently loaded type
└ @ Main ~/foo.jl:21
read successful
┌ Error: stored type FooType{FooType{Core.Int64}} does not match currently loaded type
└ @ Main ~/foo.jl:21
┌ Error: stored type AxisKeys.KeyedArray{Core.Float64,2,NamedDims.NamedDimsArray{(:x, :y),Core.Float64,2,Core.Array{Core.Float64,2}},Core.Tuple{Base.UnitRange{Core.Int64},Base.UnitRange{Core.Int64}}} does not match currently loaded type
└ @ Main ~/foo.jl:21

Found while trying append AxisKeys.KeyedArray data.