Skip to content

Commit

Permalink
added testing of deprecation warning of @submodel + replaced some
Browse files Browse the repository at this point in the history
usages in tests (though we don't support some of these so we cant' do
that yet)
  • Loading branch information
torfjelde committed Nov 15, 2024
1 parent 99d99b3 commit 0597b2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/submodel_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function prefix_submodel_context(prefix::Bool, ctx)
return ctx
end

const SUBMODEL_DEPWARN_MSG = "`@submodel model` is deprecated, use `left ~ to_sampleable(model)` instead."
const SUBMODEL_DEPWARN_MSG = "`@submodel model` and `@submodel prefix=... model` are deprecated, use `left ~ to_sampleable(model)` and `left ~ to_sampleable(prefix(model, ...))`, respectively, instead."

function submodel(prefix_expr, expr, ctx=esc(:__context__))
prefix_left, prefix = getargs_assignment(prefix_expr)
Expand Down
23 changes: 22 additions & 1 deletion test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,27 @@ module Issue537 end
@test demo2()() == 42
end

@testset "@submodel is deprecated" begin
@model inner() = x ~ Normal()
@model outer() = @submodel x = inner()
@test_logs(
(
:warn,
"`@submodel model` and `@submodel prefix=... model` are deprecated, use `left ~ to_sampleable(model)` and `left ~ to_sampleable(prefix(model, ...))`, respectively, instead.",
),
outer()()
)

@model outer_with_prefix() = @submodel prefix="sub" x = inner()
@test_logs(
(
:warn,
"`@submodel model` and `@submodel prefix=... model` are deprecated, use `left ~ to_sampleable(model)` and `left ~ to_sampleable(prefix(model, ...))`, respectively, instead.",
),
outer_with_prefix()()
)
end

@testset "submodel" begin
# No prefix, 1 level.
@model function demo1(x)
Expand Down Expand Up @@ -469,7 +490,7 @@ module Issue537 end
num_steps = length(y[1])
num_obs = length(y)
@inbounds for i in 1:num_obs
@submodel prefix = "ar1_$i" x = AR1(num_steps, α, μ, σ)
x ~ to_sampleable(prefix(AR1(num_steps, α, μ, σ), "ar1_$i"))
y[i] ~ MvNormal(x, 0.01 * I)
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/debug_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
@testset "submodel" begin
@model ModelInner() = x ~ Normal()
@model function ModelOuterBroken()
@submodel z = ModelInner()
z ~ to_sampleable(ModelInner())
return x ~ Normal()
end
model = ModelOuterBroken()
@test_throws ErrorException check_model(model; error_on_failure=true)

@model function ModelOuterWorking()
@submodel prefix = true z = ModelInner()
z = to_sampleable(prefix(ModelInner(), "z"))
x ~ Normal()
return z
end
Expand Down

0 comments on commit 0597b2a

Please sign in to comment.