Skip to content

Prevent spurious error hints #164

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

Merged
merged 1 commit into from
Jun 13, 2025
Merged

Prevent spurious error hints #164

merged 1 commit into from
Jun 13, 2025

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Jun 13, 2025

The error hint for MethodError (see #146) is indiscriminate in that it catches any MethodError. We can't do much about that, but we can try to restrict it to MethodErrors which only involve one of the arguments being a LogDensityModel.

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

Unfortunately, Base.parentmodule is not defined for callable structs, so if you called a callable struct with the wrong arguments and you happened to have AbstractMCMC in your environment, this error hint would try to take over and would itself error. I discovered this when calling (::DynamicPPL.Model)(args...) with the wrong combination of arguments.

This PR fixes it by (1) reversing the order of checks, so that it shortcircuits if none of the arguments are LogDensityModels; and also (2) explicitly checking that exc.f is a function, because the things we want to catch are LogDensityProblems.foo which are indeed functions. Both together might be a bit overkill, but anyway.

Copy link
Contributor

AbstractMCMC.jl documentation for PR #164 is available at:
https://TuringLang.github.io/AbstractMCMC.jl/previews/PR164/

@penelopeysm penelopeysm merged commit 0c5eec2 into main Jun 13, 2025
19 checks passed
@penelopeysm penelopeysm deleted the py/fix-error-hint branch June 13, 2025 22:36
Copy link

codecov bot commented Jun 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (ececa17) to head (20c0960).
Report is 1 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff     @@
##   main   #164   +/-   ##
===========================
===========================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant