Fix algo option type conversion being silently skipped with postponed annotations#691
Open
janosg wants to merge 1 commit into
Open
Fix algo option type conversion being silently skipped with postponed annotations#691janosg wants to merge 1 commit into
janosg wants to merge 1 commit into
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.
... and 2 files with indirect coverage changes 🚀 New features to boost your workflow:
|
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