Skip to content

Commit a8f4887

Browse files
Add the quotes around types only at the end (#20)
1 parent a31d1be commit a8f4887

File tree

5 files changed

+31
-38
lines changed

5 files changed

+31
-38
lines changed

src/betterproto/compile/importing.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ def reference_absolute(imports: Set[str], py_package: List[str], py_type: str) -
131131
string_import = ".".join(py_package)
132132
string_alias = safe_snake_case(string_import)
133133
imports.add(f"import {string_import} as {string_alias}")
134-
return f'"{string_alias}.{py_type}"'
134+
return f"{string_alias}.{py_type}"
135135

136136

137137
def reference_sibling(py_type: str) -> str:
138138
"""
139139
Returns a reference to a python type within the same package as the current package.
140140
"""
141-
return f'"{py_type}"'
141+
return f"{py_type}"
142142

143143

144144
def reference_descendent(
@@ -155,10 +155,10 @@ def reference_descendent(
155155
if string_from:
156156
string_alias = "_".join(importing_descendent)
157157
imports.add(f"from .{string_from} import {string_import} as {string_alias}")
158-
return f'"{string_alias}.{py_type}"'
158+
return f"{string_alias}.{py_type}"
159159
else:
160160
imports.add(f"from . import {string_import}")
161-
return f'"{string_import}.{py_type}"'
161+
return f"{string_import}.{py_type}"
162162

163163

164164
def reference_ancestor(
@@ -177,11 +177,11 @@ def reference_ancestor(
177177
string_alias = f"_{'_' * distance_up}{string_import}__"
178178
string_from = f"..{'.' * distance_up}"
179179
imports.add(f"from {string_from} import {string_import} as {string_alias}")
180-
return f'"{string_alias}.{py_type}"'
180+
return f"{string_alias}.{py_type}"
181181
else:
182182
string_alias = f"{'_' * distance_up}{py_type}__"
183183
imports.add(f"from .{'.' * distance_up} import {py_type} as {string_alias}")
184-
return f'"{string_alias}"'
184+
return string_alias
185185

186186

187187
def reference_cousin(
@@ -204,4 +204,4 @@ def reference_cousin(
204204
+ "__"
205205
)
206206
imports.add(f"from {string_from} import {string_import} as {string_alias}")
207-
return f'"{string_alias}.{py_type}"'
207+
return f"{string_alias}.{py_type}"

src/betterproto/plugin/models.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ def __post_init__(self) -> None:
405405
def get_field_string(self) -> str:
406406
"""Construct string representation of this field as a field."""
407407
name = f"{self.py_name}"
408-
annotations = f": {self.annotation}"
409408
field_args = ", ".join(
410409
([""] + self.betterproto_field_args) if self.betterproto_field_args else []
411410
)
@@ -414,7 +413,7 @@ def get_field_string(self) -> str:
414413
)
415414
if self.py_name in dir(builtins):
416415
self.parent.builtins_types.add(self.py_name)
417-
return f"{name}{annotations} = {betterproto_field_type}"
416+
return f'{name}: "{self.annotation}" = {betterproto_field_type}'
418417

419418
@property
420419
def betterproto_field_args(self) -> List[str]:
@@ -426,7 +425,7 @@ def betterproto_field_args(self) -> List[str]:
426425
if self.repeated:
427426
args.append("repeated=True")
428427
if self.field_type == "enum":
429-
t = self.py_type.strip('"')
428+
t = self.py_type
430429
args.append(f"enum_default_value=lambda: {t}.try_value(0)")
431430
return args
432431

@@ -702,7 +701,7 @@ def py_input_message_type(self) -> str:
702701
request=self.request,
703702
unwrap=False,
704703
pydantic=self.output_file.pydantic_dataclasses,
705-
).strip('"')
704+
)
706705

707706
@property
708707
def is_input_msg_empty(self: "ServiceMethodCompiler") -> bool:
@@ -741,7 +740,7 @@ def py_output_message_type(self) -> str:
741740
request=self.request,
742741
unwrap=False,
743742
pydantic=self.output_file.pydantic_dataclasses,
744-
).strip('"')
743+
)
745744

746745
@property
747746
def client_streaming(self) -> bool:

src/betterproto/plugin/typing_compiler.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,29 @@ def imports(self) -> Dict[str, Optional[Set[str]]]:
139139
class NoTyping310TypingCompiler(TypingCompiler):
140140
_imports: Dict[str, Set[str]] = field(default_factory=lambda: defaultdict(set))
141141

142-
@staticmethod
143-
def _fmt(type: str) -> str: # for now this is necessary till 3.14
144-
if type.startswith('"'):
145-
return type[1:-1]
146-
return type
147-
148142
def optional(self, type: str) -> str:
149-
return f'"{self._fmt(type)} | None"'
143+
return f"{type} | None"
150144

151145
def list(self, type: str) -> str:
152-
return f'"list[{self._fmt(type)}]"'
146+
return f"list[{type}]"
153147

154148
def dict(self, key: str, value: str) -> str:
155-
return f'"dict[{key}, {self._fmt(value)}]"'
149+
return f"dict[{key}, {value}]"
156150

157151
def union(self, *types: str) -> str:
158-
return f'"{" | ".join(map(self._fmt, types))}"'
152+
return f"{' | '.join(types)}"
159153

160154
def iterable(self, type: str) -> str:
161155
self._imports["collections.abc"].add("Iterable")
162-
return f'"Iterable[{type}]"'
156+
return f"Iterable[{type}]"
163157

164158
def async_iterable(self, type: str) -> str:
165159
self._imports["collections.abc"].add("AsyncIterable")
166-
return f'"AsyncIterable[{type}]"'
160+
return f"AsyncIterable[{type}]"
167161

168162
def async_iterator(self, type: str) -> str:
169163
self._imports["collections.abc"].add("AsyncIterator")
170-
return f'"AsyncIterator[{type}]"'
164+
return f"AsyncIterator[{type}]"
171165

172166
def imports(self) -> Dict[str, Optional[Set[str]]]:
173167
return {k: v if v else None for k, v in self._imports.items()}

src/betterproto/templates/template.py.j2

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
9696
,
9797
*
9898
, timeout: {{ output_file.typing_compiler.optional("float") }} = None
99-
, deadline: {{ output_file.typing_compiler.optional('"Deadline"') }} = None
100-
, metadata: {{ output_file.typing_compiler.optional('"MetadataLike"') }} = None
99+
, deadline: "{{ output_file.typing_compiler.optional("Deadline") }}" = None
100+
, metadata: "{{ output_file.typing_compiler.optional("MetadataLike") }}" = None
101101
) -> "{% if method.server_streaming %}{{ output_file.typing_compiler.async_iterator(method.py_output_message_type ) }}{% else %}{{ method.py_output_message_type }}{% endif %}":
102102
{% if method.comment %}
103103
"""
@@ -115,7 +115,7 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
115115
"{{ method.route }}",
116116
{{ method.py_input_message_param }}_iterator,
117117
{{ method.py_input_message_type }},
118-
{{ method.py_output_message_type.strip('"') }},
118+
{{ method.py_output_message_type }},
119119
timeout=timeout,
120120
deadline=deadline,
121121
metadata=metadata,
@@ -130,7 +130,7 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
130130
async for response in self._unary_stream(
131131
"{{ method.route }}",
132132
{{ method.py_input_message_param }},
133-
{{ method.py_output_message_type.strip('"') }},
133+
{{ method.py_output_message_type }},
134134
timeout=timeout,
135135
deadline=deadline,
136136
metadata=metadata,
@@ -144,7 +144,7 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
144144
"{{ method.route }}",
145145
{{ method.py_input_message_param }}_iterator,
146146
{{ method.py_input_message_type }},
147-
{{ method.py_output_message_type.strip('"') }},
147+
{{ method.py_output_message_type }},
148148
timeout=timeout,
149149
deadline=deadline,
150150
metadata=metadata,
@@ -158,7 +158,7 @@ class {{ service.py_name }}Stub(betterproto.ServiceStub):
158158
return await self._unary_unary(
159159
"{{ method.route }}",
160160
{{ method.py_input_message_param }},
161-
{{ method.py_output_message_type.strip('"') }},
161+
{{ method.py_output_message_type }},
162162
timeout=timeout,
163163
deadline=deadline,
164164
metadata=metadata,

tests/test_typing_compiler.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ def test_typing_import_typing_compiler():
6262
def test_no_typing_311_typing_compiler():
6363
compiler = NoTyping310TypingCompiler()
6464
assert compiler.imports() == {}
65-
assert compiler.optional("str") == '"str | None"'
65+
assert compiler.optional("str") == "str | None"
6666
assert compiler.imports() == {}
67-
assert compiler.list("str") == '"list[str]"'
67+
assert compiler.list("str") == "list[str]"
6868
assert compiler.imports() == {}
69-
assert compiler.dict("str", "int") == '"dict[str, int]"'
69+
assert compiler.dict("str", "int") == "dict[str, int]"
7070
assert compiler.imports() == {}
71-
assert compiler.union("str", "int") == '"str | int"'
71+
assert compiler.union("str", "int") == "str | int"
7272
assert compiler.imports() == {}
73-
assert compiler.iterable("str") == '"Iterable[str]"'
74-
assert compiler.async_iterable("str") == '"AsyncIterable[str]"'
75-
assert compiler.async_iterator("str") == '"AsyncIterator[str]"'
73+
assert compiler.iterable("str") == "Iterable[str]"
74+
assert compiler.async_iterable("str") == "AsyncIterable[str]"
75+
assert compiler.async_iterator("str") == "AsyncIterator[str]"
7676
assert compiler.imports() == {
7777
"collections.abc": {"Iterable", "AsyncIterable", "AsyncIterator"}
7878
}

0 commit comments

Comments
 (0)