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

Commit ad3d3d8

Browse files
Add more linting rules and remove useless files (#12)
* Use Python 3.10 unions * Remove useless files for the compiler * Add more rules * Fix the poe check command * Fix trailing commas
1 parent b96e38a commit ad3d3d8

File tree

19 files changed

+94
-527
lines changed

19 files changed

+94
-527
lines changed

pyproject.toml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protobuf = "^4"
3737
protoc-gen-python_betterproto2 = "betterproto2_compiler.plugin:main"
3838

3939
[tool.ruff]
40-
extend-exclude = ["tests/output_*"]
40+
extend-exclude = ["tests/output_*", "src/betterproto2_compiler/lib"]
4141
target-version = "py310"
4242
line-length = 120
4343

@@ -52,7 +52,11 @@ select = [
5252
"SIM102", # Simplify return or yield statements
5353
"SIM103", # Simplify list/set/dict comprehensions
5454

55+
"UP",
56+
5557
"I",
58+
59+
"COM812", # Trailing commas
5660
]
5761

5862

@@ -78,8 +82,8 @@ sequence = ["_format", "_sort-imports"]
7882
help = "Format the source code, and sort the imports"
7983

8084
[tool.poe.tasks.check]
81-
sequence = ["_check-format", "_check-imports"]
82-
help = "Check that the source code is formatted and the imports sorted"
85+
sequence = ["_check-format", "_check-ruff-lint"]
86+
help = "Check that the source code is formatted and the code passes the linter"
8387

8488
[tool.poe.tasks._format]
8589
cmd = "ruff format src tests"
@@ -93,9 +97,9 @@ help = "Sort the imports"
9397
cmd = "ruff format --diff src tests"
9498
help = "Check that the source code is formatted"
9599

96-
[tool.poe.tasks._check-imports]
97-
cmd = "ruff check --select I src tests"
98-
help = "Check that the imports are sorted"
100+
[tool.poe.tasks._check-ruff-lint]
101+
cmd = "ruff check src tests"
102+
help = "Check the code with the Ruff linter"
99103

100104
[tool.poe.tasks.generate_lib]
101105
cmd = """

src/betterproto2_compiler/compile/importing.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
import os
44
from typing import (
55
TYPE_CHECKING,
6-
Dict,
7-
List,
8-
Set,
9-
Tuple,
10-
Type,
116
)
127

138
from ..casing import safe_snake_case
@@ -18,7 +13,7 @@
1813
from ..plugin.models import PluginRequestCompiler
1914
from ..plugin.typing_compiler import TypingCompiler
2015

21-
WRAPPER_TYPES: Dict[str, Type] = {
16+
WRAPPER_TYPES: dict[str, type] = {
2217
".google.protobuf.DoubleValue": google_protobuf.DoubleValue,
2318
".google.protobuf.FloatValue": google_protobuf.FloatValue,
2419
".google.protobuf.Int32Value": google_protobuf.Int32Value,
@@ -31,7 +26,7 @@
3126
}
3227

3328

34-
def parse_source_type_name(field_type_name: str, request: "PluginRequestCompiler") -> Tuple[str, str]:
29+
def parse_source_type_name(field_type_name: str, request: PluginRequestCompiler) -> tuple[str, str]:
3530
"""
3631
Split full source type name into package and type name.
3732
E.g. 'root.package.Message' -> ('root.package', 'Message')
@@ -77,7 +72,7 @@ def get_type_reference(
7772
imports: set,
7873
source_type: str,
7974
typing_compiler: TypingCompiler,
80-
request: "PluginRequestCompiler",
75+
request: PluginRequestCompiler,
8176
unwrap: bool = True,
8277
pydantic: bool = False,
8378
) -> str:
@@ -98,8 +93,8 @@ def get_type_reference(
9893

9994
source_package, source_type = parse_source_type_name(source_type, request)
10095

101-
current_package: List[str] = package.split(".") if package else []
102-
py_package: List[str] = source_package.split(".") if source_package else []
96+
current_package: list[str] = package.split(".") if package else []
97+
py_package: list[str] = source_package.split(".") if source_package else []
10398
py_type: str = pythonize_class_name(source_type)
10499

105100
compiling_google_protobuf = current_package == ["google", "protobuf"]
@@ -122,7 +117,7 @@ def get_type_reference(
122117
return reference_cousin(current_package, imports, py_package, py_type)
123118

124119

125-
def reference_absolute(imports: Set[str], py_package: List[str], py_type: str) -> str:
120+
def reference_absolute(imports: set[str], py_package: list[str], py_type: str) -> str:
126121
"""
127122
Returns a reference to a python type located in the root, i.e. sys.path.
128123
"""
@@ -139,7 +134,7 @@ def reference_sibling(py_type: str) -> str:
139134
return f"{py_type}"
140135

141136

142-
def reference_descendent(current_package: List[str], imports: Set[str], py_package: List[str], py_type: str) -> str:
137+
def reference_descendent(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
143138
"""
144139
Returns a reference to a python type in a package that is a descendent of the
145140
current package, and adds the required import that is aliased to avoid name
@@ -157,7 +152,7 @@ def reference_descendent(current_package: List[str], imports: Set[str], py_packa
157152
return f"{string_import}.{py_type}"
158153

159154

160-
def reference_ancestor(current_package: List[str], imports: Set[str], py_package: List[str], py_type: str) -> str:
155+
def reference_ancestor(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
161156
"""
162157
Returns a reference to a python type in a package which is an ancestor to the
163158
current package, and adds the required import that is aliased (if possible) to avoid
@@ -178,7 +173,7 @@ def reference_ancestor(current_package: List[str], imports: Set[str], py_package
178173
return string_alias
179174

180175

181-
def reference_cousin(current_package: List[str], imports: Set[str], py_package: List[str], py_type: str) -> str:
176+
def reference_cousin(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
182177
"""
183178
Returns a reference to a python type in a package that is not descendent, ancestor
184179
or sibling, and adds the required import that is aliased to avoid name conflicts.

src/betterproto2_compiler/enum.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
from typing import (
99
TYPE_CHECKING,
1010
Any,
11-
Dict,
12-
Optional,
13-
Tuple,
1411
)
1512

1613
if TYPE_CHECKING:
@@ -33,14 +30,14 @@ class EnumType(EnumMeta if TYPE_CHECKING else type):
3330
_value_map_: Mapping[int, Enum]
3431
_member_map_: Mapping[str, Enum]
3532

36-
def __new__(mcs, name: str, bases: Tuple[type, ...], namespace: Dict[str, Any]) -> Self:
33+
def __new__(mcs, name: str, bases: tuple[type, ...], namespace: dict[str, Any]) -> Self:
3734
value_map = {}
3835
member_map = {}
3936

4037
new_mcs = type(
4138
f"{name}Type",
4239
tuple(
43-
dict.fromkeys([base.__class__ for base in bases if base.__class__ is not type] + [EnumType, type])
40+
dict.fromkeys([base.__class__ for base in bases if base.__class__ is not type] + [EnumType, type]),
4441
), # reorder the bases so EnumType and type are last to avoid conflicts
4542
{"_value_map_": value_map, "_member_map_": member_map},
4643
)
@@ -111,12 +108,12 @@ class Enum(IntEnum if TYPE_CHECKING else int, metaclass=EnumType):
111108
inherit from this. Emulates `enum.IntEnum`.
112109
"""
113110

114-
name: Optional[str]
111+
name: str | None
115112
value: int
116113

117114
if not TYPE_CHECKING:
118115

119-
def __new__(cls, *, name: Optional[str], value: int) -> Self:
116+
def __new__(cls, *, name: str | None, value: int) -> Self:
120117
self = super().__new__(cls, value)
121118
super().__setattr__(self, "name", name)
122119
super().__setattr__(self, "value", value)

src/betterproto2_compiler/grpc/__init__.py

Whitespace-only changes.

src/betterproto2_compiler/grpc/grpclib_client.py

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

src/betterproto2_compiler/grpc/grpclib_server.py

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

src/betterproto2_compiler/grpc/util/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)