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

Commit 3fc8562

Browse files
committed
Fix all rules with safe fix
1 parent f7d9e30 commit 3fc8562

File tree

12 files changed

+81
-105
lines changed

12 files changed

+81
-105
lines changed

src/betterproto2_compiler/casing.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ def safe_snake_case(value: str) -> str:
2222

2323

2424
def snake_case(value: str, strict: bool = True) -> str:
25-
"""
26-
Join words with an underscore into lowercase and remove symbols.
25+
"""Join words with an underscore into lowercase and remove symbols.
2726
2827
Parameters
29-
-----------
28+
----------
3029
value: :class:`str`
3130
The value to convert.
3231
strict: :class:`bool`
3332
Whether or not to force single underscores.
3433
3534
Returns
36-
--------
35+
-------
3736
:class:`str`
3837
The value in snake_case.
38+
3939
"""
4040

4141
def substitute_word(symbols: str, word: str, is_start: bool) -> str:
@@ -61,20 +61,20 @@ def substitute_word(symbols: str, word: str, is_start: bool) -> str:
6161

6262

6363
def pascal_case(value: str, strict: bool = True) -> str:
64-
"""
65-
Capitalize each word and remove symbols.
64+
"""Capitalize each word and remove symbols.
6665
6766
Parameters
68-
-----------
67+
----------
6968
value: :class:`str`
7069
The value to convert.
7170
strict: :class:`bool`
7271
Whether or not to output only alphanumeric characters.
7372
7473
Returns
75-
--------
74+
-------
7675
:class:`str`
7776
The value in PascalCase.
77+
7878
"""
7979

8080
def substitute_word(symbols, word):
@@ -96,27 +96,26 @@ def substitute_word(symbols, word):
9696

9797

9898
def camel_case(value: str, strict: bool = True) -> str:
99-
"""
100-
Capitalize all words except first and remove symbols.
99+
"""Capitalize all words except first and remove symbols.
101100
102101
Parameters
103-
-----------
102+
----------
104103
value: :class:`str`
105104
The value to convert.
106105
strict: :class:`bool`
107106
Whether or not to output only alphanumeric characters.
108107
109108
Returns
110-
--------
109+
-------
111110
:class:`str`
112111
The value in camelCase.
112+
113113
"""
114114
return lowercase_first(pascal_case(value, strict=strict))
115115

116116

117117
def lowercase_first(value: str) -> str:
118-
"""
119-
Lower cases the first character of the value.
118+
"""Lower cases the first character of the value.
120119
121120
Parameters
122121
----------
@@ -127,6 +126,7 @@ def lowercase_first(value: str) -> str:
127126
-------
128127
:class:`str`
129128
The lower cased string.
129+
130130
"""
131131
return value[0:1].lower() + value[1:]
132132

src/betterproto2_compiler/compile/importing.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727

2828

2929
def parse_source_type_name(field_type_name: str, request: PluginRequestCompiler) -> tuple[str, str]:
30-
"""
31-
Split full source type name into package and type name.
30+
"""Split full source type name into package and type name.
3231
E.g. 'root.package.Message' -> ('root.package', 'Message')
3332
'root.Message.SomeEnum' -> ('root', 'Message.SomeEnum')
3433
@@ -76,8 +75,7 @@ def get_type_reference(
7675
unwrap: bool = True,
7776
pydantic: bool = False,
7877
) -> str:
79-
"""
80-
Return a Python type name for a proto type reference. Adds the import if
78+
"""Return a Python type name for a proto type reference. Adds the import if
8179
necessary. Unwraps well known type if required.
8280
"""
8381
if unwrap:
@@ -88,7 +86,7 @@ def get_type_reference(
8886
if source_type == ".google.protobuf.Duration":
8987
return "datetime.timedelta"
9088

91-
elif source_type == ".google.protobuf.Timestamp":
89+
if source_type == ".google.protobuf.Timestamp":
9290
return "datetime.datetime"
9391

9492
source_package, source_type = parse_source_type_name(source_type, request)
@@ -118,25 +116,20 @@ def get_type_reference(
118116

119117

120118
def reference_absolute(imports: set[str], py_package: list[str], py_type: str) -> str:
121-
"""
122-
Returns a reference to a python type located in the root, i.e. sys.path.
123-
"""
119+
"""Returns a reference to a python type located in the root, i.e. sys.path."""
124120
string_import = ".".join(py_package)
125121
string_alias = safe_snake_case(string_import)
126122
imports.add(f"import {string_import} as {string_alias}")
127123
return f"{string_alias}.{py_type}"
128124

129125

130126
def reference_sibling(py_type: str) -> str:
131-
"""
132-
Returns a reference to a python type within the same package as the current package.
133-
"""
127+
"""Returns a reference to a python type within the same package as the current package."""
134128
return f"{py_type}"
135129

136130

137131
def reference_descendent(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
138-
"""
139-
Returns a reference to a python type in a package that is a descendent of the
132+
"""Returns a reference to a python type in a package that is a descendent of the
140133
current package, and adds the required import that is aliased to avoid name
141134
conflicts.
142135
"""
@@ -147,14 +140,12 @@ def reference_descendent(current_package: list[str], imports: set[str], py_packa
147140
string_alias = "_".join(importing_descendent)
148141
imports.add(f"from .{string_from} import {string_import} as {string_alias}")
149142
return f"{string_alias}.{py_type}"
150-
else:
151-
imports.add(f"from . import {string_import}")
152-
return f"{string_import}.{py_type}"
143+
imports.add(f"from . import {string_import}")
144+
return f"{string_import}.{py_type}"
153145

154146

155147
def reference_ancestor(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
156-
"""
157-
Returns a reference to a python type in a package which is an ancestor to the
148+
"""Returns a reference to a python type in a package which is an ancestor to the
158149
current package, and adds the required import that is aliased (if possible) to avoid
159150
name conflicts.
160151
@@ -167,15 +158,13 @@ def reference_ancestor(current_package: list[str], imports: set[str], py_package
167158
string_from = f"..{'.' * distance_up}"
168159
imports.add(f"from {string_from} import {string_import} as {string_alias}")
169160
return f"{string_alias}.{py_type}"
170-
else:
171-
string_alias = f"{'_' * distance_up}{py_type}__"
172-
imports.add(f"from .{'.' * distance_up} import {py_type} as {string_alias}")
173-
return string_alias
161+
string_alias = f"{'_' * distance_up}{py_type}__"
162+
imports.add(f"from .{'.' * distance_up} import {py_type} as {string_alias}")
163+
return string_alias
174164

175165

176166
def reference_cousin(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
177-
"""
178-
Returns a reference to a python type in a package that is not descendent, ancestor
167+
"""Returns a reference to a python type in a package that is not descendent, ancestor
179168
or sibling, and adds the required import that is aliased to avoid name conflicts.
180169
"""
181170
shared_ancestry = os.path.commonprefix([current_package, py_package]) # type: ignore

src/betterproto2_compiler/enum.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from collections.abc import Iterator
34
from enum import (
45
EnumMeta,
56
IntEnum,
@@ -37,7 +38,7 @@ def __new__(mcs, name: str, bases: tuple[type, ...], namespace: dict[str, Any])
3738
new_mcs = type(
3839
f"{name}Type",
3940
tuple(
40-
dict.fromkeys([base.__class__ for base in bases if base.__class__ is not type] + [EnumType, type])
41+
dict.fromkeys([base.__class__ for base in bases if base.__class__ is not type] + [EnumType, type]),
4142
), # reorder the bases so EnumType and type are last to avoid conflicts
4243
{"_value_map_": value_map, "_member_map_": member_map},
4344
)
@@ -73,7 +74,7 @@ def __call__(cls, value: int) -> Enum:
7374
except (KeyError, TypeError):
7475
raise ValueError(f"{value!r} is not a valid {cls.__name__}") from None
7576

76-
def __iter__(cls) -> Generator[Enum, None, None]:
77+
def __iter__(cls) -> Iterator[Enum]:
7778
yield from cls._member_map_.values()
7879

7980
def __reversed__(cls) -> Generator[Enum, None, None]:
@@ -103,8 +104,7 @@ def __contains__(cls, member: object) -> bool:
103104

104105

105106
class Enum(IntEnum if TYPE_CHECKING else int, metaclass=EnumType):
106-
"""
107-
The base class for protobuf enumerations, all generated enumerations will
107+
"""The base class for protobuf enumerations, all generated enumerations will
108108
inherit from this. Emulates `enum.IntEnum`.
109109
"""
110110

@@ -142,7 +142,7 @@ def try_value(cls, value: int = 0) -> Self:
142142
"""Return the value which corresponds to the value.
143143
144144
Parameters
145-
-----------
145+
----------
146146
value: :class:`int`
147147
The value of the enum member to get.
148148
@@ -151,6 +151,7 @@ def try_value(cls, value: int = 0) -> Self:
151151
:class:`Enum`
152152
The corresponding member or a new instance of the enum if
153153
``value`` isn't actually a member.
154+
154155
"""
155156
try:
156157
return cls._value_map_[value]
@@ -162,14 +163,15 @@ def from_string(cls, name: str) -> Self:
162163
"""Return the value which corresponds to the string name.
163164
164165
Parameters
165-
-----------
166+
----------
166167
name: :class:`str`
167168
The name of the enum member to get.
168169
169170
Raises
170-
-------
171+
------
171172
:exc:`ValueError`
172173
The member was not found in the Enum.
174+
173175
"""
174176
try:
175177
return cls._member_map_[name]

src/betterproto2_compiler/plugin/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"Please ensure that you've installed betterproto as "
1515
'`pip install "betterproto[compiler]"` so that compiler dependencies '
1616
"are included."
17-
"\033[0m"
17+
"\033[0m",
1818
)
1919
raise SystemExit(1)
2020

src/betterproto2_compiler/plugin/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ def main() -> None:
3838

3939

4040
def dump_request(dump_file: str, request: CodeGeneratorRequest) -> None:
41-
"""
42-
For developers: Supports running plugin.py standalone so its possible to debug it.
41+
"""For developers: Supports running plugin.py standalone so its possible to debug it.
4342
Run protoc (or generate.py) with BETTERPROTO_DUMP="yourfile.bin" to write the request to a file.
4443
Then run plugin.py from your IDE in debugging mode, and redirect stdin to the file.
4544
"""

src/betterproto2_compiler/plugin/models.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,7 @@ def __post_init__(self) -> None:
188188
raise ValueError(f"`{field_name}` is a required field.")
189189

190190
def ready(self) -> None:
191-
"""
192-
This function is called after all the compilers are created, but before generating the output code.
193-
"""
194-
pass
191+
"""This function is called after all the compilers are created, but before generating the output code."""
195192

196193
@property
197194
def output_file(self) -> "OutputTemplate":
@@ -229,6 +226,7 @@ def all_messages(self) -> list["MessageCompiler"]:
229226
-------
230227
List[MessageCompiler]
231228
List of all of the messages in this request.
229+
232230
"""
233231
return [msg for output in self.output_packages.values() for msg in output.messages.values()]
234232

@@ -261,6 +259,7 @@ def package(self) -> str:
261259
-------
262260
str
263261
Name of input package.
262+
264263
"""
265264
return self.package_proto_obj.package
266265

@@ -272,6 +271,7 @@ def input_filenames(self) -> Iterable[str]:
272271
-------
273272
Iterable[str]
274273
Names of the input files used to build this output.
274+
275275
"""
276276
return sorted(f.name for f in self.input_files)
277277

@@ -345,8 +345,7 @@ def is_map(proto_field_obj: FieldDescriptorProto, parent_message: DescriptorProt
345345

346346

347347
def is_oneof(proto_field_obj: FieldDescriptorProto) -> bool:
348-
"""
349-
True if proto_field_obj is a OneOf, otherwise False.
348+
"""True if proto_field_obj is a OneOf, otherwise False.
350349
351350
.. warning::
352351
TODO update comment
@@ -358,7 +357,6 @@ def is_oneof(proto_field_obj: FieldDescriptorProto) -> bool:
358357
essentially making oneof_index the sole member of a one_of group, which allows
359358
us to tell whether it was set, via the which_one_of interface.
360359
"""
361-
362360
return not proto_field_obj.proto3_optional and proto_field_obj.oneof_index is not None
363361

364362

@@ -420,7 +418,8 @@ def field_wraps(self) -> str | None:
420418
@property
421419
def repeated(self) -> bool:
422420
return self.proto_obj.label == FieldDescriptorProtoLabel.LABEL_REPEATED and not is_map(
423-
self.proto_obj, self.parent
421+
self.proto_obj,
422+
self.parent,
424423
)
425424

426425
@property
@@ -452,15 +451,15 @@ def py_type(self) -> str:
452451
"""String representation of Python type."""
453452
if self.proto_obj.type in PROTO_FLOAT_TYPES:
454453
return "float"
455-
elif self.proto_obj.type in PROTO_INT_TYPES:
454+
if self.proto_obj.type in PROTO_INT_TYPES:
456455
return "int"
457-
elif self.proto_obj.type in PROTO_BOOL_TYPES:
456+
if self.proto_obj.type in PROTO_BOOL_TYPES:
458457
return "bool"
459-
elif self.proto_obj.type in PROTO_STR_TYPES:
458+
if self.proto_obj.type in PROTO_STR_TYPES:
460459
return "str"
461-
elif self.proto_obj.type in PROTO_BYTES_TYPES:
460+
if self.proto_obj.type in PROTO_BYTES_TYPES:
462461
return "bytes"
463-
elif self.proto_obj.type in PROTO_MESSAGE_TYPES:
462+
if self.proto_obj.type in PROTO_MESSAGE_TYPES:
464463
# Type referencing another defined Message or a named enum
465464
return get_type_reference(
466465
package=self.output_file.package,
@@ -470,8 +469,7 @@ def py_type(self) -> str:
470469
request=self.request,
471470
pydantic=self.output_file.pydantic_dataclasses,
472471
)
473-
else:
474-
raise NotImplementedError(f"Unknown type {self.proto_obj.type}")
472+
raise NotImplementedError(f"Unknown type {self.proto_obj.type}")
475473

476474
@property
477475
def annotation(self) -> str:
@@ -643,6 +641,7 @@ def py_input_message_type(self) -> str:
643641
-------
644642
str
645643
String representation of the Python type corresponding to the input message.
644+
646645
"""
647646
return get_type_reference(
648647
package=self.output_file.package,
@@ -670,6 +669,7 @@ def py_input_message_param(self) -> str:
670669
-------
671670
str
672671
Param name corresponding to py_input_message_type.
672+
673673
"""
674674
return pythonize_field_name(self.py_input_message_type)
675675

@@ -682,6 +682,7 @@ def py_output_message_type(self) -> str:
682682
-------
683683
str
684684
String representation of the Python type corresponding to the output message.
685+
685686
"""
686687
return get_type_reference(
687688
package=self.output_file.package,

0 commit comments

Comments
 (0)