Skip to content

Commit

Permalink
unknown unionall type param (#539)
Browse files Browse the repository at this point in the history
* unknown unionall type param

* version & changelog
  • Loading branch information
JonasIsensee authored Jan 19, 2024
1 parent 6760bd4 commit b757dd5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.4.45
- fix loading of unknown struct with UnionAll (unknown) type parameter (#538)

## 0.4.44
- fix dispatch around `Union{}` as type parameter

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "JLD2"
uuid = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
version = "0.4.44"
version = "0.4.45"

[deps]
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Expand Down
26 changes: 20 additions & 6 deletions src/data/reconstructing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ function _resolve_type_singlemodule(::ReadRepresentation{T,DataTypeODR()},
return m
end

isunknowntype(x::Type{<:UnknownType}) = true
isunknowntype(x) = false
isunknowntype(::Type{Union{}}) = false
isunknowntype(x::Type) = x <: UnknownType ? true : false

function _resolve_type(rr::ReadRepresentation{T,DataTypeODR()},
f::JLDFile,
Expand Down Expand Up @@ -464,7 +464,7 @@ function Base.getproperty(rc::ReconstructedMutable{N,FN,FT}, s::Symbol) where {N
isnothing(i) && throw(ArgumentError("field $s not found"))
getfield(rc, 1)[i]::FT.parameters[i]
end
Base.propertynames(rc::ReconstructedMutable) = propertynames(getfield(rc, 1))
Base.propertynames(::ReconstructedMutable{N,FN}) where {N, FN} = FN


function Base.show(io::IO, f::ReconstructedMutable{N, FN, FT}) where {N,FN,FT}
Expand Down Expand Up @@ -499,10 +499,17 @@ Convert an UnknownType to a corresponding string. This is
only used for warning during reconstruction errors.
See also shorttypestring.
"""
function typestring(::Type{UnknownType{T, P}}) where {T, P}
function typestring(UT)# ::Type{<:UnknownType}
if UT isa UnionAll
UT = behead(UT)
T = UT.parameters[1]
params = ()
else
T = UT.parameters[1]
params = UT.parameters[2].parameters
end
tn = IOBuffer()
print(tn, T)
params = P.parameters
if !isempty(params)
write(tn, '{')
for i = 1:length(params)
Expand All @@ -526,10 +533,17 @@ Convert an UnknownType to a corresponding string. This is
only used to create names for reconstructed types.
See also typestring.
"""
function shorttypestring(::Type{UnknownType{T, P}}) where {T, P}
function shorttypestring(UT) #::Type{<:UnknownType}
if UT isa UnionAll
UT = behead(UT)
T = UT.parameters[1]
params = ()
else
T = UT.parameters[1]
params = UT.parameters[2].parameters
end
tn = IOBuffer()
print(tn, T isa Symbol ? split(string(T),'.')[end] : T)
params = P.parameters
if !isempty(params)
write(tn, '{')
for i = 1:length(params)
Expand Down
6 changes: 3 additions & 3 deletions test/Artifacts.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[testfiles]
git-tree-sha1 = "8d34be7b8603854dca03eb0330f214a9194de75a"
git-tree-sha1 = "ab278bd01e5e230fd6403df8a9f0ecfead8f673f"
lazy = true

[[testfiles.download]]
sha256 = "f91805f612a00178ca68d5ecc895af09c231d1ca949c7ef0157cb9730b3a84aa"
url = "https://github.com/JonasIsensee/JLD2TestFiles/archive/refs/tags/v0.1.1.tar.gz"
sha256 = "4db286cb7a53d22368ac9f0305064f1b70e01936d3ec05961d00df3ab660769a"
url = "https://github.com/JonasIsensee/JLD2TestFiles/archive/refs/tags/v0.1.3.tar.gz"
35 changes: 32 additions & 3 deletions test/test_files.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
using Test, JLD2
using LazyArtifacts
# When adding test files to the JLD2Testfiles repo tag a new
# release and adapt Artifacts.toml and the line below accordingly.
testfiles = artifact"testfiles/JLD2TestFiles-0.1.1/artifacts"
#=
When adding test files to the JLD2Testfiles repo tag a new
release and adapt Artifacts.toml and the line below accordingly.
git clone [email protected]:JonasIsensee/JLD2TestFiles.git
add file & commit
git tag v0.1.X
git push
git push --tags
go to github and create a new release with name "v0.1.x"
update Artifacts.toml:
1) navigate to testdir
2) launch repl and run
using Pkg
Pkg.activate(; temp=true)
Pkg.add("ArtifactUtils")
using ArtifactUtils
add_artifact!(
"Artifacts.toml",
"testfiles",
"https://github.com/JonasIsensee/JLD2TestFiles/archive/refs/tags/v0.1.x.tar.gz", # replace version here
force=true,
lazy=true)
update artifact string below to reflect new version number
=#
testfiles = artifact"testfiles/JLD2TestFiles-0.1.3/artifacts"

@testset "HDF5 compat test files" begin
# These are test files copied from the HDF5.jl test suite
Expand Down Expand Up @@ -143,4 +165,11 @@ end
@test data["ds"] isa JLD2.SerializedDict
@test JLD2.isreconstructed(data["ds"].kvvec[1].second)
@test JLD2.isreconstructed(data["tms"][2])
end

@testset "Issue #538 Reconstruction of UnionAll type parameter" begin
fn = joinpath(testfiles, "unionall_typeparam.jld2")
data = load(fn, "test")
@test propertynames(data) == (:val,)
@test data.val == 4
end

2 comments on commit b757dd5

@JonasIsensee
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/99158

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.45 -m "<description of version>" b757dd54bb40be9b51ae2fb616e45f7d79934dd3
git push origin v0.4.45

Please sign in to comment.