Fix remaining type issues

This commit is contained in:
Teknique 2023-06-17 12:09:40 -07:00
parent f1aad14099
commit e0efb2853b
No known key found for this signature in database
GPG Key ID: 54D9906A294B7FF3
3 changed files with 22 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import asyncio
import pytest import pytest
import veilid import veilid
from veilid.types import OperationId
from .conftest import server_info from .conftest import server_info
@ -20,7 +21,8 @@ async def test_routing_contexts(api_connection):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_routing_context_app_message_loopback(): async def test_routing_context_app_message_loopback():
app_message_queue = asyncio.Queue() # Seriously, mypy?
app_message_queue: asyncio.Queue = asyncio.Queue()
async def app_message_queue_update_callback(update: veilid.VeilidUpdate): async def app_message_queue_update_callback(update: veilid.VeilidUpdate):
if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE: if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE:
@ -51,13 +53,14 @@ async def test_routing_context_app_message_loopback():
update: veilid.VeilidUpdate = await asyncio.wait_for( update: veilid.VeilidUpdate = await asyncio.wait_for(
app_message_queue.get(), timeout=10 app_message_queue.get(), timeout=10
) )
appmsg: veilid.VeilidAppMessage = update.detail
assert appmsg.message == message assert isinstance(update.detail, veilid.VeilidAppMessage)
assert update.detail.message == message
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_routing_context_app_call_loopback(): async def test_routing_context_app_call_loopback():
app_call_queue = asyncio.Queue() app_call_queue: asyncio.Queue = asyncio.Queue()
async def app_call_queue_update_callback(update: veilid.VeilidUpdate): async def app_call_queue_update_callback(update: veilid.VeilidUpdate):
if update.kind == veilid.VeilidUpdateKind.APP_CALL: if update.kind == veilid.VeilidUpdateKind.APP_CALL:
@ -88,12 +91,19 @@ async def test_routing_context_app_call_loopback():
update: veilid.VeilidUpdate = await asyncio.wait_for( update: veilid.VeilidUpdate = await asyncio.wait_for(
app_call_queue.get(), timeout=10 app_call_queue.get(), timeout=10
) )
appcall: veilid.VeilidAppCall = update.detail appcall = update.detail
assert isinstance(appcall, veilid.VeilidAppCall)
assert appcall.message == request assert appcall.message == request
# now we reply to the request # now we reply to the request
reply = b"qwer5678" reply = b"qwer5678"
await api.app_call_reply(appcall.call_id, reply) # TK: OperationId use to be a subclass of `int`. When I wrapped `appcall.call_id` in int(),
# this failed JSON schema validation, which defines `call_id` as a string. Maybe that was a
# typo, and OperationId is really *supposed* to be a str? Alternatively, perhaps the
# signature of `app_call_reply` is wrong and it's supposed to take a type other than
# OperationId?
await api.app_call_reply(OperationId(appcall.call_id), reply)
# now we should get the reply from the call # now we should get the reply from the call
result = await app_call_task result = await app_call_task

View File

@ -49,8 +49,8 @@ _VALIDATOR_RECV_MESSAGE = _get_schema_validator(
class _JsonVeilidAPI(VeilidAPI): class _JsonVeilidAPI(VeilidAPI):
reader: asyncio.StreamReader reader: Optional[asyncio.StreamReader]
writer: asyncio.StreamWriter writer: Optional[asyncio.StreamWriter]
update_callback: Callable[[VeilidUpdate], Awaitable] update_callback: Callable[[VeilidUpdate], Awaitable]
handle_recv_messages_task: Optional[asyncio.Task] handle_recv_messages_task: Optional[asyncio.Task]
validate_schemas: bool validate_schemas: bool
@ -85,6 +85,7 @@ class _JsonVeilidAPI(VeilidAPI):
await self.lock.acquire() await self.lock.acquire()
try: try:
self.reader = None self.reader = None
assert self.writer is not None
self.writer.close() self.writer.close()
await self.writer.wait_closed() await self.writer.wait_closed()
self.writer = None self.writer = None
@ -138,6 +139,7 @@ class _JsonVeilidAPI(VeilidAPI):
async def handle_recv_messages(self): async def handle_recv_messages(self):
# Read lines until we're done # Read lines until we're done
try: try:
assert self.reader is not None
while True: while True:
linebytes = await self.reader.readline() linebytes = await self.reader.readline()
if not linebytes.endswith(b"\n"): if not linebytes.endswith(b"\n"):
@ -225,6 +227,7 @@ class _JsonVeilidAPI(VeilidAPI):
# Send to socket # Send to socket
try: try:
assert writer is not None
writer.write(reqbytes) writer.write(reqbytes)
await writer.drain() await writer.drain()
except Exception: except Exception:

View File

@ -82,7 +82,7 @@ class ByteCount(int):
pass pass
class OperationId(int): class OperationId(str):
pass pass