Skip to content

Commit 48c643f

Browse files
rustyconoverclaude
andcommitted
Add pyarrow-stubs and improve type coverage
- Add pyarrow-stubs dependency for proper PyArrow type hints - Fix type annotations throughout vgi/ to satisfy mypy with stubs - Add explicit structlog.stdlib.BoundLogger annotations for loggers - Use TYPE_CHECKING imports for forward references (Scalar types) - Add from __future__ import annotations for deferred type evaluation - Fix BufferedReader and PythonFile type annotations - Achieve 95.78% mypy type coverage (up from ~94%) - Add pre-commit check reminder to CLAUDE.md All 410 tests pass with mypy reporting no issues. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent f87bb3d commit 48c643f

29 files changed

Lines changed: 493 additions & 226 deletions

‎.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ wheels/
88

99
# Virtual environments
1010
.venv
11+
12+
# Mypy reports
13+
mypy-reports/
14+
mypy-html-report/

‎CLAUDE.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ uv run ruff format . # Format
1212
uv run mypy vgi/ # Type check
1313
```
1414

15+
**Before committing**, always run format and lint checks:
16+
```bash
17+
uv run ruff format . && uv run ruff check . && uv run mypy vgi/
18+
```
19+
1520
## Project Overview
1621

1722
VGI (Vector Gateway Interface) provides an Apache Arrow-based protocol for connecting DuckDB to external programs. It enables user-defined functions to run in separate processes, communicating via stdin/stdout using Arrow IPC streaming.

‎pyproject.toml‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ requires-python = ">=3.12.4"
77
dependencies = ["click", "pyarrow", "structlog", "platformdirs"]
88

99
[project.optional-dependencies]
10-
dev = ["mypy", "pytest", "pytest-mypy", "pytest-ruff", "pytest-xdist", "ruff"]
10+
dev = ["mypy", "pyarrow-stubs", "pytest", "pytest-mypy", "pytest-ruff", "pytest-xdist", "ruff"]
1111

1212
[project.scripts]
1313
vgi-client = "vgi.client.cli:main"
@@ -38,14 +38,16 @@ strict = true
3838
warn_return_any = true
3939
warn_unused_ignores = true
4040

41-
[[tool.mypy.overrides]]
42-
module = "pyarrow.*"
43-
ignore_missing_imports = true
44-
4541
[[tool.mypy.overrides]]
4642
module = "structlog.*"
4743
ignore_missing_imports = true
4844

4945
[tool.pytest.ini_options]
5046
addopts = "--mypy --ruff"
5147
testpaths = ["tests"]
48+
49+
[dependency-groups]
50+
dev = [
51+
"lxml>=6.0.2",
52+
"pytest-timeout>=2.4.0",
53+
]

‎tests/conftest.py‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Shared fixtures for VGI tests."""
22

3+
from typing import Any
4+
35
import pyarrow as pa
46
import pytest
57

@@ -13,13 +15,12 @@ def example_worker() -> str:
1315
@pytest.fixture
1416
def simple_batches() -> list[pa.RecordBatch]:
1517
"""Create simple test batches with integer and string columns."""
16-
schema = pa.schema(
17-
[
18-
pa.field("id", pa.int64()),
19-
pa.field("value", pa.int64()),
20-
pa.field("name", pa.string()),
21-
]
22-
)
18+
fields: list[pa.Field[Any]] = [
19+
pa.field("id", pa.int64()),
20+
pa.field("value", pa.int64()),
21+
pa.field("name", pa.string()),
22+
]
23+
schema = pa.schema(fields)
2324
batch1 = pa.RecordBatch.from_pydict(
2425
{"id": [1, 2], "value": [10, 20], "name": ["a", "b"]},
2526
schema=schema,
@@ -34,12 +35,11 @@ def simple_batches() -> list[pa.RecordBatch]:
3435
@pytest.fixture
3536
def numeric_batches() -> list[pa.RecordBatch]:
3637
"""Create test batches with only numeric columns for sum tests."""
37-
schema = pa.schema(
38-
[
39-
pa.field("a", pa.int32()),
40-
pa.field("b", pa.float64()),
41-
]
42-
)
38+
fields: list[pa.Field[Any]] = [
39+
pa.field("a", pa.int32()),
40+
pa.field("b", pa.float64()),
41+
]
42+
schema = pa.schema(fields)
4343
batch1 = pa.RecordBatch.from_pydict(
4444
{"a": [1, 2, 3], "b": [1.5, 2.5, 3.0]},
4545
schema=schema,

‎tests/table/generator/test_partitioned_function.py‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Tests for the PartitionedRangeFunction with multi-worker support."""
22

3+
from __future__ import annotations
4+
5+
from typing import Any
6+
37
import pyarrow as pa
48

59
from vgi.client import Client
@@ -10,6 +14,11 @@
1014
from .conftest import RunnerWithMode
1115

1216

17+
def _sorted_non_null(values: list[Any | None]) -> list[Any]:
18+
"""Return sorted list, filtering out None values for type safety."""
19+
return sorted(v for v in values if v is not None)
20+
21+
1322
class TestPartitionedRangeFunctionInProcess:
1423
"""In-process tests for the partitioned_range function."""
1524

@@ -23,7 +32,7 @@ def test_generates_full_range_single_worker(self) -> None:
2332
table = pa.Table.from_batches(outputs)
2433
assert table.num_rows == 10
2534

26-
values = sorted(table.column("value").to_pylist())
35+
values = _sorted_non_null(table.column("value").to_pylist())
2736
assert values == list(range(10))
2837

2938
def test_metadata(self) -> None:
@@ -64,7 +73,7 @@ def test_values_are_sequential(
6473
outputs, logs = runner(PartitionedRangeFunction, (50,))
6574

6675
table = pa.Table.from_batches(outputs)
67-
values = sorted(table.column("value").to_pylist())
76+
values = _sorted_non_null(table.column("value").to_pylist())
6877
assert values == list(range(50))
6978

7079

@@ -84,7 +93,7 @@ def test_two_workers_produce_complete_range(self) -> None:
8493
table = pa.Table.from_batches(outputs)
8594
assert table.num_rows == 20
8695

87-
values = sorted(table.column("value").to_pylist())
96+
values = _sorted_non_null(table.column("value").to_pylist())
8897
assert values == list(range(20))
8998

9099
def test_three_workers_produce_complete_range(self) -> None:
@@ -100,7 +109,7 @@ def test_three_workers_produce_complete_range(self) -> None:
100109
table = pa.Table.from_batches(outputs)
101110
assert table.num_rows == 30
102111

103-
values = sorted(table.column("value").to_pylist())
112+
values = _sorted_non_null(table.column("value").to_pylist())
104113
assert values == list(range(30))
105114

106115
def test_workers_produce_large_range(self) -> None:
@@ -116,7 +125,7 @@ def test_workers_produce_large_range(self) -> None:
116125
table = pa.Table.from_batches(outputs)
117126
assert table.num_rows == 10000
118127

119-
values = sorted(table.column("value").to_pylist())
128+
values = _sorted_non_null(table.column("value").to_pylist())
120129
assert values == list(range(10000))
121130

122131
def test_uneven_distribution(self) -> None:
@@ -134,7 +143,7 @@ def test_uneven_distribution(self) -> None:
134143
table = pa.Table.from_batches(outputs)
135144
assert table.num_rows == 7
136145

137-
values = sorted(table.column("value").to_pylist())
146+
values = _sorted_non_null(table.column("value").to_pylist())
138147
assert values == list(range(7))
139148

140149
def test_single_worker_fallback(self) -> None:
@@ -150,5 +159,5 @@ def test_single_worker_fallback(self) -> None:
150159
table = pa.Table.from_batches(outputs)
151160
assert table.num_rows == 15
152161

153-
values = sorted(table.column("value").to_pylist())
162+
values = _sorted_non_null(table.column("value").to_pylist())
154163
assert values == list(range(15))

‎tests/table/generator/test_random_sample_function.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ def test_values_in_range(self, run_table_function_mode: RunnerWithMode) -> None:
9595
table = pa.Table.from_batches(outputs)
9696
values = table.column("value").to_pylist()
9797

98-
assert all(0 <= v < 1 for v in values)
98+
assert all(v is not None and 0 <= v < 1 for v in values)

‎tests/table/test_function.py‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Generic tests for TableFunctionGenerator behavior."""
22

3+
from __future__ import annotations
4+
5+
from typing import Any
6+
37
import pyarrow as pa
48
import pytest
59
import structlog
@@ -14,6 +18,11 @@
1418
from vgi.testing import FunctionTestClientError, TableFunctionTestClient
1519

1620

21+
def _schema(fields: list[Any]) -> pa.Schema:
22+
"""Create schema with proper typing for field list."""
23+
return pa.schema(fields)
24+
25+
1726
class TestTableFunctionGeneratorProtocol:
1827
"""Tests for the TableFunctionGenerator protocol."""
1928

@@ -23,7 +32,7 @@ def test_empty_process_generator(self) -> None:
2332
class EmptyFunction(TableFunctionGenerator):
2433
@property
2534
def output_schema(self) -> pa.Schema:
26-
return pa.schema([pa.field("x", pa.int64())])
35+
return _schema([pa.field("x", pa.int64())])
2736

2837
with TableFunctionTestClient(EmptyFunction) as client:
2938
outputs = list(client.table_function())
@@ -37,7 +46,7 @@ def test_single_batch_output(self) -> None:
3746
class SingleBatchFunction(TableFunctionGenerator):
3847
@property
3948
def output_schema(self) -> pa.Schema:
40-
return pa.schema([pa.field("x", pa.int64())])
49+
return _schema([pa.field("x", pa.int64())])
4150

4251
def process(self) -> OutputGenerator:
4352
yield Output(
@@ -58,7 +67,7 @@ def test_multiple_batch_output(self) -> None:
5867
class MultiBatchFunction(TableFunctionGenerator):
5968
@property
6069
def output_schema(self) -> pa.Schema:
61-
return pa.schema([pa.field("n", pa.int64())])
70+
return _schema([pa.field("n", pa.int64())])
6271

6372
def process(self) -> OutputGenerator:
6473
for i in range(3):
@@ -86,7 +95,7 @@ def test_setup_called_before_process(self) -> None:
8695
class LifecycleFunction(TableFunctionGenerator):
8796
@property
8897
def output_schema(self) -> pa.Schema:
89-
return pa.schema([pa.field("x", pa.int64())])
98+
return _schema([pa.field("x", pa.int64())])
9099

91100
def setup(self) -> None:
92101
call_order.append("setup")
@@ -112,7 +121,7 @@ def test_teardown_called_on_exception(self) -> None:
112121
class ExceptionFunction(TableFunctionGenerator):
113122
@property
114123
def output_schema(self) -> pa.Schema:
115-
return pa.schema([pa.field("x", pa.int64())])
124+
return _schema([pa.field("x", pa.int64())])
116125

117126
def process(self) -> OutputGenerator:
118127
raise ValueError("test error")
@@ -140,7 +149,7 @@ def test_valid_schema_passes(self) -> None:
140149
class ValidSchemaFunction(TableFunctionGenerator):
141150
@property
142151
def output_schema(self) -> pa.Schema:
143-
return pa.schema([pa.field("x", pa.int64())])
152+
return _schema([pa.field("x", pa.int64())])
144153

145154
def process(self) -> OutputGenerator:
146155
yield Output(
@@ -158,11 +167,11 @@ def test_invalid_schema_raises(self) -> None:
158167
class InvalidSchemaFunction(TableFunctionGenerator):
159168
@property
160169
def output_schema(self) -> pa.Schema:
161-
return pa.schema([pa.field("x", pa.int64())])
170+
return _schema([pa.field("x", pa.int64())])
162171

163172
def process(self) -> OutputGenerator:
164173
# Return batch with wrong column name
165-
wrong_schema = pa.schema([pa.field("y", pa.int64())])
174+
wrong_schema = _schema([pa.field("y", pa.int64())])
166175
wrong_batch = pa.RecordBatch.from_pydict(
167176
{"y": [1]}, schema=wrong_schema
168177
)
@@ -187,7 +196,7 @@ def test_default_cardinality_is_none(self) -> None:
187196
class NoCardinalityFunction(TableFunctionGenerator):
188197
@property
189198
def output_schema(self) -> pa.Schema:
190-
return pa.schema([pa.field("x", pa.int64())])
199+
return _schema([pa.field("x", pa.int64())])
191200

192201
invocation = Invocation(
193202
function_name="test",
@@ -209,7 +218,7 @@ def test_custom_cardinality(self) -> None:
209218
class CardinalityFunction(TableFunctionGenerator):
210219
@property
211220
def output_schema(self) -> pa.Schema:
212-
return pa.schema([pa.field("x", pa.int64())])
221+
return _schema([pa.field("x", pa.int64())])
213222

214223
def cardinality(self) -> CardinalityInfo:
215224
return CardinalityInfo(estimate=100, max=1000)
@@ -244,7 +253,7 @@ class ArgFunction(TableFunctionGenerator):
244253

245254
@property
246255
def output_schema(self) -> pa.Schema:
247-
return pa.schema([pa.field("n", pa.int64())])
256+
return _schema([pa.field("n", pa.int64())])
248257

249258
def process(self) -> OutputGenerator:
250259
yield Output(
@@ -270,7 +279,7 @@ class NamedArgFunction(TableFunctionGenerator):
270279

271280
@property
272281
def output_schema(self) -> pa.Schema:
273-
return pa.schema([pa.field("result", pa.int64())])
282+
return _schema([pa.field("result", pa.int64())])
274283

275284
def process(self) -> OutputGenerator:
276285
yield Output(
@@ -299,7 +308,7 @@ def test_empty_output_batch_property(self) -> None:
299308
class TestFunction(TableFunctionGenerator):
300309
@property
301310
def output_schema(self) -> pa.Schema:
302-
return pa.schema(
311+
return _schema(
303312
[
304313
pa.field("a", pa.int64()),
305314
pa.field("b", pa.string()),

‎tests/table_in_out/generator/test_repeat_inputs_function.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
"""Tests for the RepeatInputsFunction (explosion)."""
22

3+
from __future__ import annotations
4+
5+
from typing import Any
6+
37
import pyarrow as pa
48

59
from vgi.client import Client
610
from vgi.function import Arguments
711

812

13+
def _schema(fields: list[Any]) -> pa.Schema:
14+
"""Create schema with proper typing for field list."""
15+
return pa.schema(fields)
16+
17+
918
class TestRepeatInputsFunction:
1019
"""Tests for the repeat_inputs function (explosion)."""
1120

@@ -18,7 +27,7 @@ def test_repeat_custom_count(
1827
output_batches = list(
1928
client.table_in_out_function(
2029
function_name="repeat_inputs",
21-
arguments=Arguments(positional=tuple([repeat_count]), named={}),
30+
arguments=Arguments(positional=(pa.scalar(repeat_count),)),
2231
input=iter(simple_batches),
2332
)
2433
)
@@ -35,7 +44,7 @@ def test_repeat_single_time(
3544
output_batches = list(
3645
client.table_in_out_function(
3746
function_name="repeat_inputs",
38-
arguments=Arguments(positional=tuple([1]), named={}),
47+
arguments=Arguments(positional=(pa.scalar(1),), named={}),
3948
input=iter(simple_batches),
4049
)
4150
)
@@ -46,7 +55,7 @@ def test_repeat_single_time(
4655

4756
def test_repeat_distributed_many_batches(self, example_worker: str) -> None:
4857
"""Should correctly repeat across many batches with multiple workers."""
49-
schema = pa.schema([pa.field("a", pa.int64()), pa.field("b", pa.float64())])
58+
schema = _schema([pa.field("a", pa.int64()), pa.field("b", pa.float64())])
5059

5160
# Create 100 batches, each with 50 rows
5261
num_batches = 100
@@ -69,7 +78,7 @@ def test_repeat_distributed_many_batches(self, example_worker: str) -> None:
6978
output_batches = list(
7079
client.table_in_out_function(
7180
function_name="repeat_inputs",
72-
arguments=Arguments(positional=tuple([repeat_count]), named={}),
81+
arguments=Arguments(positional=(pa.scalar(repeat_count),)),
7382
input=iter(batches),
7483
)
7584
)
@@ -80,7 +89,7 @@ def test_repeat_distributed_many_batches(self, example_worker: str) -> None:
8089

8190
def test_repeat_distributed_preserves_data(self, example_worker: str) -> None:
8291
"""Should preserve data correctly when repeated across workers."""
83-
schema = pa.schema([pa.field("id", pa.int64()), pa.field("value", pa.string())])
92+
schema = _schema([pa.field("id", pa.int64()), pa.field("value", pa.string())])
8493

8594
# Create batches with distinct values to verify data integrity
8695
batches = [
@@ -101,7 +110,7 @@ def test_repeat_distributed_preserves_data(self, example_worker: str) -> None:
101110
output_batches = list(
102111
client.table_in_out_function(
103112
function_name="repeat_inputs",
104-
arguments=Arguments(positional=tuple([repeat_count]), named={}),
113+
arguments=Arguments(positional=(pa.scalar(repeat_count),)),
105114
input=iter(batches),
106115
)
107116
)

0 commit comments

Comments
 (0)