Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error hint for MethodErrors on LogDensityModel #146

Merged
merged 6 commits into from
Sep 12, 2024
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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name = "AbstractMCMC"
uuid = "80f14c24-f653-4e6a-9b94-39d6b0f70001"
keywords = ["markov chain monte carlo", "probablistic programming"]
keywords = ["markov chain monte carlo", "probabilistic programming"]
license = "MIT"
desc = "A lightweight interface for common MCMC methods."
version = "5.2.0"
version = "5.3.0"

[deps]
BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66"
Expand Down
14 changes: 14 additions & 0 deletions src/AbstractMCMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,18 @@ include("stepper.jl")
include("transducer.jl")
include("logdensityproblems.jl")

if isdefined(Base.Experimental, :register_error_hint)
function __init__()
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, _
if Base.parentmodule(exc.f) == LogDensityProblems &&
any(a -> a <: LogDensityModel, argtypes)
print(
io,
"\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)`.",
)
end
end
end
end

end # module AbstractMCMC
9 changes: 9 additions & 0 deletions test/logdensityproblems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
@test model.logdensity === ℓ

@test_throws ArgumentError AbstractMCMC.LogDensityModel(mylogdensity)

try
LogDensityProblems.logdensity(model, ones(10))
catch exc
@test exc isa MethodError
if isdefined(Base.Experimental, :register_error_hint)
@test occursin("is a wrapper", sprint(showerror, exc))
end
end
end

@testset "fallback for log densities" begin
Expand Down
Loading