test/stubgen: add for static checks

This commit is contained in:
2026-07-09 18:03:03 +08:00
parent 556969a054
commit 20dff6d6a7
5 changed files with 74 additions and 3 deletions
+1
View File
@@ -32,6 +32,7 @@ distribution = true
[tool.pytest.ini_options]
testpaths = ["tests"]
markers = ["slow: slow tests (dummy.capnp, generics, interface)"]
[tool.setuptools.packages.find]
where = ["src"]
+1
View File
@@ -0,0 +1 @@
*_capnp.pyi
+70
View File
@@ -0,0 +1,70 @@
"""Verify generated .pyi stubs are consistent with pycapnp runtime.
Test file lives alongside the ``.capnp`` schemas + generated ``.pyi``
stubs. pycapnp's import hook handles runtime loading.
basedpyright type-checks this file together with the project.
"""
from __future__ import annotations
import sys
from pathlib import Path
# Ensure this directory is importable
_HERE = Path(__file__).parent
if str(_HERE) not in sys.path:
sys.path.insert(0, str(_HERE))
import capnp # pyright: ignore[reportMissingTypeStubs, reportUnusedImport] # activates import hook
import test_simple_capnp
class TestSimpleStub:
"""test_simple.capnp — basic struct."""
def test_new_message(self) -> None:
p = test_simple_capnp.Person.new_message(
name="Alice", age=30, email="alice@example.com"
)
assert p.name == "Alice"
assert p.age == 30
def test_field_assignment(self) -> None:
p = test_simple_capnp.Person.new_message()
p.name = "Bob"
p.age = 25
assert p.name == "Bob"
def test_to_dict(self) -> None:
p = test_simple_capnp.Person.new_message(name="Carol", age=28)
d = p.to_dict()
assert d["name"] == "Carol"
def test_from_dict(self) -> None:
p = test_simple_capnp.Person.new_message()
_ = p.from_dict({"name": "Dave", "age": 35})
assert p.name == "Dave"
def test_serialize_roundtrip(self) -> None:
p = test_simple_capnp.Person.new_message(name="Eve", age=22)
data = p.to_bytes()
p2 = test_simple_capnp.Person.from_bytes(data)
assert p2 is not None
class TestNestedStub:
"""test_nested.capnp — nested types + lists."""
def test_nested_message(self) -> None:
import test_nested_capnp
ab = test_nested_capnp.AddressBook.new_message()
assert ab.people is not None
class TestGenericsStub:
"""test_generics.capnp — generic structs."""
def test_holder_loads(self) -> None:
import test_generics_capnp
c = test_generics_capnp.Container.new_message()
assert c is not None
+2 -2
View File
@@ -85,11 +85,11 @@ class TestTypeRegistry:
assert reg.get_or_raise(1) is info
import pytest
with pytest.raises(KeyError):
reg.get_or_raise(999)
_ = reg.get_or_raise(999)
def test_contains(self):
reg = TypeRegistry()
info = TypeInfo(1, "Foo", "Foo", {}, None, "struct")
info: TypeInfo = TypeInfo(1, "Foo", "Foo", {}, None, "struct")
reg.register(info)
assert 1 in reg
assert 2 not in reg
-1
View File
@@ -1,6 +1,5 @@
"""Tests for capnp_stubgen.utils."""
import pytest
from capnp_stubgen.utils import (
filename_to_module_name,
get_display_name,