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

Avoid relative imports in grpc code #278

Merged
merged 1 commit into from
Aug 23, 2021
Merged

Avoid relative imports in grpc code #278

merged 1 commit into from
Aug 23, 2021

Conversation

nipunn1313
Copy link
Owner

Fixes #277

@nipunn1313
Copy link
Owner Author

Would appreciate a review from either @MHDante or @Evgenus as well as they've contributed majorly to the GRPC stubs here.

My rationale is that this matches more closely to how the _grpc_pb2.py work, and obviates need for __init__.py files (at least matches more closely w/ how the .py works.

@Evgenus
Copy link
Contributor

Evgenus commented Aug 19, 2021

Looks good but what if generated files are not in root package?

@nipunn1313
Copy link
Owner Author

I believe that you need to add your generated files directory to your PYTHONPATH or sys.path for things to work (see protocolbuffers/protobuf#881). I think it's just a shortcoming of protobuf. Looks like the task was wont-fixed in 2017. I don't think that use case is well supported unfortunately.

mypy will find imports via this mechanism https://mypy.readthedocs.io/en/stable/running_mypy.html#finding-imports

For example, in our testsuite, the top of dummy_pb2_grpc.py looks like this

# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

from testproto.grpc import dummy_pb2 as testproto_dot_grpc_dot_dummy__pb2

^ not using relative import.

With this diff, the dummy_pb2_grpc.pyi will match that - and thus work more similarly in cases like the one in #277

@nipunn1313 nipunn1313 merged commit 13f2ca3 into main Aug 23, 2021
@nipunn1313 nipunn1313 deleted the grpc_absolute branch August 28, 2021 06:31
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

Successfully merging this pull request may close these issues.

Not getting the type for the grpc response
2 participants