Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions example/example_proto/test_ignore/ignored_pkg.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package test_ignore.ignored_pkg;

message IgnoredMessage {
string text = 1;
}

enum IgnoredEnum {
IGNORED_ZERO = 0;
IGNORED_ONE = 1;
}
10 changes: 10 additions & 0 deletions example/example_proto/test_ignore/main_pkg.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package test_ignore.main_pkg;

message MainMessage {
string normal_field = 1;
// These types should be used but NOT imported due to ignore_pkg_list
test_ignore.ignored_pkg.IgnoredMessage ignored_field = 2;
test_ignore.ignored_pkg.IgnoredEnum ignored_enum = 3;
}
2 changes: 1 addition & 1 deletion example/plugin_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def exp_time() -> float:
}
comment_prefix = "p2p"
template: Type[Template] = CustomCommentTemplate
ignore_pkg_list: List[str] = ["validate", "p2p_validate"]
ignore_pkg_list: List[str] = ["validate", "p2p_validate", "test_ignore.ignored_pkg"]
pkg_config: Dict[str, SubConfigModel] = {
"all_field_set_optional": SubConfigModel(module=all_field_set_option_config, use_root_config=True),
"single_config": SubConfigModel(module=single_config_pkg_plugin_config),
Expand Down
4 changes: 4 additions & 0 deletions protobuf_to_pydantic/plugin/field_desc_proto_to_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def _add_other_module_pkg(self, other_fd: FileDescriptorProto, type_str: str) ->
if other_fd.name == self._fd.name:
return

# Skip import generation for ignored packages
if other_fd.package in self.config.ignore_pkg_list:
return

fd_path_list: Tuple[str, ...] = Path(self._fd.name).parts
message_path_list: Tuple[str, ...] = Path(other_fd.name).parts
index: int = -1
Expand Down
36 changes: 36 additions & 0 deletions tests/test_gen_code/test_demo_proto/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from inspect import getsource

from google.protobuf import __version__
Expand Down Expand Up @@ -311,3 +312,38 @@ class Demo2(BaseModel):
class Demo1(BaseModel):
pass"""
assert content.strip("\n") in getsource(diff_pkg_refer_2_p2p.Demo1).strip("\n")

def test_ignore_pkg_list_skips_imports(self) -> None:
"""Test that packages in ignore_pkg_list do not generate relative imports."""
# Determine which proto version to test
if __version__ > "4.0.0":
proto_path = "proto_pydanticv2" if not is_v1 else "proto_pydanticv1"
else:
proto_path = "proto_3_20_pydanticv2" if not is_v1 else "proto_3_20_pydanticv1"

test_file_path = f"example/{proto_path}/example_proto/test_ignore/main_pkg_p2p.py"

# Check if the test file exists (should be generated by tox/generation script)
if not os.path.exists(test_file_path):
# Skip this test if files haven't been generated yet
# (This can happen when running tests directly without tox example generation)
import pytest
pytest.skip(f"Test file {test_file_path} not found - run tox example generation first")

# Read the generated file directly
with open(test_file_path, 'r') as f:
source = f.read()

# Test that NO import statements were generated for ignored package
assert "from .ignored_pkg_p2p import" not in source
assert "from ..test_ignore.ignored_pkg_p2p import" not in source
assert "import IgnoredMessage" not in source
assert "import IgnoredEnum" not in source

# Test that the class structure exists but types are undefined (demonstrating the ignore works)
assert "class MainMessage(BaseModel):" in source
assert "ignored_field: IgnoredMessage" in source # Type used but not imported
assert "ignored_enum: IgnoredEnum" in source # Type used but not imported

# Verify normal field works fine
assert "normal_field: str" in source