Skip to content

Commit ccefddc

Browse files
authored
Fix UIAdapter.dispatch_request typing (#3721)
1 parent a0e8b83 commit ccefddc

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

pydantic_ai_slim/pydantic_ai/ui/_adapter.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Generic,
1414
Protocol,
1515
TypeVar,
16+
cast,
1617
runtime_checkable,
1718
)
1819

@@ -43,7 +44,6 @@
4344
'StateDeps',
4445
]
4546

46-
4747
RunInputT = TypeVar('RunInputT')
4848
"""Type variable for protocol-specific run input types."""
4949

@@ -53,10 +53,12 @@
5353
EventT = TypeVar('EventT')
5454
"""Type variable for protocol-specific event types."""
5555

56-
5756
StateT = TypeVar('StateT', bound=BaseModel)
5857
"""Type variable for the state type, which must be a subclass of `BaseModel`."""
5958

59+
DispatchDepsT = TypeVar('DispatchDepsT')
60+
"""TypeVar for deps to avoid awkwardness with unbound classvar deps."""
61+
6062

6163
@runtime_checkable
6264
class StateHandler(Protocol):
@@ -328,18 +330,18 @@ async def dispatch_request(
328330
cls,
329331
request: Request,
330332
*,
331-
agent: AbstractAgent[AgentDepsT, OutputDataT],
333+
agent: AbstractAgent[DispatchDepsT, OutputDataT],
332334
message_history: Sequence[ModelMessage] | None = None,
333335
deferred_tool_results: DeferredToolResults | None = None,
334336
model: Model | KnownModelName | str | None = None,
335-
instructions: Instructions[AgentDepsT] = None,
336-
deps: AgentDepsT = None,
337+
instructions: Instructions[DispatchDepsT] = None,
338+
deps: DispatchDepsT = None,
337339
output_type: OutputSpec[Any] | None = None,
338340
model_settings: ModelSettings | None = None,
339341
usage_limits: UsageLimits | None = None,
340342
usage: RunUsage | None = None,
341343
infer_name: bool = True,
342-
toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None,
344+
toolsets: Sequence[AbstractToolset[DispatchDepsT]] | None = None,
343345
builtin_tools: Sequence[AbstractBuiltinTool] | None = None,
344346
on_complete: OnCompleteFunc[EventT] | None = None,
345347
) -> Response:
@@ -376,7 +378,11 @@ async def dispatch_request(
376378
) from e
377379

378380
try:
379-
adapter = await cls.from_request(request, agent=agent)
381+
# The DepsT comes from `agent`, not from `cls`; the cast is necessary to explain this to pyright
382+
adapter = cast(
383+
UIAdapter[RunInputT, MessageT, EventT, DispatchDepsT, OutputDataT],
384+
await cls.from_request(request, agent=agent),
385+
)
380386
except ValidationError as e: # pragma: no cover
381387
return Response(
382388
content=e.json(),

0 commit comments

Comments
 (0)