Skip to content

Provide a clear error when the solver is not instantiated.#762

Open
SwissChardLeaf wants to merge 1 commit into
patrick-kidger:mainfrom
SwissChardLeaf:fix/uninstantiated-solver-error
Open

Provide a clear error when the solver is not instantiated.#762
SwissChardLeaf wants to merge 1 commit into
patrick-kidger:mainfrom
SwissChardLeaf:fix/uninstantiated-solver-error

Conversation

@SwissChardLeaf

Copy link
Copy Markdown

Fixes #705. Incorporates discussion from #706.

Hello Diffrax community. This is my first contribution attempt!

Summary

When solver is passed as a class rather than an instance (e.g. diffrax.EulerHeun instead of diffrax.EulerHeun()), diffeqsolve previously failed with obscure errors such as AssertionError or TypeError: functools.partial() argument after ** must be a mapping, not property.
This adds an early _validate_solver check that raises a clear ValueError with a concrete example.

Relation to #706

This implements the approach discussed in #706, incorporating Patrick's review feedback on that PR:

  • Guard issubclass with isinstance(solver, type) so non-class inputs do not raise TypeError.
  • Use diffrax.Euler() in the error message rather than dfx.Euler().

Design notes (open to feedback)

1. Widened solver annotation
The solver parameter is annotated as AbstractSolver | type[AbstractSolver] so that beartype allows uninstantiated solver classes through to _validate_solver, which then raises the intended ValueError. Without this, beartype rejects the call before our check runs.
2. validated_solver
After validation, the solver is stored in a separate validated_solver: AbstractSolver variable used for the remainder of diffeqsolve. This works well with pyright, even with the widened input type annotation.

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.

Obscure error for passing EulerHeun instead of EulerHeun()

1 participant