Constraints refactor 3 typed resolution - #688
Merged
Merged
Conversation
Create the parameters/constraints subpackage that will host the refactored constraints machinery, starting with the first pipeline stage: - types.py: ConstraintSource provenance (user constraint + position) and one frozen Resolved* dataclass per constraint kind, carrying flat parameter positions as read-only arrays. Linear weights are aligned and broadcast at resolution time; absent bounds/values are inf/nan sentinels instead of missing dict keys. - resolution.py: resolve_constraints evaluates selectors on the positional helper tree and returns typed resolved constraints. Error messages now cite the originating user constraint. The loc/query branches are gone from this stage (the deprecation adapter owns them since the previous commit). - process_selectors.py is deleted; conversion.py calls resolve_constraints and converts the result to legacy dicts right before the (still dict-based) process_constraints via the temporary to_legacy_dicts seam. The new modules are fully typed and not exempted from strict mypy checking. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
janosg
force-pushed
the
constraints-refactor-3-typed-resolution
branch
from
July 3, 2026 15:57
d0d54aa to
c561968
Compare
Codecov Report❌ Patch coverage is
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
- ResolvedConstraint is now an ABC that all Resolved* dataclasses subclass, mirroring the Constraint base class, instead of a union type alias. - The index and weights arrays are no longer made read-only; the frozen dataclasses provide enough immutability. __post_init__ still normalizes inputs to properly typed arrays. - to_legacy_dicts dispatches on ResolvedLinear explicitly because the ABC, unlike the union, does not let mypy narrow the final else branch. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Constraint gets an abstract _resolve method that each constraint class implements by constructing its Resolved* counterpart, replacing the isinstance dispatch chains in resolution.py. A frozen ResolutionContext (helper tree, registry, param names, provenance source) bundles everything the methods need and owns the selector evaluation and duplicate check. The Resolved* dataclasses, ConstraintSource, and the array aliases move from parameters/constraints/types.py into optimagic.constraints, each Resolved* class directly after its user-facing counterpart. This keeps all import edges pointing in the existing direction (pipeline modules import optimagic.constraints) and avoids a circular import; types.py is deleted. Cleanups that fall out: the type-ignore on constraint.selector and the getattr value hack for FixedValueConstraint are gone (the deprecated subclass overrides _resolve); linear weight alignment lives on LinearConstraint; NonlinearConstraint._resolve raises the same InvalidConstraintError as the old fallback branch. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
ResolvedFixed -> ResolvedFixedConstraint etc.; in particular the covariance and sdcorr classes now mirror the Flat* user classes (ResolvedFlatCovConstraint, ResolvedFlatSDCorrConstraint). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Nonlinear constraints are passed directly to optimizers that support them and are split off before resolution, so reaching NonlinearConstraint._resolve is an internal error, not an invalid user constraint. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
janosg
force-pushed
the
constraints-refactor-3-typed-resolution
branch
from
July 3, 2026 19:09
c3a905c to
ce090ad
Compare
Each constraint class's _resolve is now tested directly against a small ResolutionContext: typed result with positions and provenance, empty selections returning None, regularization carry-over, linear weight broadcasting/alignment and bound sentinels, pairwise length validation, and the NotImplementedError for nonlinear constraints. test_resolution.py keeps only the general end-to-end behavior of resolve_constraints (tree selectors, provenance positions, dropped empty selections, duplicate and failing selectors) plus the temporary to_legacy_dicts seam. Co-Authored-By: Claude Fable 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.
Part 3 of the constraints refactoring stack (follows #686 and #687).
This PR creates the
optimagic.parameters.constraintssubpackage that will hostthe refactored constraints machinery, and replaces the first pipeline stage —
selector processing — with typed resolution.