Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.
Merged
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
2 changes: 1 addition & 1 deletion src/betterproto2_compiler/compile/importing.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def get_type_reference(
if unwrap:
if source_type in WRAPPER_TYPES:
wrapped_type = type(WRAPPER_TYPES[source_type]().value)
return settings.typing_compiler.optional(wrapped_type.__name__)
return f"{wrapped_type.__name__} | None"

if source_type == ".google.protobuf.Duration":
return "datetime.timedelta"
Expand Down
10 changes: 3 additions & 7 deletions src/betterproto2_compiler/plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
ServiceDescriptorProto,
)
from betterproto2_compiler.lib.google.protobuf.compiler import CodeGeneratorRequest
from betterproto2_compiler.plugin.typing_compiler import TypingCompiler
from betterproto2_compiler.settings import Settings

# Organize proto types into categories
Expand Down Expand Up @@ -298,7 +297,6 @@ def is_oneof(proto_field_obj: FieldDescriptorProto) -> bool:

@dataclass(kw_only=True)
class FieldCompiler(ProtoContentBase):
typing_compiler: TypingCompiler
builtins_types: set[str] = field(default_factory=set)

message: MessageCompiler
Expand Down Expand Up @@ -413,9 +411,9 @@ def annotation(self) -> str:
if self.use_builtins:
py_type = f"builtins.{py_type}"
if self.repeated:
return self.typing_compiler.list(py_type)
return f"list[{py_type}]"
if self.optional:
return self.typing_compiler.optional(py_type)
return f"{py_type} | None"
return py_type


Expand Down Expand Up @@ -449,14 +447,12 @@ def ready(self) -> None:
self.py_k_type = FieldCompiler(
source_file=self.source_file,
proto_obj=nested.field[0], # key
typing_compiler=self.typing_compiler,
path=[],
message=self.message,
).py_type
self.py_v_type = FieldCompiler(
source_file=self.source_file,
proto_obj=nested.field[1], # value
typing_compiler=self.typing_compiler,
path=[],
message=self.message,
).py_type
Expand All @@ -482,7 +478,7 @@ def get_field_string(self) -> str:

@property
def annotation(self) -> str:
return self.typing_compiler.dict(self.py_k_type, self.py_v_type)
return f"dict[{self.py_k_type}, {self.py_v_type}]"

@property
def repeated(self) -> bool:
Expand Down
26 changes: 0 additions & 26 deletions src/betterproto2_compiler/plugin/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
is_map,
is_oneof,
)
from .typing_compiler import (
DirectImportTypingCompiler,
NoTyping310TypingCompiler,
TypingImportTypingCompiler,
)


def traverse(
Expand Down Expand Up @@ -65,25 +60,7 @@ def _traverse(


def get_settings(plugin_options: list[str]) -> Settings:
# Gather any typing generation options.
typing_opts = [opt[len("typing.") :] for opt in plugin_options if opt.startswith("typing.")]

if len(typing_opts) > 1:
raise ValueError("Multiple typing options provided")

# Set the compiler type.
typing_opt = typing_opts[0] if typing_opts else "direct"
if typing_opt == "direct":
typing_compiler = DirectImportTypingCompiler()
elif typing_opt == "root":
typing_compiler = TypingImportTypingCompiler()
elif typing_opt == "310":
typing_compiler = NoTyping310TypingCompiler()
else:
raise ValueError("Invalid typing option provided")

return Settings(
typing_compiler=typing_compiler,
pydantic_dataclasses="pydantic_dataclasses" in plugin_options,
)

Expand Down Expand Up @@ -203,7 +180,6 @@ def read_protobuf_type(
message=message_data,
proto_obj=field,
path=path + [2, index],
typing_compiler=output_package.settings.typing_compiler,
)
)
elif is_oneof(field):
Expand All @@ -213,7 +189,6 @@ def read_protobuf_type(
message=message_data,
proto_obj=field,
path=path + [2, index],
typing_compiler=output_package.settings.typing_compiler,
)
)
else:
Expand All @@ -223,7 +198,6 @@ def read_protobuf_type(
message=message_data,
proto_obj=field,
path=path + [2, index],
typing_compiler=output_package.settings.typing_compiler,
)
)

Expand Down
163 changes: 0 additions & 163 deletions src/betterproto2_compiler/plugin/typing_compiler.py

This file was deleted.

3 changes: 0 additions & 3 deletions src/betterproto2_compiler/settings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from dataclasses import dataclass

from .plugin.typing_compiler import TypingCompiler


@dataclass
class Settings:
pydantic_dataclasses: bool
typing_compiler: TypingCompiler
11 changes: 2 additions & 9 deletions src/betterproto2_compiler/templates/header.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ __all__ = (
import builtins
import datetime
import warnings
from collections.abc import AsyncIterable, AsyncIterator, Iterable
from typing import TYPE_CHECKING

{% if output_file.settings.pydantic_dataclasses %}
from pydantic.dataclasses import dataclass
Expand All @@ -29,21 +31,12 @@ from pydantic import model_validator
from dataclasses import dataclass
{% endif %}

{% set typing_imports = output_file.settings.typing_compiler.imports() %}
{% if typing_imports %}
{% for line in output_file.settings.typing_compiler.import_lines() %}
{{ line }}
{% endfor %}
{% endif %}

import betterproto2
{% if output_file.services %}
from betterproto2.grpc.grpclib_server import ServiceBase
import grpclib
{% endif %}

from typing import TYPE_CHECKING

{# Import the message pool of the generated code. #}
{% if output_file.package %}
from {{ "." * output_file.package.count(".") }}..message_pool import default_message_pool
Expand Down
18 changes: 9 additions & 9 deletions src/betterproto2_compiler/templates/template.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ class {{ service.py_name }}Stub(betterproto2.ServiceStub):
{%- if not method.client_streaming -%}
, message:
{%- if method.is_input_msg_empty -%}
"{{ output_file.settings.typing_compiler.optional(method.py_input_message_type) }}" = None
"{{ method.py_input_message_type }} | None" = None
{%- else -%}
"{{ method.py_input_message_type }}"
{%- endif -%}
{%- else -%}
{# Client streaming: need a request iterator instead #}
, messages: "{{ output_file.settings.typing_compiler.union(output_file.settings.typing_compiler.async_iterable(method.py_input_message_type), output_file.settings.typing_compiler.iterable(method.py_input_message_type)) }}"
, messages: "AsyncIterable[{{ method.py_input_message_type }}] | Iterable[{{ method.py_input_message_type }}]"
{%- endif -%}
,
*
, timeout: {{ output_file.settings.typing_compiler.optional("float") }} = None
, deadline: "{{ output_file.settings.typing_compiler.optional("Deadline") }}" = None
, metadata: "{{ output_file.settings.typing_compiler.optional("MetadataLike") }}" = None
) -> "{% if method.server_streaming %}{{ output_file.settings.typing_compiler.async_iterator(method.py_output_message_type ) }}{% else %}{{ method.py_output_message_type }}{% endif %}":
, timeout: "float | None" = None
, deadline: "Deadline | None" = None
, metadata: "MetadataLike | None" = None
) -> "{% if method.server_streaming %}AsyncIterator[{{ method.py_output_message_type }}]{% else %}{{ method.py_output_message_type }}{% endif %}":
{% if method.comment %}
"""
{{ method.comment | indent(8) }}
Expand Down Expand Up @@ -202,9 +202,9 @@ class {{ service.py_name }}Base(ServiceBase):
, message: "{{ method.py_input_message_type }}"
{%- else -%}
{# Client streaming: need a request iterator instead #}
, messages: {{ output_file.settings.typing_compiler.async_iterator(method.py_input_message_type) }}
, messages: "AsyncIterator[{{ method.py_input_message_type }}]"
{%- endif -%}
) -> {% if method.server_streaming %}{{ output_file.settings.typing_compiler.async_iterator(method.py_output_message_type) }}{% else %}"{{ method.py_output_message_type }}"{% endif %}:
) -> {% if method.server_streaming %}"AsyncIterator[{{ method.py_output_message_type }}]"{% else %}"{{ method.py_output_message_type }}"{% endif %}:
{% if method.comment %}
"""
{{ method.comment | indent(8) }}
Expand Down Expand Up @@ -235,7 +235,7 @@ class {{ service.py_name }}Base(ServiceBase):

{% endfor %}

def __mapping__(self) -> {{ output_file.settings.typing_compiler.dict("str", "grpclib.const.Handler") }}:
def __mapping__(self) -> "dict[str, grpclib.const.Handler":
return {
{% for method in service.methods %}
"{{ method.route }}": grpclib.const.Handler(
Expand Down
Loading
Loading