Skip to content

[Python][Typeguard] Part 2 - Add Typeguard to AIO stack in tests #40215

Closed
asheshvidyut wants to merge 15 commits intogrpc:masterfrom
asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2
Closed

[Python][Typeguard] Part 2 - Add Typeguard to AIO stack in tests #40215
asheshvidyut wants to merge 15 commits intogrpc:masterfrom
asheshvidyut:feature/type-hints/typeguard-aio-stack-part-2

Conversation

@asheshvidyut
Copy link
Member

@asheshvidyut asheshvidyut commented Jul 15, 2025

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.
The change is done because the code flow actually sends bytes instead of str see - https://github.com/grpc/grpc/blob/master/src/python/grpcio/grpc/aio/_interceptor.py#L655

It was detected by typeguard as well.

Testing

CI

Related Work

raise NotImplementedError()


class StreamStreamCallResponseIterator(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class and 3 classes above are cut - paste from the file below, because the class was needed for type hints.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With typeguard version bump - this works. Thanks.

@asheshvidyut asheshvidyut marked this pull request as ready for review July 15, 2025 13:51
@asheshvidyut asheshvidyut requested a review from veblush as a code owner July 15, 2025 13:51
@asheshvidyut asheshvidyut changed the title [Python] [Part 2] - Add Typeguard to AIO stack in tests [Python] [Typeguard] Part 2 - Add Typeguard to AIO stack in tests Jul 15, 2025
raise NotImplementedError()


class StreamStreamCallResponseIterator(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be reverted.

self, request: RequestType, call: InterceptedCall
self,
request: RequestType,
call: Union[InterceptedCall, _base_call.Call],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@asheshvidyut asheshvidyut requested a review from eugeneo as a code owner July 22, 2025 05:34
@asheshvidyut asheshvidyut requested review from sergiitk and sreenithi and removed request for eugeneo and veblush July 22, 2025 05:43
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jul 22, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@asheshvidyut asheshvidyut force-pushed the feature/type-hints/typeguard-aio-stack-part-2 branch from f4abeef to eb3f14c Compare July 22, 2025 15:38
@asheshvidyut asheshvidyut added release notes: yes Indicates if PR needs to be in release notes and removed release notes: no Indicates if PR should not be in release notes labels Jul 22, 2025
@asheshvidyut asheshvidyut force-pushed the feature/type-hints/typeguard-aio-stack-part-2 branch from 935d65b to e0af9a8 Compare July 23, 2025 10:58
@sergiitk sergiitk changed the title [Python] [Typeguard] Part 2 - Add Typeguard to AIO stack in tests [Python][Typeguard] Part 2 - Add Typeguard to AIO stack in tests Jul 23, 2025
) -> UnaryStreamCall:
request_serializer: Optional[SerializingFunction],
response_deserializer: Optional[DeserializingFunction],
) -> Union[UnaryStreamCall, UnaryStreamCallResponseIterator]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

@sergiitk sergiitk Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - same as above. LGTM.

copybara-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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-service bot pushed a commit that referenced this pull request Jul 24, 2025
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
@ctiller ctiller closed this in 4499b46 Jul 24, 2025
copybara-service bot pushed a commit that referenced this pull request Jul 25, 2025
)

### 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
asheshvidyut added a commit to asheshvidyut/grpc that referenced this pull request Jul 28, 2025
…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
asheshvidyut added a commit to asheshvidyut/grpc that referenced this pull request Jul 28, 2025
…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
@asheshvidyut asheshvidyut added this to the Python Type Hints Support milestone Aug 7, 2025
eugeneo pushed a commit to eugeneo/grpc that referenced this pull request Aug 11, 2025
…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
paulosjca pushed a commit to paulosjca/grpc that referenced this pull request Aug 23, 2025
…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
paulosjca pushed a commit to paulosjca/grpc that referenced this pull request Aug 23, 2025
…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
copybara-service bot pushed a commit that referenced this pull request Feb 18, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants