From f73b630cf147b705a572a5679caa95298d00bc83 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 11 Sep 2024 20:56:12 +0100 Subject: [PATCH 1/6] Add hint for MethodErrors on LogDensityModel --- src/AbstractMCMC.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/AbstractMCMC.jl b/src/AbstractMCMC.jl index dc464d42..4a7edd26 100644 --- a/src/AbstractMCMC.jl +++ b/src/AbstractMCMC.jl @@ -88,4 +88,19 @@ include("stepper.jl") include("transducer.jl") include("logdensityproblems.jl") +function __init__() + if isdefined(Base.Experimental, :register_error_hint) + Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, _ + if Base.parentmodule(exc.f) == LogDensityProblems && + any(a -> a <: LogDensityModel, argtypes) + printstyled( + io, + "\nAbstractMCMC.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)`."; + color=:cyan, + ) + end + end + end +end + end # module AbstractMCMC From 4ca17697536791c52e86320d163212144b8957a3 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 11 Sep 2024 22:37:50 +0100 Subject: [PATCH 2/6] Apply suggestions from code review Co-authored-by: David Widmann --- src/AbstractMCMC.jl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/AbstractMCMC.jl b/src/AbstractMCMC.jl index 4a7edd26..d7374dd2 100644 --- a/src/AbstractMCMC.jl +++ b/src/AbstractMCMC.jl @@ -88,15 +88,14 @@ include("stepper.jl") include("transducer.jl") include("logdensityproblems.jl") -function __init__() - if isdefined(Base.Experimental, :register_error_hint) +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) - printstyled( + print( io, - "\nAbstractMCMC.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)`."; - color=:cyan, + "\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 From 63e50dcee221a45924ffc1cfc93594748d404db3 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Wed, 11 Sep 2024 22:38:39 +0100 Subject: [PATCH 3/6] Bump patch version --- Project.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 97c90709..5f98bb3a 100644 --- a/Project.toml +++ b/Project.toml @@ -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.2.1" [deps] BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66" From 6dfce50ff944d2991605c2249360ad9a33e2edc7 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 12 Sep 2024 00:43:44 +0100 Subject: [PATCH 4/6] Bump minor version instead --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 5f98bb3a..60215cec 100644 --- a/Project.toml +++ b/Project.toml @@ -3,7 +3,7 @@ uuid = "80f14c24-f653-4e6a-9b94-39d6b0f70001" keywords = ["markov chain monte carlo", "probabilistic programming"] license = "MIT" desc = "A lightweight interface for common MCMC methods." -version = "5.2.1" +version = "5.3.0" [deps] BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66" From d514c33fec043982e505fe224f71d37c4635f65d Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 12 Sep 2024 00:46:23 +0100 Subject: [PATCH 5/6] Test that error message is correctly thrown --- test/logdensityproblems.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/logdensityproblems.jl b/test/logdensityproblems.jl index 181d2645..0b746d48 100644 --- a/test/logdensityproblems.jl +++ b/test/logdensityproblems.jl @@ -22,6 +22,13 @@ @test model.logdensity === ℓ @test_throws ArgumentError AbstractMCMC.LogDensityModel(mylogdensity) + + try + LogDensityProblems.logdensity(model, ones(10)) + catch exc + @test exc isa MethodError + @test occursin("is a wrapper", sprint(showerror, exc)) + end end @testset "fallback for log densities" begin From 03d0c721bda8712dc872e86775a1d6a6c252ec40 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Thu, 12 Sep 2024 01:23:38 +0100 Subject: [PATCH 6/6] Only test error hint if it can be defined --- test/logdensityproblems.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/logdensityproblems.jl b/test/logdensityproblems.jl index 0b746d48..4a3a22d5 100644 --- a/test/logdensityproblems.jl +++ b/test/logdensityproblems.jl @@ -27,7 +27,9 @@ LogDensityProblems.logdensity(model, ones(10)) catch exc @test exc isa MethodError - @test occursin("is a wrapper", sprint(showerror, exc)) + if isdefined(Base.Experimental, :register_error_hint) + @test occursin("is a wrapper", sprint(showerror, exc)) + end end end