[Python][Typeguard] Part 2 - Add Typeguard to AIO stack in tests #40215
[Python][Typeguard] Part 2 - Add Typeguard to AIO stack in tests #40215asheshvidyut wants to merge 15 commits intogrpc:masterfrom
Conversation
| raise NotImplementedError() | ||
|
|
||
|
|
||
| class StreamStreamCallResponseIterator( |
There was a problem hiding this comment.
This class and 3 classes above are cut - paste from the file below, because the class was needed for type hints.
There was a problem hiding this comment.
I'm OK with this, just be aware that your name will be on git blame for this lines. Internal Google java style guide calls it "You change it, you own it".
This may be solvable with from __future__ import annotations. Apparently type deferred evaluation will be the default behavior in Python 3.14 anyway: https://docs.python.org/3.14/whatsnew/3.14.html#pep-649-and-749-deferred-evaluation-of-annotations
There was a problem hiding this comment.
With typeguard version bump - this works. Thanks.
| raise NotImplementedError() | ||
|
|
||
|
|
||
| class StreamStreamCallResponseIterator( |
There was a problem hiding this comment.
I'm OK with this, just be aware that your name will be on git blame for this lines. Internal Google java style guide calls it "You change it, you own it".
This may be solvable with from __future__ import annotations. Apparently type deferred evaluation will be the default behavior in Python 3.14 anyway: https://docs.python.org/3.14/whatsnew/3.14.html#pep-649-and-749-deferred-evaluation-of-annotations
| client_call_details: ClientCallDetails, | ||
| request: RequestType, | ||
| ) -> Union[UnaryUnaryCall, ResponseType]: | ||
| ) -> UnaryUnaryCall: |
There was a problem hiding this comment.
We should globally disable this check in typeguard. If our tests don't have coverage for returning ResponseType that we claim is a valid return type, it's on us to add the missing coverage.
There was a problem hiding this comment.
This will be reverted.
| self, request: RequestType, call: InterceptedCall | ||
| self, | ||
| request: RequestType, | ||
| call: Union[InterceptedCall, _base_call.Call], |
There was a problem hiding this comment.
This is not a public function, but looks like we're changing the allowed inputs to both an InterceptedCall and a general Call object. Is this an intended behaviour?
There was a problem hiding this comment.
On reverting this change, typeguard fails with error
File "/usr/local/google/home/asheshvidyut/.cache/bazel/_bazel_asheshvidyut/37ab11cdf3f320a86e32407534188cd9/sandbox/linux-sandbox/2564/execroot/com_github_grpc_grpc/bazel-out/k8-fastbuild/bin/src/python/grpcio_tests/tests_aio/unit/client_stream_stream_interceptor_test.runfiles/grpc_python_dependencies_typeguard/site-packages/typeguard/_functions.py", line 135, in check_argument_types
check_type_internal(value, annotation, memo)
File "/usr/local/google/home/asheshvidyut/.cache/bazel/_bazel_asheshvidyut/37ab11cdf3f320a86e32407534188cd9/sandbox/linux-sandbox/2564/execroot/com_github_grpc_grpc/bazel-out/k8-fastbuild/bin/src/python/grpcio_tests/tests_aio/unit/client_stream_stream_interceptor_test.runfiles/grpc_python_dependencies_typeguard/site-packages/typeguard/_checkers.py", line 779, in check_type_internal
raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}")
typeguard.TypeCheckError: argument "call" (grpc.aio._interceptor.StreamStreamCallResponseIterator) is not an instance of grpc.aio._interceptor.InterceptedCall
There was a problem hiding this comment.
I see the log lines like these
typeguard.TypeCheckError: argument "call" (grpc.aio._call.StreamUnaryCall) is not an instance of grpc.aio._interceptor.InterceptedCall
typeguard.TypeCheckError: argument "call" (grpc.aio._interceptor.StreamStreamCallResponseIterator) is not an instance of grpc.aio._interceptor.InterceptedCal
So keeping it only _base_calll.Call
f4abeef to
eb3f14c
Compare
935d65b to
e0af9a8
Compare
| ) -> UnaryStreamCall: | ||
| request_serializer: Optional[SerializingFunction], | ||
| response_deserializer: Optional[DeserializingFunction], | ||
| ) -> Union[UnaryStreamCall, UnaryStreamCallResponseIterator]: |
There was a problem hiding this comment.
Typeguard stack trace after reverting this.
raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}")
typeguard.TypeCheckError: the return value (grpc.aio._interceptor.UnaryStreamCallResponseIterator) is not an instance of grpc.aio._call.UnaryStreamCall
There was a problem hiding this comment.
One thing I considered is that UnaryStreamCallResponseIterator is not in the public API. But neither are any of these methods we're changing.
| ) -> StreamStreamCall: | ||
| request_serializer: Optional[SerializingFunction], | ||
| response_deserializer: Optional[DeserializingFunction], | ||
| ) -> Union[StreamStreamCall, StreamStreamCallResponseIterator]: |
There was a problem hiding this comment.
Typeguard stack trace
raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}")
typeguard.TypeCheckError: the return value (grpc.aio._interceptor.StreamStreamCallResponseIterator) is not an instance of grpc.aio._call.StreamStreamCall
also we have return StreamStreamCallResponseIterator in the method below.
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/786620461](http://cl/786620461) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 786620461
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/785301169](http://cl/785301169) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 785301169
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/786620461](http://cl/786620461) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 786620461
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/786620461](http://cl/786620461) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 786620461
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/785301169](http://cl/785301169) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 785301169
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/785301169](http://cl/785301169) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 785301169
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/785301169](http://cl/785301169) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 785301169
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/786620461](http://cl/786620461) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 786620461
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/785301169](http://cl/785301169) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 785301169
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/786620461](http://cl/786620461) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 786620461
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/786620461](http://cl/786620461) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 786620461
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/786620461](http://cl/786620461) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 786620461
COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d FUTURE_COPYBARA_INTEGRATE_REVIEW=#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d ---- DO NOT SUBMIT. This PR is for testing purposes only. [cl/786690273](http://cl/786690273) [cl/786606656](http://cl/786606656) [cl/786572880](http://cl/786572880) [cl/786529063](http://cl/786529063) [cl/786454401](http://cl/786454401) PiperOrigin-RevId: 786690273
) ### Description Part 3 of Add Typeguard to AIO stack - runs only in tests, fixes type hints for `grpc.aio._utils` and `grpc.aio._call` ### Testing CI ### Related work * Part1: #40201 * Part2: #40215 Closes #40217 This CL also has changes from https://github.com/grpc/grpc/pull/40286/files, because it was merged on Github without copybara. COPYBARA_INTEGRATE_REVIEW=#40217 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-3 cd82052 PiperOrigin-RevId: 786961247
…c#40215) Part 2 of Add Typeguard to AIO stack - runs only in tests, fixes type hints for `grpc.aio._interceptor` and `grpc.aio._utils` **Fixing the error in public API** - `ClientCallDetails` where `method` was `str` but actually it should be `bytes`. It was detected by `typeguard` as well. CI - Part 1: grpc#40201 - Part 3: grpc#40217 - b/423755915 Closes grpc#40215 This CL has changes from https://github.com/grpc/grpc/pull/40286/files because it was merged to master on Github without copybara. COPYBARA_INTEGRATE_REVIEW=grpc#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d PiperOrigin-RevId: 786606656
…c#40217) Part 3 of Add Typeguard to AIO stack - runs only in tests, fixes type hints for `grpc.aio._utils` and `grpc.aio._call` CI * Part1: grpc#40201 * Part2: grpc#40215 Closes grpc#40217 This CL also has changes from https://github.com/grpc/grpc/pull/40286/files, because it was merged on Github without copybara. COPYBARA_INTEGRATE_REVIEW=grpc#40217 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-3 cd82052 PiperOrigin-RevId: 786961247
…c#40217) ### Description Part 3 of Add Typeguard to AIO stack - runs only in tests, fixes type hints for `grpc.aio._utils` and `grpc.aio._call` ### Testing CI ### Related work * Part1: grpc#40201 * Part2: grpc#40215 Closes grpc#40217 This CL also has changes from https://github.com/grpc/grpc/pull/40286/files, because it was merged on Github without copybara. COPYBARA_INTEGRATE_REVIEW=grpc#40217 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-3 cd82052 PiperOrigin-RevId: 786961247
…c#40215) ### Description Part 2 of Add Typeguard to AIO stack - runs only in tests, fixes type hints for `grpc.aio._interceptor` and `grpc.aio._utils` ### Type hint for Public API change for Experimental API **Fixing the error in public API** - `ClientCallDetails` where `method` was `str` but actually it should be `bytes`. It was detected by `typeguard` as well. ### Testing CI ### Related Work - Part 1: grpc#40201 - Part 3: grpc#40217 - b/423755915 Closes grpc#40215 This CL has changes from https://github.com/grpc/grpc/pull/40286/files because it was merged to master on Github without copybara. COPYBARA_INTEGRATE_REVIEW=grpc#40215 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2 3fc7a9d PiperOrigin-RevId: 786606656
…c#40217) ### Description Part 3 of Add Typeguard to AIO stack - runs only in tests, fixes type hints for `grpc.aio._utils` and `grpc.aio._call` ### Testing CI ### Related work * Part1: grpc#40201 * Part2: grpc#40215 Closes grpc#40217 This CL also has changes from https://github.com/grpc/grpc/pull/40286/files, because it was merged on Github without copybara. COPYBARA_INTEGRATE_REVIEW=grpc#40217 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-3 cd82052 PiperOrigin-RevId: 786961247
…0226) ### Description Last Part of Add Typeguard to AIO stack - runs only in tests, fixes type hints for AIO Stack Change - #### Type Annotation Update in Channel Constructor File: _channel.py (line 330) Before: credentials: `Optional[grpc.ChannelCredentials]`, After: credentials: `Optional[cygrpc.ChannelCredentials]`, Change: Updated type hint from `grpc.ChannelCredentials` to `cygrpc.ChannelCredentials` #### Metadata Class Method Enhancement File: _metadata.py (around line 100-102) Before: `def from_tuple(cls, raw_metadata: tuple):` After: `def from_tuple(cls, raw_metadata: Union[tuple, Self]):` #### `GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY`decode at Cython layer. File: src/python/grpcio/grpc/_cython/_cygrpc/records.pyx.pxi Before: `GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY = ( _GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY)` After: `GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY = ( _GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY).decode()` ### Testing CI ### Related Work - Part 1: #40201 - Part 2: #40215 - Part 3: #40217 - Part 5: #40278 - b/423755915 Closes #40226 COPYBARA_INTEGRATE_REVIEW=#40226 from asheshvidyut:feature/type-hints/typeguard-aio-stack-part-4 70db2d6 PiperOrigin-RevId: 871724432
Description
Part 2 of Add Typeguard to AIO stack - runs only in tests, fixes type hints for
grpc.aio._interceptorandgrpc.aio._utilsType hint for Public API change for Experimental API
Fixing the error in public API -
ClientCallDetailswheremethodwasstrbut actually it should bebytes.The change is done because the code flow actually sends
bytesinstead ofstrsee - https://github.com/grpc/grpc/blob/master/src/python/grpcio/grpc/aio/_interceptor.py#L655It was detected by
typeguardas well.Testing
CI
Related Work