GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add type annotations to wrapped grpc calls by daniel-sanche · Pull Request #554 · googleapis/python-api-core

This repository was archived by the owner on Feb 23, 2026. It is now read-only.

Conversation

@daniel-sanche

@daniel-sanche daniel-sanche commented Nov 17, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Our gapic libraries declare that they return Iterable[SomeProto] or Awaitable[SomeProto] or Awaitable[AsyncIterable[SomeProto], but that doesn't tell the whole story. The objects returned are actually grpc.Call subclasses, which expose the ability to retrieve metadata from the rpc, or call cancel, or other trigger useful functionality.

Unfortunately, there is currently no way to use these grpc.Call methods without mypy errors, because of the restrictive return type annotation used

This PR is the first step in addressing this, by giving us a more powerful return type in api-core.

On the sync side, I made _StreamingResponseIterator into a Generic container, and gave it a better public facing name. This way, we we can return GrpcStream[SomeProto] instead of Iterable[SomeProto], with all grpc.Call methods accessible

On the Async side, I made every _WrappedXYResponse class into a Generic container, and declared new GrpcAsyncStream[SomeProto] and AwaitableGrpcCall[SomeProto] types that can be used in place of AsyncIterable[SomeProto] and Awaitable[SomeProto] respectively.

For more context on the motivating problem, see googleapis/gapic-generator-python#1856. This PR lays some of the groundwork for resolving that issue in the future. But the type improvements here should also stand alone

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
daniel-sanche added 2 commits November 16, 2023 16:30
@daniel-sanche
add types to grpc call wrappers
709eb49
@daniel-sanche
fixed tests
2026974
@daniel-sanche
daniel-sanche requested review from a team November 17, 2023 01:28
@product-auto-label product-auto-label Bot added the size: s Pull request size is small. label Nov 17, 2023
@gcf-owl-bot
🦉 Updates from OwlBot post-processor
f65b0c7
See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md
@product-auto-label product-auto-label Bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Nov 17, 2023
daniel-sanche added 3 commits November 16, 2023 17:55
@daniel-sanche
changed type
373ef40
@daniel-sanche
changed async types
2c20dec
@daniel-sanche
added tests
430e033
@parthea

parthea commented Nov 17, 2023

Copy link
Copy Markdown
Contributor

@daniel-sanche Please could you address the lint failure?

daniel-sanche reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@daniel-sanche
fixed lint issues
3cf50bb
@daniel-sanche

daniel-sanche commented Nov 17, 2023

Copy link
Copy Markdown
Contributor Author

@parthea done

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@daniel-sanche
Merge branch 'main' into type-annotate-grpc-wrappers
4f8fbcd
daniel-sanche
daniel-sanche commented Nov 17, 2023
View reviewed changes
Comment thread google/api_core/grpc_helpers_async.py Outdated
# public type alias denoting the return type of streaming gapic calls
GrpcAsyncStream = _WrappedStreamResponseMixin[S]
# public type alias denoting the return type of unary gapic calls
AwaitableGrpcCall = _WrappedUnaryResponseMixin[U]

@daniel-sanche daniel-sanche Nov 17, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Let me know if you have other naming suggestions for these (AsyncGrpcCall? GrpcAsyncIterable?)

I liked Awaitable because it's clear how to interact with it, and Stream instead of Iterable because it can do more than just iterate. But names are hard and I'm open to alternatives

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

parthea reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
parthea
parthea reviewed Nov 17, 2023
View reviewed changes
Comment thread tests/asyncio/test_grpc_helpers_async.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/api_core/grpc_helpers.py Outdated


class _StreamingResponseIterator(grpc.Call):
class GrpcStream(grpc.Call, Generic[S]):

@parthea parthea Nov 17, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Do you think this could be a breaking change? Could we keep the name as _StreamingResponseIterator ? There are many hits for _StreamingResponseIterator in Google search. I'm worried that changing _StreamingResponseIterator could cause issues downstream in user code as it is a response we provide.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@daniel-sanche daniel-sanche Nov 17, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Makes sense, I'll create a type alias instead, like I did on the async side

I thought it would be safe to change since it's a private class, but better to be on the safe side

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

parthea reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
daniel-sanche and others added 6 commits November 17, 2023 09:48
@daniel-sanche @parthea
Update tests/asyncio/test_grpc_helpers_async.py
b817527
Co-authored-by: Anthonios Partheniou <[email protected]>
@daniel-sanche
turned GrpcStream into a type alias
623becd
@daniel-sanche
added test for GrpcStream
781cdee
@gcf-owl-bot
🦉 Updates from OwlBot post-processor
9c3cd54
See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md
@daniel-sanche
added comment
5d674e7
@daniel-sanche
reordered types
06a5ea9
parthea
parthea approved these changes Nov 17, 2023
View reviewed changes
daniel-sanche and others added 2 commits November 17, 2023 10:23
@daniel-sanche
changed type var to P
10ef5bf
@daniel-sanche
Merge branch 'main' into type-annotate-grpc-wrappers
7e43af7
parthea
parthea approved these changes Nov 17, 2023
View reviewed changes
@daniel-sanche
daniel-sanche merged commit fc12b40 into main Nov 17, 2023
@daniel-sanche
daniel-sanche deleted the type-annotate-grpc-wrappers branch November 17, 2023 18:29
@release-please release-please Bot mentioned this pull request Nov 17, 2023
chore(main): release 2.15.0 #555
Merged
@parthea parthea mentioned this pull request Dec 4, 2023
chore(main): Release candidate v2.15.0rc1 #565
Closed
@parthea parthea mentioned this pull request Nov 19, 2024
fix: update return annotation for streaming calls googleapis/gapic-generator-python#2256
Closed
This was referenced May 30, 2025
chore(main): release 2.0.0 #822
Closed
chore(main): release 2.0.0 #823
Closed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

1 more reviewer
@parthea parthea parthea approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

size: m Pull request size is medium.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@daniel-sanche @parthea
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.