Skip to content

Commit 28d2401

Browse files
aidandjWouldYouKindly
authored andcommitted
Start adding tests
Rename folders Update pyproject.toml so pyright works on test code for sync/async only Signed-off-by: Aidan Jensen <aidandj.github@gmail.com>
1 parent ddd9a2f commit 28d2401

File tree

113 files changed

+75
-3714
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+75
-3714
lines changed

pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ exclude = [
3939
executionEnvironments = [
4040
# Due to how upb is typed, we need to disable incompatible variable override checks
4141
{ root = "test/generated", extraPaths = ["./"], reportIncompatibleVariableOverride = "none" },
42-
{ root = "test/generated-concrete", extraPaths = ["./"], reportIncompatibleVariableOverride = "none" },
43-
{ root = "test/generated-sync-only", extraPaths = ["./"], reportIncompatibleVariableOverride = "none" },
44-
{ root = "test/generated-async-only", extraPaths = ["./"], reportIncompatibleVariableOverride = "none" },
42+
{ root = "test/generated_concrete", extraPaths = ["./"], reportIncompatibleVariableOverride = "none" },
43+
{ root = "test/generated_sync_only", extraPaths = ["./"], reportIncompatibleVariableOverride = "none" },
44+
{ root = "test/generated_async_only", extraPaths = ["./"], reportIncompatibleVariableOverride = "none" },
4545
{ root = "mypy_protobuf/extensions_pb2.pyi", reportIncompatibleVariableOverride = "none" },
46+
{ root = "test/async_only", extraPaths = ["test/generated_async_only"] },
47+
{ root = "test/sync_only", extraPaths = ["test/generated_sync_only"] },
4648
]

run_test.sh

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ MYPY_PROTOBUF_VENV=venv_$PY_VER_MYPY_PROTOBUF
132132
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 "$PROTOC" "${PROTOC_ARGS[@]}" --mypy_grpc_out=test/generated
133133

134134
# Generate with concrete service stubs for testing
135-
find proto -name "*.proto" -print0 | xargs -0 "$PROTOC" "${PROTOC_ARGS[@]}" --mypy_out=generate_concrete_servicer_stubs:test/generated-concrete
136-
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 "$PROTOC" "${PROTOC_ARGS[@]}" --mypy_grpc_out=generate_concrete_servicer_stubs:test/generated-concrete
135+
find proto -name "*.proto" -print0 | xargs -0 "$PROTOC" "${PROTOC_ARGS[@]}" --mypy_out=generate_concrete_servicer_stubs:test/generated_concrete
136+
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 "$PROTOC" "${PROTOC_ARGS[@]}" --mypy_grpc_out=generate_concrete_servicer_stubs:test/generated_concrete
137137

138138
# Generate with sync_only stubs for testing
139-
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 "$PROTOC" "${PROTOC_ARGS[@]}" --mypy_grpc_out=only_sync:test/generated-sync-only
139+
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 "$PROTOC" "${PROTOC_ARGS[@]}" --mypy_grpc_out=only_sync:test/generated_sync_only --mypy_out=test/generated_sync_only --python_out=test/generated_sync_only
140140

141141
# Generate with async_only stubs for testing
142-
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 "$PROTOC" "${PROTOC_ARGS[@]}" --mypy_grpc_out=only_async:test/generated-async-only
142+
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 "$PROTOC" "${PROTOC_ARGS[@]}" --mypy_grpc_out=only_async:test/generated_async_only --mypy_out=test/generated_async_only --python_out=test/generated_async_only
143143

144144
if [[ -n $VALIDATE ]] && ! diff <(echo "$SHA_BEFORE") <(find test/generated -name "*.pyi" -print0 | xargs -0 sha1sum); then
145145
echo -e "${RED}Some .pyi files did not match. Please commit those files${NC}"
@@ -157,22 +157,24 @@ for PY_VER in $PY_VER_UNIT_TESTS; do
157157
(
158158
source "$UNIT_TESTS_VENV"/bin/activate
159159
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 python -m grpc_tools.protoc "${PROTOC_ARGS[@]}" --grpc_python_out=test/generated
160+
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 python -m grpc_tools.protoc "${PROTOC_ARGS[@]}" --grpc_python_out=test/generated_sync_only
161+
find proto/testproto/grpc -name "*.proto" -print0 | xargs -0 python -m grpc_tools.protoc "${PROTOC_ARGS[@]}" --grpc_python_out=test/generated_async_only
160162
)
161163

162164
# Run mypy on unit tests / generated output
163165
(
164166
source "$MYPY_VENV"/bin/activate
165167
# Run concrete mypy
166168
CONCRETE_MODULES=( -m test.test_concrete )
167-
MYPYPATH=$MYPYPATH:test/generated-concrete mypy ${CUSTOM_TYPESHED_DIR_ARG:+"$CUSTOM_TYPESHED_DIR_ARG"} --python-executable="$UNIT_TESTS_VENV"/bin/python --python-version="$PY_VER_MYPY_TARGET" "${CONCRETE_MODULES[@]}"
169+
MYPYPATH=$MYPYPATH:test/generated_concrete mypy ${CUSTOM_TYPESHED_DIR_ARG:+"$CUSTOM_TYPESHED_DIR_ARG"} --python-executable="$UNIT_TESTS_VENV"/bin/python --python-version="$PY_VER_MYPY_TARGET" "${CONCRETE_MODULES[@]}"
168170

169171
# Run sync_only mypy
170-
SYNC_ONLY_MODULES=( -m test.test_sync_only )
171-
MYPYPATH=$MYPYPATH:test/generated-sync-only mypy ${CUSTOM_TYPESHED_DIR_ARG:+"$CUSTOM_TYPESHED_DIR_ARG"} --python-executable="$UNIT_TESTS_VENV"/bin/python --python-version="$PY_VER_MYPY_TARGET" "${SYNC_ONLY_MODULES[@]}"
172+
SYNC_ONLY_MODULES=( -m test.sync_only.test_sync_only )
173+
MYPYPATH=$MYPYPATH:test/generated_sync_only mypy ${CUSTOM_TYPESHED_DIR_ARG:+"$CUSTOM_TYPESHED_DIR_ARG"} --python-executable="$UNIT_TESTS_VENV"/bin/python --python-version="$PY_VER_MYPY_TARGET" "${SYNC_ONLY_MODULES[@]}"
172174

173175
# Run async_only mypy
174-
ASYNC_ONLY_MODULES=( -m test.test_async_only )
175-
MYPYPATH=$MYPYPATH:test/generated-async-only mypy ${CUSTOM_TYPESHED_DIR_ARG:+"$CUSTOM_TYPESHED_DIR_ARG"} --python-executable="$UNIT_TESTS_VENV"/bin/python --python-version="$PY_VER_MYPY_TARGET" "${ASYNC_ONLY_MODULES[@]}"
176+
ASYNC_ONLY_MODULES=( -m test.async_only.test_async_only )
177+
MYPYPATH=$MYPYPATH:test/generated_async_only mypy ${CUSTOM_TYPESHED_DIR_ARG:+"$CUSTOM_TYPESHED_DIR_ARG"} --python-executable="$UNIT_TESTS_VENV"/bin/python --python-version="$PY_VER_MYPY_TARGET" "${ASYNC_ONLY_MODULES[@]}"
176178

177179
export MYPYPATH=$MYPYPATH:test/generated
178180

@@ -222,7 +224,7 @@ for PY_VER in $PY_VER_UNIT_TESTS; do
222224
(
223225
# Run unit tests.
224226
source "$UNIT_TESTS_VENV"/bin/activate
225-
PYTHONPATH=test/generated py.test --ignore=test/generated --ignore=test/generated-sync-only --ignore=test/generated-async-only -v
227+
PYTHONPATH=test/generated py.test --ignore=test/generated --ignore=test/generated_sync_only --ignore=test/generated_async_only -v
226228
)
227229
done
228230

test/async_only/test_async_only.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Type-checking test for async_only GRPC stubs.
3+
4+
This module is run through mypy to validate that stubs generated with the
5+
only_async flag have the correct types:
6+
- Regular (non-generic) Stub class that only accepts grpc.aio.Channel
7+
- No AsyncStub type alias (the stub itself is async-only)
8+
- Servicer methods use AsyncIterator for client streaming (not _MaybeAsyncIterator)
9+
- add_XXXServicer_to_server accepts grpc.aio.Server
10+
"""
11+
12+
from typing import Awaitable
13+
import grpc.aio
14+
from testproto.grpc import dummy_pb2_grpc, dummy_pb2
15+
16+
17+
class AsyncOnlyServicer(dummy_pb2_grpc.DummyServiceServicer):
18+
async def UnaryUnary(
19+
self,
20+
request: dummy_pb2.DummyRequest,
21+
context: grpc.aio.ServicerContext[dummy_pb2.DummyRequest, Awaitable[dummy_pb2.DummyReply]],
22+
) -> dummy_pb2.DummyReply:
23+
await context.abort(grpc.StatusCode.UNIMPLEMENTED, "Not implemented")
24+
return dummy_pb2.DummyReply(value=request.value[::-1])
25+
26+
27+
async def main():
28+
stub = dummy_pb2_grpc.DummyServiceStub(channel=grpc.aio.insecure_channel("localhost:50051"))
29+
await stub.UnaryUnary(dummy_pb2.DummyRequest(value="test"))

test/generated-async-only/__init__.py

Whitespace-only changes.

test/generated-async-only/testproto/Capitalized/Capitalized_pb2.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

test/generated-async-only/testproto/Capitalized/__init__.py

Whitespace-only changes.

test/generated-async-only/testproto/__init__.py

Whitespace-only changes.

test/generated-async-only/testproto/comment_special_chars_pb2.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

test/generated-async-only/testproto/dot/__init__.py

Whitespace-only changes.

test/generated-async-only/testproto/dot/com/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)