]> Piment Noir Git Repositories - freqai-strategies.git/commit
refactor(quickadapter): dispatch optuna_create_sampler via match + assert_never ...
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 22 Jun 2026 10:09:19 +0000 (12:09 +0200)
committerGitHub <noreply@github.com>
Mon, 22 Jun 2026 10:09:19 +0000 (12:09 +0200)
commitdfde6e98c581d7a9d514d570616767f2f5d11292
tree99838de81d7aec178d5f062a0ebd05b7201602bb
parent6bdc61f797c74162ada021afa5f9de0b9862fb47
refactor(quickadapter): dispatch optuna_create_sampler via match + assert_never (#103)

Follow-up to #101. Converts the `optuna_create_sampler` if/elif/else dispatch chain to a `match`/`case` statement using value patterns (`QuickAdapterRegressorV3._OPTUNA_SAMPLERS.<name>`) -- the per-field singleton `Literal[...]` typing introduced by #101 unlocks pyright/mypy exhaustiveness narrowing, so the final `case _: assert_never(sampler)` type-checks as `Never` and catches any future extension of `OptunaSampler` that forgets to add a corresponding match branch.

The `case None:` branch is structurally required (not stylistic): without it, after the four `Literal[...]` value patterns, pyright/mypy would narrow `sampler` to `None`, not `Never`, and `assert_never(sampler)` would fail to type-check. Its presence is what makes the "5th sampler addition -> type error at assert_never" claim effective. The error message inside `case None:` is preserved verbatim from the prior `else: raise ValueError(...)` for user-facing wire compatibility.

Behavior delta is confined to non-`Literal` string inputs (e.g. dynamically-injected `"garbage"`): prior code raised `ValueError` with the supported-values list; new code raises `AssertionError` with the standard `typing.assert_never` message. In practice this path is unreachable from every current in-repo call site -- the sole caller `optuna_create_study` already validates `sampler not in samplers` and raises `ValueError` with the supported-values list before dispatching, so misconfigured `_optuna_config["sampler"]` values surface the old error shape from the upstream gate. The new `AssertionError` would only manifest via a hypothetical future caller that bypasses `optuna_create_study`, which does not exist today.

Pattern parity: the same `assert_never` exhaustiveness idiom is already used in this file for `support_policy` dispatch; value-pattern syntax matches the 8 call sites migrated by #101.

Closes the deferred follow-up from PR #101 (issue #88).
quickadapter/user_data/freqaimodels/QuickAdapterRegressorV3.py