Skip to content
This repository was archived by the owner on Jun 11, 2026. It is now read-only.

Issue 850 fix imports in pyi files - #851

Open
juraj-juraj wants to merge 2 commits into
cpcloud: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 into
cpcloud: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

cpcloud commented May 18, 2023

Copy link
Copy Markdown
Owner

@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

cpcloud commented May 25, 2023

Copy link
Copy Markdown
Owner

Ok, I can take a look at the failures.

@cpcloud cpcloud left a comment

Copy link
Copy Markdown
Owner

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

cpcloud commented May 25, 2023

Copy link
Copy Markdown
Owner

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

cpcloud commented May 25, 2023

Copy link
Copy Markdown
Owner

@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

metasyn commented Apr 8, 2024

Copy link
Copy Markdown

👋 What is the conclusion here?

@c1moore

c1moore commented Feb 11, 2026

Copy link
Copy Markdown

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants