Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/model_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,13 @@ function model_macro_interior(backend_type, model_specification)

num_interfaces = Base.length(ms_args)
if !isnothing(ms_kwargs) && length(ms_kwargs) > 0
@warn("Model specification language does not support keyword arguments. Ignoring $(length(ms_kwargs)) keyword arguments.")
# Keyword arguments in a model signature were previously parsed and then silently dropped, with only
# a warning. The body never saw them, so the only real signal was a later `UndefVarError`. Fail here
# instead, consistent with how unsupported positional arguments are rejected at the call site.
kwargs_names = map(kwarg -> (kwarg isa Expr && kwarg.head === :kw) ? kwarg.args[1] : kwarg, ms_kwargs)
error(
"The `$(ms_name)` model macro does not support keyword arguments in the model signature, but got $(length(ms_kwargs)): $(join(kwargs_names, ", ")). Declare all model interfaces as positional arguments, `$(ms_name)($(join(ms_args, ", ")))`, they are passed by name at the call site."
)
end

boilerplate_functions = GraphPPL.get_boilerplate_functions(backend_type, ms_name, ms_args, num_interfaces)
Expand Down
25 changes: 25 additions & 0 deletions test/model_macro_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2070,3 +2070,28 @@ end
somemodel()
)
end

@testitem "`@model` should reject keyword arguments in the model signature" begin
import GraphPPL: model_macro_interior

include("testutils.jl")

# Keyword arguments used to be parsed and then silently dropped with only a warning, so the body
# never saw them and the user's only signal was a later `UndefVarError`
kwargs_spec = :(function model_with_kwargs(x; a = 1, b = 2)
y ~ Normal(x, a)
end)

@test_throws "does not support keyword arguments in the model signature" model_macro_interior(
TestUtils.TestGraphPPLBackend, kwargs_spec
)
# the offending keyword arguments are named, and the supported form is shown
@test_throws "but got 2: a, b" model_macro_interior(TestUtils.TestGraphPPLBackend, kwargs_spec)
@test_throws "model_with_kwargs(x)" model_macro_interior(TestUtils.TestGraphPPLBackend, kwargs_spec)

# positional-only signatures are unaffected
positional_spec = :(function model_without_kwargs(x, a)
y ~ Normal(x, a)
end)
@test model_macro_interior(TestUtils.TestGraphPPLBackend, positional_spec) isa Expr
end
Loading