ADR 0005 — Typed values in registry options: str or list[str]¶
- Status: Accepted (2026-10)
- Contracts: none (see Decision 4)
- Supersedes: —
- Issue / PRs: #260; follows #258 (JSON argv inside the
commandstring, never released)
Context¶
Registry-dispatched backends (QMIO, the "subprocess" bridge, any third-party
backend registered with register_backend) receive their provider-specific
configuration through an options bag. Until now it was dict[str, str] end to
end: HashMap<String, String> in BackendConfig::Registered and in
BackendBuildContext, read with BackendBuildContext::option(key) ->
Option<&str>.
A string cannot carry an argv. The subprocess bridge's command was split on
whitespace (v0.7.2), so a worker or interpreter under a path with spaces could
not be launched. #258 worked around it by parsing a value that starts with [ as
a JSON array of strings: a second, ad-hoc type system inside a string, with its
own error modes, that every third-party backend needing a list would have had to
reinvent. That form landed on main after v0.7.2 and has not been released.
option() also returned None for an absent key only, so the backends' rule
"absent key → default; present but malformed → error" held. Widening the value
type must keep it.
Decision¶
- One value type,
polypus_backend::OptionValue—Str(String)orList(Vec<String>),Debug + Clone + PartialEq + Eq, withFrom<&str>,From<String>andFrom<Vec<String>>. It lives in the pyo3-freepolypus-backendand adds no dependency.BackendBuildContext.optionsandBackendConfig::Registered.optionsbecomeHashMap<String, OptionValue>. Notserde_json::Value(an untyped tree every factory would have to validate, and a dependency of the public contract) and not a second map for lists (two places to look for one key, and a key could be in both). - Two fallible, symmetric accessors replace
option().option_str(key) -> Result<Option<&str>, BackendError>andoption_list(key) -> Result<Option<&[String]>, BackendError>:Ok(None)for an absent key,Ok(Some(_))for a value of the expected shape, andBackendError::Conversionnaming the key for the other shape. A lenientoption()that returnedNonefor a list would bring back the silent default the backends avoid.option()is removed rather than kept beside them. - The JSON-in-a-string
commandis withdrawn now.commandis astr, split on whitespace without quotes or escapes (as in v0.7.2), or alist[str], the exact argv. An empty list or an emptyargv[0]is aBackendError. A string starting with[is split on whitespace like any other string, with no warning or error: there is no heuristic guessing it is a leftover JSON argv. - No new contract.
optionsdoes not appear indocs/CONTRACTS.md(C-1 is thepolypus_pythonseam, which this does not touch). The Python→Rust edge and theBackendBuildContextshape are documented indocs/backends.mdand here. - At the Python edge,
run_quantum_circuit,trainandqml.trainacceptoptions: dict[str, str | list[str]]. The conversion lives in thepolypuscrate (a localFromPyObjectnewtype, since both the trait andOptionValueare foreign to it). It walks a snapshot of the dict's items and reads each list/tuple by index, so no user code (an overridden__iter__) runs while the dict is walked — a dict mutated mid-walk would otherwise panic inside PyO3. OptionValueis#[non_exhaustive]. A further value shape (an integer, a nested map) can be added without another breaking change. The cost: code outsidepolypus-backendthatmatches on it needs a wildcard arm (the subprocess bridge'scommandreader has one). Factories are expected to use the accessors, notmatch.- An empty list is accepted at the edge. Whether it means anything is the
backend's call: the subprocess bridge rejects an empty
command. - Accepted Python values are
strandlist/tupleofstr. Everything else —int,bool,float,None,dict, a list holding a non-str— is aTypeErrornaming the key (options['k'] must be a str or a list of str, got int;options['k'][1] must be a str, got NoneType). PyO3 already raisedTypeErrorfor a non-strvalue, so the exception type is unchanged; numbers are not coerced to strings (see Alternatives considered 5). Relaxing this later (coercing numbers) would break no caller, so it can be its own change.
Decisions 6, 7 and 8, and the absence of a heuristic in 3, were proposed by the planner of #260 and confirmed by the maintainers on review (2026-10-09).
Compatibility¶
Relative to v0.7.2 (the last release):
| Who | Breaks | Migration |
|---|---|---|
| Rust factory reading an option | ctx.option(key) no longer exists |
ctx.option_str(key)? (now Result<Option<&str>, _>); ctx.option_list(key)? for a list |
Rust code building a BackendBuildContext or BackendConfig::Registered |
options is HashMap<String, OptionValue> |
wrap values: OptionValue::from("…"), OptionValue::from(vec![…]) |
Python caller with str values |
nothing | — |
Python caller passing a non-str value |
still a TypeError; the message now names the key |
— |
Subprocess command as a string |
nothing (same whitespace split as v0.7.2) | use a list for any argument containing a space |
The JSON-array string from #258 is removed without a deprecation period because it
never reached a release. A main user who wrote json.dumps([...]) must pass the
list itself.
Consequences¶
- A backend that needs a list reads it with
option_listand gets type checking for free; none needs its own string encoding. - A list where a factory reads a string (QMIO's
endpoint, the bridge'srecv_timeout_ms, …) is aBackendError::Conversionnaming the key, raised aspolypus.BackendError, never a silent default. polypus-backendstays pyo3-free and gains no dependency.- Every third-party factory using
option()has to change one call per key. This is the price of D2, accepted over a tolerant accessor. local/cunqakeep rejecting a non-emptyoptions(withValueError), whatever the value types.
Alternatives considered¶
- Keep
dict[str, str]and the JSON-in-a-string argv (#258). A private encoding per backend, invisible in the type, and a string that happens to start with[changes meaning. serde_json::Valueas the value type. Any shape, but an untyped tree every factory has to validate, andserde_jsonin the public contract's API.- A second
list_optionsmap. No enum, but two lookups for one key and an ambiguous key present in both. - Keep
option()beside the new accessors, returningNonefor a list. No break for existing factories, but a list in a string key would silently become the default. - Coerce Python numbers and booleans to strings at the edge. Convenient
(
{"recv_timeout_ms": 600000}), butTrue→"True"and1.0→"1.0"are spellings each backend would then have to accept; kept out of scope.
Reopening criteria¶
- A backend needs a value that is neither a string nor a list of strings (a
number with its type preserved, a nested map): add a variant (non-breaking,
thanks to
#[non_exhaustive]) or reconsider alternative 2. optionsbecomes part of a seam contract: move this intodocs/CONTRACTS.mdwith its enforcing test.