Skip to content

Commit 33f5588

Browse files
committed
Document and test init ProcessState union typing
1 parent 1f4012f commit 33f5588

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎tests/test_protocol_classes.py‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@
55

66
from __future__ import annotations
77

8-
from typing import TypeVar
8+
from typing import TypeVar, get_args, get_origin, get_type_hints
99

1010
import pyarrow as pa
1111
import pytest
1212
from vgi_rpc.log import Level, Message
13+
from vgi_rpc.rpc import Stream
1314
from vgi_rpc.utils import deserialize_record_batch
1415

1516
from tests.conftest import make_schema
1617
from vgi.arguments import Arg, Arguments, ArgumentValidationError
17-
from vgi.table_function import (
18-
TableCardinality,
18+
from vgi.protocol import (
19+
ProcessState,
20+
ScalarExchangeState,
21+
TableInOutExchangeState,
22+
TableInOutFinalizeState,
23+
TableProducerState,
24+
VgiProtocol,
1925
)
26+
from vgi.table_function import TableCardinality
2027

2128
T = TypeVar("T")
2229

@@ -259,6 +266,25 @@ def test_exact_cardinality(self) -> None:
259266
exact = TableCardinality(estimate=1, max=1)
260267
assert exact.estimate == exact.max == 1
261268

269+
270+
class TestProtocolStateTyping:
271+
"""Tests for VGI protocol stream state typing."""
272+
273+
def test_init_declares_process_state_union(self) -> None:
274+
"""VgiProtocol.init() returns Stream with the ProcessState union."""
275+
hints = get_type_hints(VgiProtocol.init)
276+
return_hint = hints["return"]
277+
state_arg = get_args(return_hint)[0]
278+
assert state_arg == ProcessState
279+
assert set(get_args(state_arg)) == {
280+
ScalarExchangeState,
281+
TableProducerState,
282+
TableInOutExchangeState,
283+
TableInOutFinalizeState,
284+
}
285+
assert get_args(return_hint)[1].__name__ == "GlobalInitResponse"
286+
assert get_origin(return_hint) is Stream
287+
262288
def test_frozen(self) -> None:
263289
"""TableCardinality should be immutable (frozen dataclass)."""
264290
info = TableCardinality(estimate=100, max=1000)

‎vgi/protocol.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,9 @@ def produce(self, out: OutputCollector, ctx: CallContext) -> None:
557557
self._index += 1
558558

559559

560-
# Type alias for the union of all stream state types
560+
# Type alias for the union of all stream state variants produced by init().
561+
# vgi-rpc resolves this union using a method-local numeric tag in HTTP state
562+
# tokens, so state recovery does not depend on Python class names.
561563
ProcessState = ScalarExchangeState | TableProducerState | TableInOutExchangeState | TableInOutFinalizeState
562564

563565

0 commit comments

Comments
 (0)