Repository navigation
More consistent error types - #2058
Merged
Merged
Conversation
Distribution constructors now throw a consistent error type based on the nature of the violation: * `DomainError(val, msg)` for numerically invalid parameter values. For relational checks across multiple parameters (e.g. `a < b` in Uniform, `lower ≤ upper` in Censored, `1 ≤ rank ≤ n` in OrderStatistic), the named tuple of related parameters is passed as `err.val` so downstream catchers can identify each value by name. * `DimensionMismatch` for structural size checks in constructors — rows/cols of `M` vs `U`/`V`/`Σ`/`Ω` in `MatrixNormal`/`MatrixTDist`, and length of support vs probability vector in `DiscreteNonParametric`. This matches the idiom already used across the library for `suffstats`/`fit_mle`. * `ArgumentError` is retained only for non-parameter, non-dimension issues (empty data in `fit_mle`, invalid enum in `_char_uplo`, etc.). The `@check_args` macro is collapsed to two `DomainError`-based forms, `(arg, cond, message)` and `(arg, cond)`, both requiring an offending value. The previous no-value forms that fell back to `ArgumentError` have been removed; the macro now errors at expansion time if a caller omits the value. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2058 +/- ##
==========================================
- Coverage 86.65% 86.63% -0.02%
==========================================
Files 149 149
Lines 8925 8921 -4
==========================================
- Hits 7734 7729 -5
- Misses 1191 1192 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
devmotion
marked this pull request as ready for review
April 23, 2026 07:38
# Conflicts: # src/utils.jl
) `@check_args` forwards the explicitly-named argument of each check to `_arg_checkable` to decide whether the check can run at all; under the global `TracerSparsityDetector` a primal-free tracer is not checkable, so the check is skipped and construction can be traced. Relational checks over several parameters (e.g. `Uniform`'s `a < b`) name them with a named tuple such as `(; a, b)`. A `NamedTuple`/`Tuple` hit the default `_arg_checkable(_) = true`, so those checks still ran on tracers and errored. Recurse `_arg_checkable` through `Tuple`/`NamedTuple` so such an argument is checkable only when all of its elements are. This fixes global gradient tracing through the real-parameter constructors `Uniform`, `Arcsine`, `LogUniform`, `Censored` and `TriangularDist`, with regression tests (including non-dense patterns to confirm dependencies are tracked, not just that tracing succeeds). `DiscreteUniform` and `OrderStatistic` use the same named-tuple form but their checked parameters are integer-valued and can never be tracers, so global tracing does not apply to them; the recursion handles their named tuples consistently regardless. Also bump the version. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
andreasnoack
approved these changes
Jun 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In a numerical estimation problem, I encountered numerical issues resulting in
NaNparameters which then threw aDomainErrorwhen constructing aNormaldistributions. So fine, so good - I could easily handle it by catching and handling theDomainErrorgracefully. Only later I noticed that similar numerical problems withUniformwill lead to anArgumentError.I think it's more convenient and more predictable for downstream users and packages if incorrect/infeasible parameter values always lead to
DomainError.Changes
@check_argsnow requires every check to name the offending value(s) and always throws aDomainErrorcarrying that value. The previous bare-condition and(cond, message)(ArgumentError) forms are removed; relational checks over several parameters (e.g.a < b) name them with a named tuple such as(; a, b)so downstream catchers can read the individual parameters offerr.val.ArgumentErrors for infeasible parameters were converted toDomainError(orDimensionMismatchwhere a check is really about sizes/lengths, e.g.MatrixNormal,DiscreteNonParametric).Merge with master / sparsity tracing
After merging master (which added global sparsity tracing through
@check_argsvia_arg_checkable, #2064), the new named-tuple arguments needed to participate in the tracer-skip decision: aNamedTuplehit the default_arg_checkable(_) = true, so the relational checks still ran on primal-free tracers and errored.This is fixed by recursing
_arg_checkablethroughTuple/NamedTuple, so such an argument is checkable only when all of its elements are. Global gradient tracing now works through the real-parameter constructorsUniform,Arcsine,LogUniform,CensoredandTriangularDist. (DiscreteUniformandOrderStatisticuse the same named-tuple form, but their checked parameters are integer-valued and can never be tracers, so global tracing does not apply to them; the recursion handles their named tuples consistently regardless.)Because every multi-variable relational check now names all of its variables via a named tuple, the caveat noted in #2067 — that a cross-argument named condition could still slip through — no longer applies anywhere.
Added regression tests covering:
minimum/maximum/mode) to confirm dependencies are tracked rather than collapsing to a fully dense Jacobian;check_args=falsebypass under the local detector (primal present, so checks are not auto-skipped) with infeasible values, for both the scalar check (Normal'sσ ≥ 0) and the named-tuple check (Uniform'sa < b).Closes #2067.