Fix import loop with Pydantic (#624)
This commit is contained in:
parent
c2bcd31fe3
commit
6a3bbe3f25
@ -238,7 +238,7 @@ class OutputTemplate:
|
|||||||
parent_request: PluginRequestCompiler
|
parent_request: PluginRequestCompiler
|
||||||
package_proto_obj: FileDescriptorProto
|
package_proto_obj: FileDescriptorProto
|
||||||
input_files: List[str] = field(default_factory=list)
|
input_files: List[str] = field(default_factory=list)
|
||||||
imports: Set[str] = field(default_factory=set)
|
imports_end: Set[str] = field(default_factory=set)
|
||||||
datetime_imports: Set[str] = field(default_factory=set)
|
datetime_imports: Set[str] = field(default_factory=set)
|
||||||
pydantic_imports: Set[str] = field(default_factory=set)
|
pydantic_imports: Set[str] = field(default_factory=set)
|
||||||
builtins_import: bool = False
|
builtins_import: bool = False
|
||||||
@ -532,7 +532,7 @@ class FieldCompiler(MessageCompiler):
|
|||||||
# Type referencing another defined Message or a named enum
|
# Type referencing another defined Message or a named enum
|
||||||
return get_type_reference(
|
return get_type_reference(
|
||||||
package=self.output_file.package,
|
package=self.output_file.package,
|
||||||
imports=self.output_file.imports,
|
imports=self.output_file.imports_end,
|
||||||
source_type=self.proto_obj.type_name,
|
source_type=self.proto_obj.type_name,
|
||||||
typing_compiler=self.typing_compiler,
|
typing_compiler=self.typing_compiler,
|
||||||
pydantic=self.output_file.pydantic_dataclasses,
|
pydantic=self.output_file.pydantic_dataclasses,
|
||||||
@ -730,7 +730,7 @@ class ServiceMethodCompiler(ProtoContentBase):
|
|||||||
"""
|
"""
|
||||||
return get_type_reference(
|
return get_type_reference(
|
||||||
package=self.output_file.package,
|
package=self.output_file.package,
|
||||||
imports=self.output_file.imports,
|
imports=self.output_file.imports_end,
|
||||||
source_type=self.proto_obj.input_type,
|
source_type=self.proto_obj.input_type,
|
||||||
typing_compiler=self.output_file.typing_compiler,
|
typing_compiler=self.output_file.typing_compiler,
|
||||||
unwrap=False,
|
unwrap=False,
|
||||||
@ -760,7 +760,7 @@ class ServiceMethodCompiler(ProtoContentBase):
|
|||||||
"""
|
"""
|
||||||
return get_type_reference(
|
return get_type_reference(
|
||||||
package=self.output_file.package,
|
package=self.output_file.package,
|
||||||
imports=self.output_file.imports,
|
imports=self.output_file.imports_end,
|
||||||
source_type=self.proto_obj.output_type,
|
source_type=self.proto_obj.output_type,
|
||||||
typing_compiler=self.output_file.typing_compiler,
|
typing_compiler=self.output_file.typing_compiler,
|
||||||
unwrap=False,
|
unwrap=False,
|
||||||
|
@ -8,7 +8,6 @@ import {{ i }}
|
|||||||
|
|
||||||
{% if output_file.pydantic_dataclasses %}
|
{% if output_file.pydantic_dataclasses %}
|
||||||
from pydantic.dataclasses import dataclass
|
from pydantic.dataclasses import dataclass
|
||||||
from pydantic.dataclasses import rebuild_dataclass
|
|
||||||
{%- else -%}
|
{%- else -%}
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
{% endif %}
|
{% endif %}
|
||||||
@ -35,10 +34,6 @@ from betterproto.grpc.grpclib_server import ServiceBase
|
|||||||
import grpclib
|
import grpclib
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% for i in output_file.imports|sort %}
|
|
||||||
{{ i }}
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
{% if output_file.imports_type_checking_only %}
|
{% if output_file.imports_type_checking_only %}
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
@ -77,14 +77,14 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
|
|||||||
, {{ method.py_input_message_param }}: "{{ method.py_input_message_type }}"
|
, {{ method.py_input_message_param }}: "{{ method.py_input_message_type }}"
|
||||||
{%- else -%}
|
{%- else -%}
|
||||||
{# Client streaming: need a request iterator instead #}
|
{# Client streaming: need a request iterator instead #}
|
||||||
, {{ 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)) }}
|
, {{ 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)) }}"
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
,
|
,
|
||||||
*
|
*
|
||||||
, timeout: {{ output_file.typing_compiler.optional("float") }} = None
|
, timeout: {{ output_file.typing_compiler.optional("float") }} = None
|
||||||
, deadline: {{ output_file.typing_compiler.optional('"Deadline"') }} = None
|
, deadline: {{ output_file.typing_compiler.optional('"Deadline"') }} = None
|
||||||
, metadata: {{ output_file.typing_compiler.optional('"MetadataLike"') }} = None
|
, metadata: {{ output_file.typing_compiler.optional('"MetadataLike"') }} = None
|
||||||
) -> {% if method.server_streaming %}{{ output_file.typing_compiler.async_iterator(method.py_output_message_type ) }}{% else %}"{{ method.py_output_message_type }}"{% endif %}:
|
) -> "{% if method.server_streaming %}{{ output_file.typing_compiler.async_iterator(method.py_output_message_type ) }}{% else %}{{ method.py_output_message_type }}{% endif %}":
|
||||||
{% if method.comment %}
|
{% if method.comment %}
|
||||||
{{ method.comment }}
|
{{ method.comment }}
|
||||||
|
|
||||||
@ -143,6 +143,10 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
{% for i in output_file.imports_end %}
|
||||||
|
{{ i }}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
{% for service in output_file.services %}
|
{% for service in output_file.services %}
|
||||||
class {{ service.py_name }}Base(ServiceBase):
|
class {{ service.py_name }}Base(ServiceBase):
|
||||||
{% if service.comment %}
|
{% if service.comment %}
|
||||||
@ -211,11 +215,3 @@ class {{ service.py_name }}Base(ServiceBase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
{% if output_file.pydantic_dataclasses %}
|
|
||||||
{% for message in output_file.messages %}
|
|
||||||
{% if message.has_message_field %}
|
|
||||||
rebuild_dataclass({{ message.py_name }}) # type: ignore
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
|
||||||
{% endif %}
|
|
||||||
|
@ -26,5 +26,5 @@ import "other.proto";
|
|||||||
// (root: Test & RootPackageMessage) <-------> (other: OtherPackageMessage)
|
// (root: Test & RootPackageMessage) <-------> (other: OtherPackageMessage)
|
||||||
message Test {
|
message Test {
|
||||||
RootPackageMessage message = 1;
|
RootPackageMessage message = 1;
|
||||||
other.OtherPackageMessage other = 2;
|
other.OtherPackageMessage other_value = 2;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user