Skip to content

Commit a9c151c

Browse files
committed
Fix codec type
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
1 parent b192212 commit a9c151c

10 files changed

Lines changed: 19 additions & 28 deletions

File tree

.gitignore

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,7 @@ venv.bak/
110110
*.so
111111
*.dylib
112112

113-
# Test binary, build with `go test -c`
114-
*.test
115-
116-
# Output of the go coverage tool, specifically when used with LiteIDE
117-
*.out
118-
119113
.vscode
120114

121-
out/
122-
123115
# GitHub Actions temporary files
124116
.github/.tmp/

conformance/test/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ def send_unary_request_sync(
475475
if test_request.cancel:
476476
match test_request.cancel.cancel_timing:
477477
case Oneof("after_close_send_ms", after_close_send_ms):
478+
await request_closed.wait()
478479
await asyncio.sleep(after_close_send_ms / 1000.0)
479480
task.cancel()
480481
await task

src/connectrpc/_client_async.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,7 @@ async def _send_request_unary(
333333
f"message is larger than configured max {self._read_max_bytes}",
334334
)
335335

336-
response = ctx.method.output()
337-
return self._codec.decode(resp.content, response)
336+
return self._codec.decode(resp.content, ctx.method.output)
338337
raise ConnectWireError.from_response(resp).to_exception()
339338
except (TimeoutError, asyncio.TimeoutError) as e:
340339
raise ConnectError(Code.DEADLINE_EXCEEDED, "Request timed out") from e

src/connectrpc/_client_sync.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,7 @@ def _send_request_unary(self, request: REQ, ctx: RequestContext[REQ, RES]) -> RE
330330
f"message is larger than configured max {self._read_max_bytes}",
331331
)
332332

333-
response = ctx.method.output()
334-
return self._codec.decode(resp.content, response)
333+
return self._codec.decode(resp.content, ctx.method.output)
335334
raise ConnectWireError.from_response(resp).to_exception()
336335
except TimeoutError as e:
337336
raise ConnectError(Code.DEADLINE_EXCEEDED, "Request timed out") from e

src/connectrpc/_codec.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def encode(self, message: T_contra) -> bytes:
5757
"""Marshals the given message."""
5858
...
5959

60-
def decode(self, data: bytes | bytearray, message: U) -> U:
60+
def decode(self, data: bytes | bytearray, message_class: type[U]) -> U:
6161
"""Unmarshals the given message."""
6262
...
6363

@@ -71,8 +71,8 @@ def name(self) -> str:
7171
def encode(self, message: Message) -> bytes:
7272
return message.to_binary()
7373

74-
def decode(self, data: bytes | bytearray, message: V) -> V:
75-
return message.__class__.from_binary(data) # TODO: fix type
74+
def decode(self, data: bytes | bytearray, message_class: type[V]) -> V:
75+
return message_class.from_binary(data)
7676

7777

7878
class ProtoJSONCodec(Codec[Message, V]):
@@ -88,8 +88,8 @@ def name(self) -> str:
8888
def encode(self, message: Message) -> bytes:
8989
return message.to_json(registry=self._registry).encode()
9090

91-
def decode(self, data: bytes | bytearray, message: V) -> V:
92-
return message.__class__.from_json(data, registry=self._registry)
91+
def decode(self, data: bytes | bytearray, message_class: type[V]) -> V:
92+
return message_class.from_json(data, registry=self._registry)
9393

9494

9595
_proto_binary_codec = ProtoBinaryCodec()

src/connectrpc/_envelope.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def _read_messages(self) -> Iterator[_RES]:
7575
if self.handle_end_message(prefix_byte, message_data):
7676
return
7777

78-
res = self._message_class()
79-
res = self._codec.decode(message_data, res)
78+
res = self._codec.decode(message_data, self._message_class)
8079
yield res
8180

8281
if len(self._buffer) < 5:

src/connectrpc/_server_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ async def _read_get_request(
330330
message = compression.decompress(message)
331331

332332
# Get the appropriate decoder for the endpoint
333-
return codec.decode(message, endpoint.method.input())
333+
return codec.decode(message, endpoint.method.input)
334334

335335
async def _read_post_request(
336336
self,
@@ -363,7 +363,7 @@ async def _read_post_request(
363363
f"message is larger than configured max {self._read_max_bytes}",
364364
)
365365

366-
return codec.decode(req_body, endpoint.method.input())
366+
return codec.decode(req_body, endpoint.method.input)
367367

368368
async def _handle_stream(
369369
self,

src/connectrpc/_server_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def _handle_post_request(
336336
)
337337

338338
try:
339-
return codec.decode(req_body, endpoint.method.input()), codec
339+
return codec.decode(req_body, endpoint.method.input), codec
340340
except Exception as e:
341341
raise ConnectError(
342342
Code.INVALID_ARGUMENT, f"Failed to decode request body: {e!s}"
@@ -396,7 +396,7 @@ def _handle_get_request(
396396
# Handle GET request with proto decoder
397397
try:
398398
# TODO - Use content type from queryparam
399-
request = codec.decode(message, endpoint.method.input())
399+
request = codec.decode(message, endpoint.method.input)
400400
return request, codec
401401
except Exception as e:
402402
raise ConnectError(

src/connectrpc/compat/_codec.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ def name(self) -> str:
2626
def encode(self, message: Message) -> bytes:
2727
return message.SerializeToString()
2828

29-
def decode(self, data: bytes | bytearray, message: V) -> V:
30-
message.ParseFromString(data) # ty:ignore[invalid-argument-type] type is incorrect
31-
return message
29+
def decode(self, data: bytes | bytearray, message_class: type[V]) -> V:
30+
return message_class.FromString(data) # ty:ignore[invalid-argument-type] type is incorrect
3231

3332

3433
class ProtoJSONCodec(Codec[Message, V]):
@@ -43,7 +42,8 @@ def name(self) -> str:
4342
def encode(self, message: Message) -> bytes:
4443
return MessageToJson(message).encode()
4544

46-
def decode(self, data: bytes | bytearray, message: V) -> V:
45+
def decode(self, data: bytes | bytearray, message_class: type[V]) -> V:
46+
message = message_class()
4747
MessageFromJson(data, message) # ty:ignore[invalid-argument-type] type is incorrect
4848
return message
4949

test/test_codec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ def encode(self, message: Message) -> bytes:
4040
case _:
4141
raise ValueError(f"unexpected message type: {type(message)}")
4242

43-
def decode(self, data: bytes | bytearray, message: Message) -> Message:
43+
def decode(self, data: bytes | bytearray, message_class: type[Message]) -> Message:
4444
s = data.decode()
45+
message = message_class()
4546
match message:
4647
case Size():
4748
message.inches = int(s)

0 commit comments

Comments
 (0)