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

Using _Metadata as a typing #109

Closed
paulefoe opened this issue May 16, 2020 · 2 comments
Closed

Using _Metadata as a typing #109

paulefoe opened this issue May 16, 2020 · 2 comments

Comments

@paulefoe
Copy link

paulefoe commented May 16, 2020

Hi, thanks for the great library, we are really enjoying using it!

My question is regarding _Metadata being a protected member of the module. Is there any reason for this?
Because sometimes we want to manipulate metadata inside our business logic and we send it as an argument to a function.
Then, trying to annotate our function with types we would get a warning from IDE that it is protected.
I would be glad to provide PR if you think this is okay?

Here's the small example:

async def hello_world(
        self,
        stream: Stream[HelloWorldRequest, HelloWorldResponse]
) -> None:
        request = await stream.recv_message()
        assert request is not None
        result = await say_hello_with_metadata(
              request.name,
              stream.metadata,
        )
        await stream.send_message(result)

async def say_hello_with_metadata(name: str, metadata: _Metadata) -> str:
        ...
@vmagamedov
Copy link
Owner

Hi, thanks!

I didn't wanted to expose implementation details. Metadata should be an abstract multidict-like data structure from a user's perspective, not a specific type – multidict.Multidict.

There are two possible solutions:

  • if you don't expect keys with multiple values you can use the typing.Mapping type, because Multidict implements the Mapping protocol and mypy should be ok with that
  • if you expect multiple values you can implement a class Multidict(typing.Protocol): protocol and use it, mypy should be also ok with that

I don't want to add this protocol into grpclib because protocols were introduced only in Python 3.8 or you should depend on typing_extensions, and I think that there should be other place for such generic protocols.

@paulefoe
Copy link
Author

Great, thank you for the quick reply! Will do just that. Have a nice day!

I'm closing it.

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

2 participants