Skip to content

Issue 850 fix imports in pyi files#851

Open
juraj-juraj wants to merge 2 commits intocpcloud:mainfrom
juraj-juraj:issue-850-fix-imports-in-pyi-files
Open

Issue 850 fix imports in pyi files#851
juraj-juraj wants to merge 2 commits intocpcloud:mainfrom
juraj-juraj:issue-850-fix-imports-in-pyi-files

Conversation

@juraj-juraj
Copy link
Copy Markdown

This pull request fixes issue #850.

Imports in pyi file generated by grpc_tools.protoc are different, thus rewrite rules do not catch it.
New rule was added to account for that.

@cpcloud
Copy link
Copy Markdown
Owner

cpcloud commented May 18, 2023

@juraj-juraj Sorry for the delay!

Would you mind adding a test case, or describing it here if you don't want to do that?

@juraj-juraj
Copy link
Copy Markdown
Author

@cpcloud This change basically adds an import statement. So because of that, the tests will fail. I think to test this enough would be to just fix the tests. Sadly I wasn't able to make tests work, otherwise I would've done it.

@cpcloud
Copy link
Copy Markdown
Owner

cpcloud commented May 25, 2023

Ok, I can take a look at the failures.

Copy link
Copy Markdown
Owner

@cpcloud cpcloud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently generating invalid Python code.

@cpcloud cpcloud force-pushed the issue-850-fix-imports-in-pyi-files branch from 0004eed to 4047fc2 Compare May 25, 2023 13:31
@cpcloud
Copy link
Copy Markdown
Owner

cpcloud commented May 25, 2023

Ok, I got the tests passing, but I think the rules are incorrect.

The rewrite should only apply to pyi files, whereas now the patterns apply to every file.

@cpcloud
Copy link
Copy Markdown
Owner

cpcloud commented May 25, 2023

@juraj-juraj can you paste here the following:

  1. a proto file you used to trigger the bug
  2. the codegen command you ran, e.g., buf/protoc/grpc_tools.protoc invocation
  3. the protol invocation

From those I can build a test to verify the desired behavior

@juraj-juraj
Copy link
Copy Markdown
Author

I'm including whole hierarchy of protofiles used protofiles.zip. The bug is only apparent in pyi files, where import simply isn't fixed. The rule which I introduced applies to all files, not just .pyi. I didn't know whether it would break something, and it worked for me. So that's why it is fixed like it is.
The protoc invocation:

 python3 -m grpc_tools.protoc -I./ --python_out=./src/phx_grpc_interface --grpc_python_out=./src/phx_grpc_interface --pyi_out=./src/phx_grpc_interface ./phonexia/common/core.proto ./phonexia/common/health_check.proto ./phonexia/technologies/genderid/v1beta/gid.proto ./phonexia/technologies/speakerid/v1beta/sid.proto

The protol invocation:

protol -o ./src/phx_grpc_interface --in-place protoc --proto-path=./ ./phonexia/common/core.proto ./phonexia/common/health_check.proto ./phonexia/technologies/genderid/v1beta/gid.proto ./phonexia/technologies/speakerid/v1beta/sid.proto

@v-chernyshev
Copy link
Copy Markdown

Just stumbled upon this issue in one of our projects. The proposed rewrite rule doesn't seem right:

old=f"from {from_} import {part}_pb2 as _{part}_pb2",
new=f"from {leading_dots}{from_} import _{part}_pb2",

There is no bare _{part}_pb2 in the generated code. I think the new rule should be from {leading_dots}{from_} import {part}_pb2 as _{part}_pb2 instead.

But the bigger problem is that this change does not account for name collisions. Imagine that you have packages pkg_1 and pkg_2 that both define a message type msg. And these are then imported into pkg_3.msg. The non-transformed pyi output for pkg_3.msg would contain something like this:

from pkg_1 import msg_pb2 as _msg_pb2
from pkg_2 import msg_pb2 as _msg_pb2_1

The _N suffix will be incremented for each subsequent collision.

@metasyn
Copy link
Copy Markdown

metasyn commented Apr 8, 2024

👋 What is the conclusion here?

@c1moore
Copy link
Copy Markdown

c1moore commented Feb 11, 2026

@cpcloud Are there any plans to move forward with this?

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.

5 participants