8000 Fix import loop with Pydantic (#624) · AdrienVannson/python-betterproto@6a3bbe3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a3bbe3

Browse files
Fix import loop with Pydantic (danielgtaylor#624)
1 parent c2bcd31 commit 6a3bbe3

File tree

4 files changed

+11
-20
lines changed

4 files changed

+11
-20
lines changed

src/betterproto/plugin/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class OutputTemplate:
238238
parent_request: PluginRequestCompiler
239239
package_proto_obj: FileDescriptorProto
240240
input_files: List[str] = field(default_factory=list)
241-
imports: Set[str] = field(default_factory=set)
241+
imports_end: Set[str] = field(default_factory=set)
242242
datetime_imports: Set[str] = field(default_factory=set)
243243
pydantic_imports: Set[str] = field(default_factory=set)
244244
builtins_import: bool = False
@@ -532,7 +532,7 @@ def py_type(self) -> str:
532532
# Type referencing another defined Message or a named enum
533533
return get_type_reference(
534534
package=self.output_file.package,
535-
imports=self.output_file.imports,
535+
imports=self.output_file.imports_end,
536536
source_type=self.proto_obj.type_name,
537537
typing_compiler=self.typing_compiler,
538538
pydantic=self.output_file.pydantic_dataclasses,
@@ -730,7 +730,7 @@ def py_input_message_type(self) -> str:
730730
"""
731731
return get_type_reference(
732732
package=self.output_file.package,
733-
imports=self.output_file.imports,
733+
imports=self.output_file.imports_end,
734734
source_type=self.proto_obj.input_type,
735735
typing_compiler=self.output_file.typing_compiler,
736736
unwrap=False,
@@ -760,7 +760,7 @@ def py_output_message_type(self) -> str:
760760
"""
761761
return get_type_reference(
762762
package=self.output_file.package,
763-
imports=self.output_file.imports,
763+
imports=self.output_file.imports_end,
764764
source_type=self.proto_obj.output_type,
765765
typing_compiler=self.output_file.typing_compiler,
766766
unwrap=False,

src/betterproto/templates/header.py.j2

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {{ i }}
88

99
{% if output_file.pydantic_dataclasses %}
1010
from pydantic.dataclasses import dataclass
11-
from pydantic.dataclasses import rebuild_dataclass
1211
{%- else -%}
1312
from dataclasses import dataclass
1413
{% endif %}
@@ -35,10 +34,6 @@ from betterproto.grpc.grpclib_server import ServiceBase
3534
import grpclib
3635
{% endif %}
3736

38-
{% for i in output_file.imports|sort %}
39-
{{ i }}
40-
{% endfor %}
41-
4237
{% if output_file.imports_type_checking_only %}
4338
from typing import TYPE_CHECKING
4439

src/betterproto/templates/template.py.j2

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
7777
, {{ method.py_input_message_param }}: "{{ method.py_input_message_type }}"
7878
{%- else -%}
7979
{# Client streaming: need a request iterator instead #}
80-
, {{ method.py_input_message_param }}_iterator: {{ output_file.typing_compiler.union(output_file.typing_compiler.async_iterable(method.py_input_message_type), output_file.typing_compiler.iterable(method.py_input_message_type)) }}
80+
, {{ method.py_input_message_param }}_iterator: "{{ output_file.typing_compiler.union(output_file.typing_compiler.async_iterable(method.py_input_message_type), output_file.typing_compiler.iterable(method.py_input_message_type)) }}"
8181
{%- endif -%}
8282
,
8383
*
8484
, timeout: {{ output_file.typing_compiler.optional("float") }} = None
8585
, deadline: {{ output_file.typing_compiler.optional('"Deadline"') }} = None
8686
, metadata: {{ output_file.typing_compiler.optional('"MetadataLike"') }} = None
87-
) -> {% if method.server_streaming %}{{ output_file.typing_compiler.async_iterator(method.py_output_message_type ) }}{% else %}"{{ method.py_output_message_type }}"{% endif %}:
87+
) -> "{% if method.server_streaming %}{{ output_file.typing_compiler.async_iterator(method.py_output_message_type ) }}{% else %}{{ method.py_output_message_type }}{% endif %}":
8888
{% if method.comment %}
8989
{{ method.comment }}
9090

@@ -143,6 +143,10 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
143143
{% endfor %}
144144
{% endfor %}
145145

146+
{% for i in output_file.imports_end %}
147+
{{ i }}
148+
{% endfor %}
149+
146150
{% for service in output_file.services %}
147151
class {{ service.py_name }}Base(ServiceBase):
148152
{% if service.comment %}
@@ -211,11 +215,3 @@ class {{ service.py_name }}Base(ServiceBase):
211215
}
212216

213217
{% endfor %}
214-
215-
{% if output_file.pydantic_dataclasses %}
216-
{% for message in output_file.messages %}
217-
{% if message.has_message_field %}
218-
rebuild_dataclass({{ message.py_name }}) # type: ignore
219-
{% endif %}
220-
{% endfor %}
221-
{% endif %}

tests/inputs/import_circular_dependency/import_circular_dependency.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ import "other.proto";
2626
// (root: Test & RootPackageMessage) <-------> (other: OtherPackageMessage)
2727
message Test {
2828
RootPackageMessage message = 1;
29-
other.OtherPackageMessage other = 2;
29+
other.OtherPackageMessage other_value = 2;
3030
}

0 commit comments

Comments
 (0)
0