Skip to content

Commit 8431b31

Browse files
authored
Add error hint for MethodErrors on LogDensityModel (#146)
1 parent e742318 commit 8431b31

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Project.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name = "AbstractMCMC"
22
uuid = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
3-
keywords = ["markov chain monte carlo", "probablistic programming"]
3+
keywords = ["markov chain monte carlo", "probabilistic programming"]
44
license = "MIT"
55
desc = "A lightweight interface for common MCMC methods."
6-
version = "5.2.0"
6+
version = "5.3.0"
77

88
[deps]
99
BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66"

src/AbstractMCMC.jl

+14
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,18 @@ include("stepper.jl")
8888
include("transducer.jl")
8989
include("logdensityproblems.jl")
9090

91+
if isdefined(Base.Experimental, :register_error_hint)
92+
function __init__()
93+
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, _
94+
if Base.parentmodule(exc.f) == LogDensityProblems &&
95+
any(a -> a <: LogDensityModel, argtypes)
96+
print(
97+
io,
98+
"\n`AbstractMCMC.LogDensityModel` is a wrapper and does not itself implement the LogDensityProblems.jl interface. To use LogDensityProblems.jl methods, access the inner type with (e.g.) `logdensity(model.logdensity, params)` instead of `logdensity(model, params)`.",
99+
)
100+
end
101+
end
102+
end
103+
end
104+
91105
end # module AbstractMCMC

test/logdensityproblems.jl

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
@test model.logdensity ===
2323

2424
@test_throws ArgumentError AbstractMCMC.LogDensityModel(mylogdensity)
25+
26+
try
27+
LogDensityProblems.logdensity(model, ones(10))
28+
catch exc
29+
@test exc isa MethodError
30+
if isdefined(Base.Experimental, :register_error_hint)
31+
@test occursin("is a wrapper", sprint(showerror, exc))
32+
end
33+
end
2534
end
2635

2736
@testset "fallback for log densities" begin

0 commit comments

Comments
 (0)