Skip to content

Typemap yields Upgrade(type) objects on load #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cstjean opened this issue Jan 18, 2025 · 1 comment
Open

Typemap yields Upgrade(type) objects on load #630

cstjean opened this issue Jan 18, 2025 · 1 comment
Labels

Comments

@cstjean
Copy link

cstjean commented Jan 18, 2025

Thank you for answering #627. So far it works well, however I found an issue:

using JLD2

struct Foo
    x::Int
end

jldsave("test.jld2"; type=Foo)

load("test.jld2", "type"; typemap=Dict("Main.Foo"=>JLD2.Upgrade(Foo)))

This should reasonably yield Foo, but it yields Upgrade(Foo). To put it another way, if I'm deserializing a Dict(Foo=>4), it'll become Dict(Upgrade(Foo)=>4).

@JonasIsensee
Copy link
Collaborator

JonasIsensee commented Jan 23, 2025

Hi @cstjean,

that makes sense, given how things are implemented....
Things should work if you "just" do:
load("test.jld2", "type"; typemap=Dict("Main.Foo"=>Foo))
(Which then does not allow you to deserialize instances of the changed Foo in the same operation)...

I don't think there is an easy fix right now.
To improve things, we would have to add an extra information passage pathway into JLD2.
jlconvert loads a type and returns JLD2.Upgrade(Foo) in your case.
This is important for reconstructing instances, as it signals to the calling function that the rconvert constructor should be used instead of the default version.
However, this is somewhat inconsistent as jlconvert normally promises to return the requested type.

EDIT: also for reference

julia> jldsave("test.jld2"; type=DataType[Float64, Foo])

julia> load("test.jld2"; typemap=Dict("Main.Foo"=>JLD2.Upgrade(Foo)))
Error encountered while load FileIO.File{FileIO.DataFormat{:JLD2}, String}("test.jld2").

Fatal error:
ERROR: MethodError: Cannot `convert` an object of type JLD2.Upgrade to an object of type DataType

Closest candidates are:
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84

Stacktrace:
  [1] setindex!(A::Vector{DataType}, x::JLD2.Upgrade, i1::Int64)
    @ Base ./array.jl:1021
  [2] macro expansion
    @ /data.lmp/isensee/.julia/dev/JLD2/src/io/dataio.jl:125 [inlined]
  [3] macro expansion
    @ ./simdloop.jl:77 [inlined]
  [4] read_array!(v::Vector{…}, f::JLD2.JLDFile{…}, rr::JLD2.MappedRepr{…})
    @ JLD2 /data.lmp/isensee/.julia/dev/JLD2/src/io/dataio.jl:122

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants