Skip to content

Commit

Permalink
Add error hint for MethodErrors on LogDensityModel (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
penelopeysm authored Sep 12, 2024
1 parent e742318 commit 8431b31
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
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

5 comments on commit 8431b31

@penelopeysm
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: Register Failed
@penelopeysm, it looks like you are not a publicly listed member/owner in the parent organization (TuringLang).
If you are a member/owner, you will need to change your membership to public. See GitHub Help

@penelopeysm
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come I could do it before? :/

@penelopeysm
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/115049

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v5.3.0 -m "<description of version>" 8431b3124dc68214211db519f142ad48ab6894cc
git push origin v5.3.0

Please sign in to comment.