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

mypy not happy with .pyi rewriting #689

Open
aucampia opened this issue Nov 22, 2022 · 2 comments
Open

mypy not happy with .pyi rewriting #689

aucampia opened this issue Nov 22, 2022 · 2 comments

Comments

@aucampia
Copy link

This project is a great, thank you very much for making and maintaining it.

I have tried using it with typing however and I notice that mypy is not entirely happy with the rewriting of .pyi files.

I have setup an example here: https://github.com/aucampia/issue-20221116-python_protobuf_cloudevents/tree/a245d29c2b1600b6580c6db861b4b715e72b9b51

The specific .pyi file that upsets mypy is generated/protol/_plgen/example/v1/messages_pb2.pyi and the untransformed file is generated/proto/example/v1/messages_pb2.pyi

With the mypy config from the example, running mypy yields:

$ python -m poetry run mypy --show-error-codes --show-error-context 
tests/test_conflicting_ns.py:80: note: In module imported here:
generated/protol/_plgen/example/v1/messages_pb2.pyi: note: In member "field_b" of class "Something":
generated/protol/_plgen/example/v1/messages_pb2.pyi:24: error: Name "io.cloudevents.v1.cloudevents_pb2.CloudEvent" is not defined  [name-defined]
generated/protol/_plgen/example/v1/messages_pb2.pyi: note: In member "__init__" of class "Something":
generated/protol/_plgen/example/v1/messages_pb2.pyi:27: error: Name "io.cloudevents.v1.cloudevents_pb2.CloudEvent" is not defined  [name-defined]
Found 2 errors in 1 file (checked 5 source files)
make: *** [Makefile:78: python-validate-static] Error 1

The specific part of the transformation that is causing the problem is this I believe:

$ diff -u -r generated/proto/example/v1/messages_pb2.pyi generated/protol/_plgen/example/v1/messages_pb2.pyi 
--- generated/proto/example/v1/messages_pb2.pyi	2022-11-22 09:08:54.417836214 +0100
+++ generated/protol/_plgen/example/v1/messages_pb2.pyi	2022-11-22 09:08:55.062838530 +0100
@@ -5,32 +5,31 @@
 import builtins
 import google.protobuf.descriptor
 import google.protobuf.message
-import io.cloudevents.v1.cloudevents_pb2
+from ... import io
 import sys

If I change this by hand to instead do from ...io.cloudevents.v1 import cloudevents_pb2 as io_dot_cloudevents_dot_v1_dot_cloudevents__pb2 and make corresponding changes in the rest of the file to use io_dot_cloudevents_dot_v1_dot_cloudevents__pb2 instead of io.cloudevents.v1.cloudevents_pb2 then mypy is entirely happy.

@cpcloud
Copy link
Owner

cpcloud commented Mar 18, 2023

@aucampia Can you paste the .proto file that reproduces this issue somewhere?

@aucampia
Copy link
Author

All the proto files are in here: https://github.com/aucampia/issue-20221116-python_protobuf_cloudevents/tree/a245d29c2b1600b6580c6db861b4b715e72b9b51/spec/proto

but the file that the problems originate from is spec/proto/example/v1/messages.proto and its content is:

syntax = "proto3";

package example.v1;

import "io/cloudevents/v1/cloudevents.proto";

message Something {
  string field_a = 1;
  io.cloudevents.v1.CloudEvent field_b = 2;
}

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