From 74399e937d8d0add1d9883af4ee732d43e60c6ae Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 14:27:59 -0700 Subject: [PATCH 1/2] Test construct for the empty NamedTuple These fail on main with a MethodError: construct builds field-less types with T(), and NamedTuple{(), Tuple{}} has no zero-argument constructor. Co-Authored-By: Claude Opus 5.5 --- test/runtests.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index adf3c57..145d59d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -244,6 +244,12 @@ v = v"1.2.3" # 47 @test StructTypes.construct(Nothing, nothing) === nothing + +# types with no fields, including the empty NamedTuple +@test StructTypes.construct((i, nm, FT) -> error("no fields"), NamedTuple{(), Tuple{}}) === NamedTuple() +@test StructTypes.construct(Any[], NamedTuple{(), Tuple{}}) === NamedTuple() +@test StructTypes.construct(Any[], Tuple{}) === () +@test StructTypes.constructfrom(NamedTuple{(), Tuple{}}, Dict{String, Any}()) === NamedTuple() x = Dict("hey" => "ho") @test StructTypes.construct(Dict{String, String}, x) === x From 45146929da22e3c5640f9dc9b996ee82405da958 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 14:27:59 -0700 Subject: [PATCH 2/2] Construct the empty NamedTuple with T(()) construct(f, T) and construct(values, T) returned T() for types with no fields, which throws for NamedTuple{(), Tuple{}}, so an empty NamedTuple written by JSON3 could not be read back. They now call T(()) for NamedTuple types and T() for the rest. Co-Authored-By: Claude Opus 5.5 --- src/StructTypes.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/StructTypes.jl b/src/StructTypes.jl index e1b7325..a83ed71 100644 --- a/src/StructTypes.jl +++ b/src/StructTypes.jl @@ -610,7 +610,7 @@ Note that any `StructTypes.names` mappings are applied, as well as field-specifi """ @inline function construct(f, ::Type{T}) where {T} N = fieldcount(T) - N == 0 && return T() + N == 0 && return T <: NamedTuple ? T(()) : T() nms = names(T) kwargs = keywordargs(T) constructor = T <: Tuple ? tuple : T <: NamedTuple ? ((x...) -> T(tuple(x...))) : T @@ -895,7 +895,7 @@ end @inline function construct(values::Vector{Any}, ::Type{T}) where {T} N = fieldcount(T) - N == 0 && return T() + N == 0 && return T <: NamedTuple ? T(()) : T() nms = names(T) kwargs = keywordargs(T) constructor = T <: Tuple ? tuple : T <: NamedTuple ? ((x...) -> T(tuple(x...))) : T