Skip to content

Commit 5c96f74

Browse files
Improve typing (#126)
Why === Adding `pyright` and `make lint`, addressing type errors that cropped up. What changed ============ - `GrpcContext` was missing a `Generic[RequestType, ResponseType]` Test plan ========= CI
1 parent 502278f commit 5c96f74

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
lint:
2+
uv run black --check .
3+
uv run ruff check .
4+
uv run mypy .
5+
uv run pyright-python .
6+
7+
format:
8+
uv run black .
9+
uv run ruff check . --fix

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dev-dependencies = [
4747
"ruff>=0.0.278",
4848
"types-protobuf>=4.24.0.20240311",
4949
"types-nanoid>=2.0.0.20240601",
50+
"pyright>=1.1.389",
5051
]
5152

5253
[tool.ruff]

replit_river/rpc.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def set(self, carrier: TransportMessage, key: str, value: str) -> None:
127127
logger.warning("unknown trace propagation key", extra={"key": key})
128128

129129

130-
class GrpcContext(grpc.aio.ServicerContext):
130+
class GrpcContext(grpc.aio.ServicerContext, Generic[RequestType, ResponseType]):
131131
"""Represents a gRPC-compatible ServicerContext for River interop."""
132132

133133
def __init__(self, peer: str) -> None:
@@ -229,9 +229,8 @@ async def wrapped(
229229
input: Channel[Any],
230230
output: Channel[Any],
231231
) -> None:
232-
context = None
232+
context: GrpcContext[RequestType, ResponseType] = GrpcContext(peer)
233233
try:
234-
context = GrpcContext(peer)
235234
request = request_deserializer(await input.get())
236235
response = method(request, context)
237236
if isinstance(response, Awaitable):
@@ -287,9 +286,8 @@ async def wrapped(
287286
input: Channel[Any],
288287
output: Channel[Any],
289288
) -> None:
290-
context = None
289+
context: GrpcContext[RequestType, ResponseType] = GrpcContext(peer)
291290
try:
292-
context = GrpcContext(peer)
293291
request = request_deserializer(await input.get())
294292
iterator = method(request, context)
295293
if isinstance(iterator, AsyncIterable):
@@ -349,7 +347,7 @@ async def wrapped(
349347
) -> None:
350348
task_manager = BackgroundTaskManager()
351349
try:
352-
context = GrpcContext(peer)
350+
context: GrpcContext[RequestType, ResponseType] = GrpcContext(peer)
353351
request: Channel[RequestType] = Channel(MAX_MESSAGE_BUFFER_SIZE)
354352

355353
async def _convert_inputs() -> None:
@@ -426,9 +424,8 @@ async def wrapped(
426424
output: Channel[Any],
427425
) -> None:
428426
task_manager = BackgroundTaskManager()
429-
context = None
427+
context: GrpcContext[RequestType, ResponseType] = GrpcContext(peer)
430428
try:
431-
context = GrpcContext(peer)
432429
request: Channel[RequestType] = Channel(MAX_MESSAGE_BUFFER_SIZE)
433430

434431
async def _convert_inputs() -> None:

tests/test_message_buffer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ def mock_transport_message(seq: int) -> TransportMessage:
1111
seq=seq,
1212
id="test",
1313
ack=0,
14-
from_="test",
14+
from_="test", # type: ignore
1515
to="test",
1616
streamId="test",
1717
controlFlags=0,
1818
payload=0,
19-
model_config={},
19+
model_config={}, # type: ignore
2020
)
2121

2222

uv.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)