Background
Algorithm options are now validated by pydantic via validated_dataclass in optimagic/typing.py (see also #694 for the option dataclasses).
optimagic/constraints.py contains ~20 __post_init__ methods. A part of what they do is per-attribute type checking that pydantic could take over, so the constraint classes are natural candidates for the same treatment.
Why this should wait
The typed-constraints refactor is currently in flight as a stack of branches touching exactly this file. Migrating the validation now would create constant rebase conflicts. This issue should be tackled after the constraints refactor stack has been merged.
Notes for implementation
- Most constraint
__post_init__ logic is semantic (bounds relations, weight lengths, index compatibility), which stays in __post_init__ — pydantic dataclasses run it after field validation. The win is smaller than for the option dataclasses, so evaluate per class whether the migration pays off.
- Constraint fields hold PyTrees and selector callables; fields where pydantic adds no value can keep plain annotations (
Any, Callable) and rely on the semantic checks.
- Translate
pydantic.ValidationError into InvalidConstraintError to keep the public exception contract.
Background
Algorithm options are now validated by pydantic via
validated_dataclassinoptimagic/typing.py(see also #694 for the option dataclasses).optimagic/constraints.pycontains ~20__post_init__methods. A part of what they do is per-attribute type checking that pydantic could take over, so the constraint classes are natural candidates for the same treatment.Why this should wait
The typed-constraints refactor is currently in flight as a stack of branches touching exactly this file. Migrating the validation now would create constant rebase conflicts. This issue should be tackled after the constraints refactor stack has been merged.
Notes for implementation
__post_init__logic is semantic (bounds relations, weight lengths, index compatibility), which stays in__post_init__— pydantic dataclasses run it after field validation. The win is smaller than for the option dataclasses, so evaluate per class whether the migration pays off.Any,Callable) and rely on the semantic checks.pydantic.ValidationErrorintoInvalidConstraintErrorto keep the public exception contract.