Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No stubs for aio #22

Closed
damodar-anthem opened this issue Jul 29, 2021 · 9 comments
Closed

No stubs for aio #22

damodar-anthem opened this issue Jul 29, 2021 · 9 comments

Comments

@damodar-anthem
Copy link

Is there any plans to include stubs for grpc.aio? I love this package but it doesn't seem to provide the aio features. I am more than willing to help out if necessary. I also saw a discussion about this in #15, but not sure what's happening here.

@shabbyrobe
Copy link
Owner

Thanks for reaching out! There are no concrete plans but there's definitely a desire.

The last PR had to be reverted because it introduced a ton of problems I didn't have the time or inclination to work through. It was all autogenerated, didn't play nice with the rest of the package, and when I tried to write tests it showed up a lot of issues I didn't want to impose on package users. The PRs I get here are usually fine but this one caught me unawares and I just haven't come back to it yet.

I think one of the things that would most help nudge me into action here would be if you had any simple, runnable examples of the ways you use the package lying around. That'd help me set up an environment (which if I'm honest, I find a bit of an "ugh" task) I can use to validate whether we're on the right track. The easier it is for me to run the examples without faffing around, the likelier it is that I'll get cracking on it.

After the issues with the previous attempt, I probably won't just accept something wholesale again, I'd prefer to be involved, especially as there seemed to be a lot of messy overlap between the different coloured versions. I certainly won't be merging something that large without tests again, I've learned my lesson there! But as I'm not using the aio part of the package myself, that's really where those examples will be a huge help.

@damodar-anthem
Copy link
Author

damodar-anthem commented Aug 4, 2021

A sample aio test case could look like:

@pytest.mark.asyncio
async def test_grpc_response() -> None:
    request = my_pb2.MyRequest(prop1="hello")

    async with grpc.aio.insecure_channel("localhost:8000") as channel:
        stub = my_pb2_grpc.MyServiceStub(channel)
        response = await stub.GetResponse(request)

    assert response

Note that mypy also doesn't support the generated pb2 and pb2_grpc files properly. An issue has been filed with mypy-protobuf as well.

See: nipunn1313/mypy-protobuf#216

@nipunn1313
Copy link

My understanding is that generating stubs for _pb2.py (andpb2_grpc.py for non-aio use cases) are supported by mypy-protobuf

In order to generate reasonable stubs for pb2_grpc for aio use cases, it'll need to import grpc.aio.Server to generate roughly this (or potentially two overloaded definitions - one for sync, one for async).

def add_DummyServiceServicer_to_server(
    servicer: DummyServiceServicer,
    server: typing.Union[grpc.Server, grpc.aio.AioServer]) -> None: ...

Hence I think this task is a pre-req for mypy-protobuf support.

@artificial-aidan
Copy link

Tagging onto this. Over here I ran into an issue where when i use grpc-stubs it breaks any sort of typing for aio. Where if I don't have grpc-stubs installed it gets some basic type info (and doesn't fail to import). Is there an easy way to make grpc-stubs only be a stub package for the non-aio subset? Or am I stuck with a this or that.

@hmc-cs-mdrissi
Copy link

hmc-cs-mdrissi commented Feb 24, 2022

I think you are asking for a partial stub package. https://www.python.org/dev/peps/pep-0561/#partial-stub-packages If stub package includes the line partial\n in it's py.typed it is treated as only documenting a part of package.

https://github.com/shabbyrobe/grpc-stubs/blob/master/grpc-stubs/py.typed add one line to that file and that may be enough to fix your issue.

@gitpushdashf
Copy link
Contributor

There's no stubs for grpc aio, to my knowledge. As a workaround, you can do this:

pyproject.toml:

[[tool.mypy.overrides]]
module = [
    "grpc.aio.*",  # https://github.com/shabbyrobe/grpc-stubs/issues/22
]
ignore_missing_imports = true

@artificial-aidan
Copy link

I think you are asking for a partial stub package. https://www.python.org/dev/peps/pep-0561/#partial-stub-packages If stub package includes the line partial\n in it's py.typed it is treated as only documenting a part of package.

https://github.com/shabbyrobe/grpc-stubs/blob/master/grpc-stubs/py.typed add one line to that file and that may be enough to fix your issue.

@shabbyrobe would you be open to a PR for this if it works?

shonfeder pushed a commit to apalache-mc/apalache-chai that referenced this issue Jul 25, 2022
Required since stubs for grpc.aio are not yet available

See shabbyrobe/grpc-stubs#22 (comment)
@shonfeder
Copy link

shonfeder commented Jul 25, 2022

Thanks to #22 (comment) for the tip, here's a workaround for pyright:

In your source, import aio as

import grpc.aio as aio

to ensure we get a missing import error instead of a missing symbol error. Then in the pyproject.toml:

[tool.pyright]
exclude = ["**/*_pb2*"]
# https://github.com/shabbyrobe/grpc-stubs/issues/22
reportMissingImports = "warning"

But an even more targeted workaround is something like:

# TODO remove ignore when stubs are availabe for grpc.aio
# See https://github.com/shabbyrobe/grpc-stubs/issues/22
import grpc.aio as aio  # type: ignore

UPDATE: doh! sorry for the commit spam! I shouldn't have linked this issue in the commit X|

shonfeder pushed a commit to apalache-mc/apalache-chai that referenced this issue Jul 25, 2022
Required since stubs for grpc.aio are not yet available

See shabbyrobe/grpc-stubs#22 (comment)
@shabbyrobe
Copy link
Owner

There are now incomplete subs for aio. Completing them is happening in #36. I think we can consider this issue complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants