Skip to content

Commit 0283d03

Browse files
committed
Improve error message for models with wrong number of interfaces
1 parent 1f86a0f commit 0283d03

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/model_macro.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,17 @@ function get_make_node_function(ms_body, ms_args, ms_name)
737737
$ms_body
738738
end
739739

740+
function GraphPPL.add_terminated_submodel!(
741+
__model__::GraphPPL.Model,
742+
__context__::GraphPPL.Context,
743+
__options__::GraphPPL.NodeCreationOptions,
744+
__fform__::typeof($ms_name),
745+
__interfaces__::NamedTuple,
746+
any::GraphPPL.StaticInt{N}
747+
) where {N}
748+
error("Model $(__fform__) is not defined for $(N) interfaces ($(keys(__interfaces__))).")
749+
end
750+
740751
function ($ms_name)(; kwargs...)
741752
return GraphPPL.ModelGenerator($ms_name, kwargs)
742753
end

src/plugins/variational_constraints/variational_constraints.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,4 @@ function postprocess_plugin(plugin::VariationalConstraintsPlugin, model::Model)
9797
end
9898
apply_constraints!(model, GraphPPL.get_principal_submodel(model), plugin.constraints)
9999
materialize_constraints!(model)
100-
end
100+
end

test/model_generator_tests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ end
158158
return (a = 2, observation = 3)
159159
end
160160
# Too many keys, `c = 1` is extra
161-
@test_throws MethodError create_model(generator) do model, ctx
161+
@test_throws "Model simple_model_for_model_generator is not defined for 4 interfaces ((:c, :a, :b, :observation))." create_model(
162+
generator
163+
) do model, ctx
162164
return (a = 1, b = 2, observation = 3)
163165
end
164166
end

test/model_macro_tests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,3 +2016,22 @@ end
20162016

20172017
@test default_backend(hello) === TestUtils.TestGraphPPLBackend()
20182018
end
2019+
2020+
@testitem "error message for other number of interfaces" begin
2021+
using GraphPPL
2022+
using Distributions
2023+
2024+
import GraphPPL: @model
2025+
2026+
@model function somemodel(a, b, c)
2027+
a ~ Normal(b, c)
2028+
end
2029+
2030+
@test_throws "Model somemodel is not defined for 2 interfaces" GraphPPL.create_model(somemodel(a = 1, b = 2))
2031+
2032+
@model function somemodel(a, b)
2033+
c ~ Normal(b, a)
2034+
end
2035+
2036+
@test !isnothing(GraphPPL.create_model(somemodel(a = 1, b = 2)))
2037+
end

0 commit comments

Comments
 (0)