shabbyrobe / grpc-stubs

gRPC typing stubs for Python
MIT License
35 stars 21 forks source link

No stubs for `aio` #22

Closed damodar-anthem closed 1 year ago

damodar-anthem commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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: dropbox/mypy-protobuf#216

nipunn1313 commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Thanks to https://github.com/shabbyrobe/grpc-stubs/issues/22#issuecomment-1049961112 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|

shabbyrobe commented 1 year ago

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