Replace truthiness guards on streamed tool-call `function` deltas with explicit `is not None` checks
This pull request was posted by Claude Code using claude-fable-5 on behalf of David.
Three streamed tool-call-delta loops passed dtc.function and dtc.function.name (and ... and dtc.function.arguments) to ModelResponsePartsManager.handle_tool_call_delta. Truthiness narrowing types that expression as Function | str | None — if a Function subclass overrode __bool__, the object itself would flow through as tool_name. The explicit dtc.function.name if dtc.function is not None else None types as exactly str | None and states the actual condition (presence of function, not its truthiness). A shorter ... or None rewrite would be wrong: streamed deltas legitimately carry '', which or would coerce to None, and the parts manager distinguishes the two.
Surfaced while running ty over the repo (#3970): ty and mypy both reject the truthiness form; only pyright accepts it. The explicit form is the remedy the ty maintainers recommend (astral-sh/ty#2432 discussion).
Notes:
huggingface.py: the SDK declaresChatCompletionStreamOutputDeltaToolCall.functionas required, but the API sends chunks without it —test_stream_structuredconstructsfunction=Nonechunks and fails on direct attribute access. The guard therefore stays, now with# pyright: ignore[reportUnnecessaryComparison]documenting the wrong SDK declaration (replacing the previousreportArgumentTypeignore on the truthiness form).cohere.py'sif c.function and c.function.name and c.function.arguments:is left as-is deliberately — it's a boolean condition, not a value being passed, so truthiness is semantically fine there.- Tests: the mock-stream tool-call-delta tests are extracted from the three provider files into
tests/models/test_streamed_tool_call_deltas.py, parametrized over the three providers with per-SDK chunk builders (3 scenarios × 3 providers), and extended with the edge shapes this diff touches —functionpresent,function=None, and empty-stringname/argumentsdeltas, pinning that''reaches the parts manager as''and never becomesNone. Net −124/−59/−122 lines fromtest_groq.py/test_openai.py/test_huggingface.py; missing-line coverage of the three modules is byte-identical before and after the move.
Why this cannot change any payload
- No
Nonecheck is being added. All three sites already guarded withdtc.function and ...; this diff only changes the spelling of the existing guard. - The two forms can only diverge on a falsy-but-not-
Nonefunction. OpenAI's and Groq'sChoiceDeltaToolCallFunctionare pydanticBaseModels that define neither__bool__nor__len__, and Hugging Face's is a plain dataclass — instances of all three are unconditionally truthy, so the diverging input cannot be constructed from these SDKs. For every constructible input the two forms produce identical values:None → None, instance →.name/.arguments. - The
Nonearm is real and already pinned per provider:tests/models/test_openai.py,tests/models/test_groq.py, andtests/models/test_huggingface.pyall streamfunction=Nonechunks and snapshot the resulting parts. All of them pass unchanged on this diff.