Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit ea977be

Browse files
Remove typing compiler (#40)
* Remove typing compiler * Remove typing compiler test * Fix merge conflict * Rename variable
1 parent b99d979 commit ea977be

File tree

8 files changed

+15
-288
lines changed

8 files changed

+15
-288
lines changed

src/betterproto2_compiler/compile/importing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def get_type_reference(
8383
if unwrap:
8484
if source_type in WRAPPER_TYPES:
8585
wrapped_type = type(WRAPPER_TYPES[source_type]().value)
86-
return settings.typing_compiler.optional(wrapped_type.__name__)
86+
return f"{wrapped_type.__name__} | None"
8787

8888
if source_type == ".google.protobuf.Duration":
8989
return "datetime.timedelta"

src/betterproto2_compiler/plugin/models.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
ServiceDescriptorProto,
5858
)
5959
from betterproto2_compiler.lib.google.protobuf.compiler import CodeGeneratorRequest
60-
from betterproto2_compiler.plugin.typing_compiler import TypingCompiler
6160
from betterproto2_compiler.settings import Settings
6261

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

299298
@dataclass(kw_only=True)
300299
class FieldCompiler(ProtoContentBase):
301-
typing_compiler: TypingCompiler
302300
builtins_types: set[str] = field(default_factory=set)
303301

304302
message: MessageCompiler
@@ -413,9 +411,9 @@ def annotation(self) -> str:
413411
if self.use_builtins:
414412
py_type = f"builtins.{py_type}"
415413
if self.repeated:
416-
return self.typing_compiler.list(py_type)
414+
return f"list[{py_type}]"
417415
if self.optional:
418-
return self.typing_compiler.optional(py_type)
416+
return f"{py_type} | None"
419417
return py_type
420418

421419

@@ -449,14 +447,12 @@ def ready(self) -> None:
449447
self.py_k_type = FieldCompiler(
450448
source_file=self.source_file,
451449
proto_obj=nested.field[0], # key
452-
typing_compiler=self.typing_compiler,
453450
path=[],
454451
message=self.message,
455452
).py_type
456453
self.py_v_type = FieldCompiler(
457454
source_file=self.source_file,
458455
proto_obj=nested.field[1], # value
459-
typing_compiler=self.typing_compiler,
460456
path=[],
461457
message=self.message,
462458
).py_type
@@ -482,7 +478,7 @@ def get_field_string(self) -> str:
482478

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

487483
@property
488484
def repeated(self) -> bool:

src/betterproto2_compiler/plugin/parser.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
is_map,
3232
is_oneof,
3333
)
34-
from .typing_compiler import (
35-
DirectImportTypingCompiler,
36-
NoTyping310TypingCompiler,
37-
TypingImportTypingCompiler,
38-
)
3934

4035

4136
def traverse(
@@ -65,25 +60,7 @@ def _traverse(
6560

6661

6762
def get_settings(plugin_options: list[str]) -> Settings:
68-
# Gather any typing generation options.
69-
typing_opts = [opt[len("typing.") :] for opt in plugin_options if opt.startswith("typing.")]
70-
71-
if len(typing_opts) > 1:
72-
raise ValueError("Multiple typing options provided")
73-
74-
# Set the compiler type.
75-
typing_opt = typing_opts[0] if typing_opts else "direct"
76-
if typing_opt == "direct":
77-
typing_compiler = DirectImportTypingCompiler()
78-
elif typing_opt == "root":
79-
typing_compiler = TypingImportTypingCompiler()
80-
elif typing_opt == "310":
81-
typing_compiler = NoTyping310TypingCompiler()
82-
else:
83-
raise ValueError("Invalid typing option provided")
84-
8563
return Settings(
86-
typing_compiler=typing_compiler,
8764
pydantic_dataclasses="pydantic_dataclasses" in plugin_options,
8865
)
8966

@@ -203,7 +180,6 @@ def read_protobuf_type(
203180
message=message_data,
204181
proto_obj=field,
205182
path=path + [2, index],
206-
typing_compiler=output_package.settings.typing_compiler,
207183
)
208184
)
209185
elif is_oneof(field):
@@ -213,7 +189,6 @@ def read_protobuf_type(
213189
message=message_data,
214190
proto_obj=field,
215191
path=path + [2, index],
216-
typing_compiler=output_package.settings.typing_compiler,
217192
)
218193
)
219194
else:
@@ -223,7 +198,6 @@ def read_protobuf_type(
223198
message=message_data,
224199
proto_obj=field,
225200
path=path + [2, index],
226-
typing_compiler=output_package.settings.typing_compiler,
227201
)
228202
)
229203

src/betterproto2_compiler/plugin/typing_compiler.py

Lines changed: 0 additions & 163 deletions
This file was deleted.

src/betterproto2_compiler/settings.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from dataclasses import dataclass
22

3-
from .plugin.typing_compiler import TypingCompiler
4-
53

64
@dataclass
75
class Settings:
86
pydantic_dataclasses: bool
9-
typing_compiler: TypingCompiler

src/betterproto2_compiler/templates/header.py.j2

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ __all__ = (
2121
import builtins
2222
import datetime
2323
import warnings
24+
from collections.abc import AsyncIterable, AsyncIterator, Iterable
25+
from typing import TYPE_CHECKING
2426

2527
{% if output_file.settings.pydantic_dataclasses %}
2628
from pydantic.dataclasses import dataclass
@@ -29,21 +31,12 @@ from pydantic import model_validator
2931
from dataclasses import dataclass
3032
{% endif %}
3133

32-
{% set typing_imports = output_file.settings.typing_compiler.imports() %}
33-
{% if typing_imports %}
34-
{% for line in output_file.settings.typing_compiler.import_lines() %}
35-
{{ line }}
36-
{% endfor %}
37-
{% endif %}
38-
3934
import betterproto2
4035
{% if output_file.services %}
4136
from betterproto2.grpc.grpclib_server import ServiceBase
4237
import grpclib
4338
{% endif %}
4439

45-
from typing import TYPE_CHECKING
46-
4740
{# Import the message pool of the generated code. #}
4841
{% if output_file.package %}
4942
from {{ "." * output_file.package.count(".") }}..message_pool import default_message_pool

src/betterproto2_compiler/templates/template.py.j2

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,20 @@ class {{ service.py_name }}Stub(betterproto2.ServiceStub):
100100
{%- if not method.client_streaming -%}
101101
, message:
102102
{%- if method.is_input_msg_empty -%}
103-
"{{ output_file.settings.typing_compiler.optional(method.py_input_message_type) }}" = None
103+
"{{ method.py_input_message_type }} | None" = None
104104
{%- else -%}
105105
"{{ method.py_input_message_type }}"
106106
{%- endif -%}
107107
{%- else -%}
108108
{# Client streaming: need a request iterator instead #}
109-
, 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)) }}"
109+
, messages: "AsyncIterable[{{ method.py_input_message_type }}] | Iterable[{{ method.py_input_message_type }}]"
110110
{%- endif -%}
111111
,
112112
*
113-
, timeout: {{ output_file.settings.typing_compiler.optional("float") }} = None
114-
, deadline: "{{ output_file.settings.typing_compiler.optional("Deadline") }}" = None
115-
, metadata: "{{ output_file.settings.typing_compiler.optional("MetadataLike") }}" = None
116-
) -> "{% if method.server_streaming %}{{ output_file.settings.typing_compiler.async_iterator(method.py_output_message_type ) }}{% else %}{{ method.py_output_message_type }}{% endif %}":
113+
, timeout: "float | None" = None
114+
, deadline: "Deadline | None" = None
115+
, metadata: "MetadataLike | None" = None
116+
) -> "{% if method.server_streaming %}AsyncIterator[{{ method.py_output_message_type }}]{% else %}{{ method.py_output_message_type }}{% endif %}":
117117
{% if method.comment %}
118118
"""
119119
{{ method.comment | indent(8) }}
@@ -202,9 +202,9 @@ class {{ service.py_name }}Base(ServiceBase):
202202
, message: "{{ method.py_input_message_type }}"
203203
{%- else -%}
204204
{# Client streaming: need a request iterator instead #}
205-
, messages: {{ output_file.settings.typing_compiler.async_iterator(method.py_input_message_type) }}
205+
, messages: "AsyncIterator[{{ method.py_input_message_type }}]"
206206
{%- endif -%}
207-
) -> {% if method.server_streaming %}{{ output_file.settings.typing_compiler.async_iterator(method.py_output_message_type) }}{% else %}"{{ method.py_output_message_type }}"{% endif %}:
207+
) -> {% if method.server_streaming %}"AsyncIterator[{{ method.py_output_message_type }}]"{% else %}"{{ method.py_output_message_type }}"{% endif %}:
208208
{% if method.comment %}
209209
"""
210210
{{ method.comment | indent(8) }}
@@ -235,7 +235,7 @@ class {{ service.py_name }}Base(ServiceBase):
235235

236236
{% endfor %}
237237

238-
def __mapping__(self) -> {{ output_file.settings.typing_compiler.dict("str", "grpclib.const.Handler") }}:
238+
def __mapping__(self) -> "dict[str, grpclib.const.Handler":
239239
return {
240240
{% for method in service.methods %}
241241
"{{ method.route }}": grpclib.const.Handler(

0 commit comments

Comments
 (0)