Conversation
… annotations. With `from __future__ import annotations`, dataclass field types are annotation strings rather than type objects, so the TYPE_CONVERTERS lookup in Algorithm.__post_init__ never matched and option coercion/validation was silently skipped for all optimizer modules using the import. Adds a string-keyed TYPE_CONVERTERS_BY_NAME lookup so coercion works identically with and without the import, plus regression tests: a dummy algorithm defined under postponed annotations mirroring the existing type conversion tests, and a registry-wide test that passes floats for all int-typed options of every algorithm. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
Member
Author
|
Closed in favor of #696 |
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.
Problem
Algorithm.__post_init__converts and validates algo option values by looking up each dataclass field's type inTYPE_CONVERTERS, which is keyed by type objects (PositiveInt,int, ...). In modules withfrom __future__ import annotations— which the how-to-document-optimizers guide requires so that autodoc renders type aliases readably —field.typeis the annotation string ("PositiveInt"), so the lookup never matched and coercion/validation was silently skipped.This already affected every optimizer module that uses the import on main (scipy, gfo, nevergrad, pyswarms, pygad, bayesian, pygmo, tranquilo, iminuit) and surfaced as a test failure in #690 when
ipopt.pygained the import:test_ipopt_algo_optionspasses5.5for an integer-typed Ipopt option and relies on coercion to int.Fix
TYPE_CONVERTERS_BY_NAMEintype_conversion.py: a string-keyed companion table with the same converters.Algorithm.__post_init__branches on whetherfield.typeis a string and uses the matching table, so coercion works identically with and without the future import.Tests
test_algorithm_future_annotations.py(new): a dummy algorithm defined under postponed annotations, mirroring the existing type conversion tests intest_algorithm.py, plus a guard test asserting the field types really are annotation strings.test_algorithm.py: a mirror guard asserting the existing dummy's field types are type objects (so the two modules keep covering both code paths), a float→int coercion test, andtest_int_options_are_coerced_for_all_algorithms, which passes floats for all int-typed options of every algorithm inALL_ALGORITHMSand asserts they come back as ints.Without the fix, 97 of the new tests fail (the postponed-annotations module and the registry-wide test for ~90 algorithms); with it, everything passes.
#690 is rebased on top of this branch.
🤖 Generated with Claude Code