Skip to content

Commit c538d83

Browse files
authored
Merge pull request #254 from ReactiveBayes/error-hint-determentistic-mapping
Give a hint if a user uses = operator wrong
2 parents b5521a5 + 78b22ca commit c538d83

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/GraphPPL.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,20 @@ macro model(model_specification)
5050
return esc(GraphPPL.model_macro_interior(DefaultBackend, model_specification))
5151
end
5252

53+
function __init__()
54+
if isdefined(Base.Experimental, :register_error_hint)
55+
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
56+
if any(x -> x <: VariableRef, argtypes)
57+
print(io, "\nOne of the arguments to ")
58+
printstyled(io, "`$(exc.f)`", color = :cyan)
59+
print(io, " is of type ")
60+
printstyled(io, "`GraphPPL.VariableRef`", color = :cyan)
61+
print(io, ". Did you mean to create a new random variable with ")
62+
printstyled(io, "`:=`", color = :cyan)
63+
print(io, " operator instead?")
64+
end
65+
end
66+
end
67+
end
68+
5369
end # module

test/model_macro_tests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,3 +2030,16 @@ end
20302030

20312031
@test !isnothing(GraphPPL.create_model(somemodel(a = 1, b = 2)))
20322032
end
2033+
2034+
@testitem "model should warn users against incorrect usages of `=` operator with random variables" begin
2035+
using GraphPPL, Distributions
2036+
import GraphPPL: @model
2037+
2038+
@model function somemodel()
2039+
a ~ Normal(0, 1)
2040+
t = exp(a)
2041+
y ~ Normal(0, t)
2042+
end
2043+
2044+
@test_throws "One of the arguments to `exp` is of type `GraphPPL.VariableRef`. Did you mean to create a new random variable with `:=` operator instead?" GraphPPL.create_model(somemodel())
2045+
end

0 commit comments

Comments
 (0)