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

The generation logic for import statements has issues in some cases #62

Open
kasegao opened this issue Aug 24, 2024 · 2 comments · May be fixed by #65
Open

The generation logic for import statements has issues in some cases #62

kasegao opened this issue Aug 24, 2024 · 2 comments · May be fixed by #65

Comments

@kasegao
Copy link

kasegao commented Aug 24, 2024

Describe the bug

The _add_other_module_pkg function in protobuf_to_pydantic/plugin/field_desc_proto_to_code.py has issues in some cases. Specifically, in cases where the top-level paths are different and the intermediate paths match, the generated relative import paths are not appropriate.

To Reproduce

Consider the following directory structure. Both files share the intermediate path v1.

.
├─ app/v1/app.proto
└─ shared/v1/types.proto

app/v1/app.proto

syntax = "proto3";

package user.v1;

import "shared/v1/types.proto";

service AppService {
  rpc GetMe(GetMeRequest) returns (GetMeResponse);
}

message GetMeRequest {}
message GetMeResponse {
  shared.v1.UserType user_type = 1;
  string name = 2;
  int32 age = 3;
}

shared/v1/types.proto

syntax = "proto3";

package shared.v1;

enum UserType {
  USER_TYPE_UNSPECIFIED = 0;
  USER_TYPE_NORMAL = 1;
  USER_TYPE_GUEST = 2;
}

When generating Pydantic code for these files, the following files are created:

app/v1/app_p2p.py

from .types_p2p import UserType
from google.protobuf.message import Message  # type: ignore
from pydantic import BaseModel
from pydantic import Field


class GetMeRequest(BaseModel):    pass

class GetMeResponse(BaseModel):
    user_type: UserType = Field(default=0)
    name: str = Field(default="")
    age: int = Field(default=0)

shared/v1/types_p2p.py

from enum import IntEnum
from pydantic import BaseModel

class UserType(IntEnum):
    USER_TYPE_UNSPECIFIED = 0
    USER_TYPE_NORMAL = 1
    USER_TYPE_GUEST = 2

However, the import statement in the first line of app/v1/app_p2p.py fails to resolve the relative path correctly.

from .types_p2p import UserType

Expected behavior

I suggest specifying the path from the project root instead of using a relative path. In that case, the previously mentioned import statement should preferably be

from shared.v1.types_p2p import UserType

Expectied import path:

from ...shared.v1.types_p2p import UserType

Desktop (please complete the following information):

  • OS: macOS Sonoma 14.5
  • Version MacBook Pro M3 Max (2023)
@kasegao
Copy link
Author

kasegao commented Sep 1, 2024

I'm sorry, but the expected behavior I originally wrote was inappropriate, so I updated it.

@kasegao kasegao linked a pull request Sep 1, 2024 that will close this issue
@so1n
Copy link
Owner

so1n commented Sep 9, 2024

Thank you very much for your feedback. There is no problem with this code in the current example proto. Can you add the corresponding proto to the example in the project and execute it? It would be great if you could add corresponding test cases @kasegao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants