Use pydantic for algo option validation (instead of our own validation logict). - #696
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 2 files with indirect coverage changes 🚀 New features to boost your workflow:
|
timmens
approved these changes
Aug 3, 2026
timmens
left a comment
Member
There was a problem hiding this comment.
Very nice; I couldn't find anything to flag.
janosg
added a commit
that referenced
this pull request
Sep 29, 2026
Brings in #696, which replaces the hand-written option converter table with pydantic validation. PR #691, whose fix for the same problem was the first commit on this branch, was closed in favor of #696, so its changes are dropped here rather than merged: - src/optimagic/type_conversion.py: deleted (superseded by validated_dataclass in optimagic/typing.py), including the TYPE_CONVERTERS_BY_NAME lookup #691 added. - Algorithm.__post_init__: removed, main validates via mark.minimizer instead. - tests/.../test_algorithm_future_annotations.py: deleted. Its coverage of the postponed-annotations path is provided by main's test_algo_option_validation_stringified.py, and its registry-wide int coercion test by test_all_algorithms_can_be_instantiated_with_defaults. Docs conflicts resolved by keeping this branch's docstrings on top of main's widened annotations: - Fides.hessian_update_strategy is now HessianUpdateStrategy, not a bare Literal, so its docstring no longer claims that HessianApproximation instances are unavailable; it explains that Broyden must be passed as an instance because phi has no default. - PygmoMbh.inner_algorithm uses the PygmoAlgorithm alias. Also documents the new constraint that field annotations must be resolvable at runtime in how_to_document_optimizers.md, since pydantic now resolves them and a TYPE_CHECKING-only name in a field type raises on instantiation. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Current situation
Algorithm options are converted and validated by a hand-written converter table (TYPE_CONVERTERS in type_conversion.py) that Algorithm.post_init looks up per field, keyed by type objects for eight supported types. AlgoInfo and InternalOptimizeResult carry ~50 lines of manual isinstance checks each.
Two problems are hard to solve within this design:
Proposed Implementation
Replace our validation logic with pydantic.
Backwards compatibility
mark.minimizer, so no algorithm definition changes.InvalidAlgoOptionError,InvalidAlgoInfoError,TypeErrorforInternalOptimizeResult), including for unknown constructor kwargs (extra="forbid").Remaining breaking changes:
Alternatives
PR #691 patches the string-annotation symptom by adding a second, string-keyed converter table (TYPE_CONVERTERS_BY_NAME) and branching on the annotation kind in post_init. That fixes the silently-skipped coercion but keeps everything else: optional and union types remain unvalidated, coverage stays limited to eight hand-written converters, and two parallel tables must be kept in sync with typing.py forever. This PR solves the whole class of problems, deletes code instead of adding it, and extends naturally to the other validation boilerplate in the codebase (#694, #695). If this is merged, #691 should be closed and #690 rebased onto main afterwards.