Skip to content

Commit a9e16e0

Browse files
committed
refactor(v2): remove redundant type casts
1 parent c69de6e commit a9e16e0

3 files changed

Lines changed: 27 additions & 27 deletions

File tree

src/acp/experimental/negotiation.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import asyncio
44
from collections.abc import Callable
5-
from typing import Any, cast
5+
from typing import Any
66

77
from pydantic import BaseModel
88

@@ -97,15 +97,28 @@ async def _initialize(self, method: str, params: Any, is_notification: bool) ->
9797
if is_notification or method != V2_AGENT_METHODS["initialize"]:
9898
raise RequestError.invalid_request({"details": "The first ACP request must be initialize"})
9999
requested = _read_protocol_version(params)
100-
selected = self._select(requested)
101100
connection = self._connection
102101
if connection is None:
103102
raise RuntimeError("Protocol router is not connected")
104103

105-
if selected == v2.PROTOCOL_VERSION:
106-
endpoint, handler = V2AgentSideConnection._attach(cast(Any, self._v2_agent), connection)
104+
if self._v2_agent is not None and requested >= v2.PROTOCOL_VERSION:
105+
selected = v2.PROTOCOL_VERSION
106+
endpoint, handler = V2AgentSideConnection._attach(self._v2_agent, connection)
107+
elif self._v1_agent is not None and requested >= v1_meta.PROTOCOL_VERSION:
108+
selected = v1_meta.PROTOCOL_VERSION
109+
endpoint, handler = V1AgentSideConnection._attach(self._v1_agent, connection)
107110
else:
108-
endpoint, handler = V1AgentSideConnection._attach(cast(Any, self._v1_agent), connection)
111+
supported = [
112+
version
113+
for version, implementation in (
114+
(v1_meta.PROTOCOL_VERSION, self._v1_agent),
115+
(v2.PROTOCOL_VERSION, self._v2_agent),
116+
)
117+
if implementation is not None
118+
]
119+
raise RequestError.invalid_request({
120+
"details": f"Unsupported ACP protocol {requested}; configured versions are {supported}"
121+
})
109122
self._endpoint = endpoint
110123
self._selected = handler
111124
normalized = _normalize_initialize(params, selected)
@@ -117,23 +130,6 @@ async def _initialize(self, method: str, params: Any, is_notification: bool) ->
117130
})
118131
return response
119132

120-
def _select(self, requested: int) -> int:
121-
if self._v2_agent is not None and requested >= v2.PROTOCOL_VERSION:
122-
return v2.PROTOCOL_VERSION
123-
if self._v1_agent is not None and requested >= v1_meta.PROTOCOL_VERSION:
124-
return v1_meta.PROTOCOL_VERSION
125-
supported = [
126-
version
127-
for version, implementation in (
128-
(v1_meta.PROTOCOL_VERSION, self._v1_agent),
129-
(v2.PROTOCOL_VERSION, self._v2_agent),
130-
)
131-
if implementation is not None
132-
]
133-
raise RequestError.invalid_request({
134-
"details": f"Unsupported ACP protocol {requested}; configured versions are {supported}"
135-
})
136-
137133

138134
class AgentProtocolConnection:
139135
def __init__(self, connection: Connection) -> None:

src/acp/experimental/v2/agent.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import asyncio
44
from collections.abc import Callable
5-
from typing import Any, cast
5+
from typing import Any
66

77
from pydantic import BaseModel
88

@@ -37,10 +37,10 @@ def __init__(self, agent: Agent, state: InitializationState) -> None:
3737
async def __call__(self, method: str, params: Any | None, is_notification: bool) -> Any:
3838
initialize = self._router.request_spec("initialize")
3939
if not is_notification and initialize is not None and method == initialize.method:
40-
request = cast(schema.InitializeRequest, initialize.request.validate_python(params))
40+
request: schema.InitializeRequest = initialize.request.validate_python(params)
4141
self._state.begin(request)
4242
try:
43-
response = cast(schema.InitializeResponse, await self._router.handle_request(initialize, params))
43+
response: schema.InitializeResponse = await self._router.handle_request(initialize, params)
4444
self._state.complete(response)
4545
except BaseException as error:
4646
self._state.fail(error)

tests/test_protocol_negotiation.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4-
from typing import Any, cast
4+
from typing import Any
55

66
import pytest
77

@@ -16,6 +16,10 @@ class Client:
1616
pass
1717

1818

19+
class V1Client(acp.Client):
20+
pass
21+
22+
1923
class V1Agent:
2024
def __init__(self) -> None:
2125
self.initialize_calls = 0
@@ -103,7 +107,7 @@ async def test_agent_protocol_router_selects_v1() -> None:
103107
v2_agent = V2Agent()
104108
wire: list[StreamEvent] = []
105109
agent_connection = AgentProtocolRouter(v1=lambda _: v1_agent, v2=lambda _: v2_agent).connect(agent_transport)
106-
client_connection = acp.connect_to_agent(cast(acp.Client, Client()), client_transport, observers=[wire.append])
110+
client_connection = acp.connect_to_agent(V1Client(), client_transport, observers=[wire.append])
107111

108112
try:
109113
initialized = await client_connection.initialize(

0 commit comments

Comments
 (0)