yetuge 0eed90df3a
refactor(config): converge the boolean guards into one shared helper (#6226)
* refactor(config): converge the boolean guards into one shared helper

Willem-bd's #6171 review follow-up: a tiny shared helper lets the next numeric field get the guard in one line and unifies the messages. Consolidates 14 validator copies across 9 config files into deerflow/config/_boolean_guards.reject_boolean(value, info, kind=...); the bare per-file messages gain their field name prefix, command_timeout keeps its distinct wording.

Signed-off-by: yetuge <2219677952@qq.com>

* refactor(config): converge stream_bridge_config and pin the guard wording

Review follow-ups on #6226: convert the last surviving per-file copy (stream_bridge_config) so deerflow/config is fully converged, make kind required keyword-only so a float field cannot silently inherit integer wording, and pin the unified field-prefixed messages with direct tests.

Signed-off-by: yetuge <2219677952@qq.com>

* test(config): cast the boolean-guard test probe to ValidationInfo

The _info probe was annotated -> object, so a type checker flags every call
site that passes its result to reject_boolean's info: ValidationInfo
parameter (runtime was fine since the helper only reads .field_name).
Return cast(ValidationInfo, _Probe(field_name)) to keep the probe honest
for both.

Signed-off-by: yetuge <2219677952@qq.com>

---------

Signed-off-by: yetuge <2219677952@qq.com>
2026-10-04 09:09:15 +08:00

38 lines
1.7 KiB
Python

"""Shared ``mode="before"`` field validators that reject booleans for numeric config fields.
Pydantic coerces ``true``/``false`` into ``1``/``0`` for int and float fields, so a boolean in
config silently flips behavior — capping a loop at one step, disabling retries, serializing
concurrent calls, or skipping a health check (#6017, #6171). Register one guard per numeric
field instead of copying the check into another config file:
@field_validator("max_concurrent_calls", mode="before")
@classmethod
def _reject_boolean_llm_call_settings(cls, value: object, info: ValidationInfo) -> object:
return reject_boolean(value, info, kind="an integer")
"""
from __future__ import annotations
from pydantic import ValidationInfo
def reject_boolean(value: object, info: ValidationInfo, *, kind: str) -> object:
"""Reject a boolean for the numeric config field named by ``info.field_name``.
Args:
value: the raw field value, before Pydantic's numeric coercion.
info: the validation context carrying the field name.
kind: the wording after "must be" — ``"an integer"`` for int fields,
``"a number"`` for float fields, or a custom phrase such as
``"a number of seconds or null"``. Required, so wiring a float
field cannot silently inherit integer wording.
Raises:
ValueError: when ``value`` is a bool. ``bool`` is an ``int`` subclass,
so without this guard Pydantic coerces ``true``/``false`` into
``1``/``0`` before ``ge``/``le`` constraints ever run.
"""
if isinstance(value, bool):
raise ValueError(f"{info.field_name} must be {kind}, not a boolean")
return value