Skip to content

Commit c97630d

Browse files
committed
flat_params hotfix respect to empty models
1 parent fc6323b commit c97630d

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/parameter_inspection.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ flat_params(m; prefix="") = flat_params(m, Val(isamodel(m)); prefix=prefix)
5757
flat_params(m, ::Val{false}; prefix="") = NamedTuple{(Symbol(prefix),), Tuple{Any}}((m,))
5858
function flat_params(m, ::Val{true}; prefix="")
5959
fields = propertynames(m)
60+
if isempty(fields)
61+
return NamedTuple{(Symbol(prefix),)}((m,))
62+
end
6063
prefix = prefix == "" ? "" : prefix * "__"
6164
merge([flat_params(getproperty(m, field); prefix="$(prefix)$(field)") for field in fields]...)
6265
end

test/parameter_inspection.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ end
3535
end
3636

3737
struct ChildModel <: Model
38-
x::Int
39-
y::String
38+
r::Int
39+
s
4040
end
4141

4242
struct ParentModel <: Model
@@ -46,18 +46,20 @@ struct ParentModel <: Model
4646
second_child::ChildModel
4747
end
4848

49+
struct Missy <: Model end
50+
4951
@testset "flat_params method" begin
5052

5153
m = ParentModel(1, "parent", ChildModel(2, "child1"),
52-
ChildModel(3, "child2"))
54+
ChildModel(3, Missy()))
5355

5456
@test MLJModelInterface.flat_params(m) == (
5557
x = 1,
5658
y = "parent",
57-
first_child__x = 2,
58-
first_child__y = "child1",
59-
second_child__x = 3,
60-
second_child__y = "child2"
59+
first_child__r = 2,
60+
first_child__s = "child1",
61+
second_child__r = 3,
62+
second_child__s = Missy()
6163
)
6264
end
6365
true

0 commit comments

Comments
 (0)