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
16 changes: 10 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protobuf = "^4"
protoc-gen-python_betterproto2 = "betterproto2_compiler.plugin:main"

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

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

"UP",

"I",

"COM812", # Trailing commas
]


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

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

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

[tool.poe.tasks._check-imports]
cmd = "ruff check --select I src tests"
help = "Check that the imports are sorted"
[tool.poe.tasks._check-ruff-lint]
cmd = "ruff check src tests"
help = "Check the code with the Ruff linter"

[tool.poe.tasks.generate_lib]
cmd = """
Expand Down
23 changes: 9 additions & 14 deletions src/betterproto2_compiler/compile/importing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import os
from typing import (
TYPE_CHECKING,
Dict,
List,
Set,
Tuple,
Type,
)

from ..casing import safe_snake_case
Expand All @@ -18,7 +13,7 @@
from ..plugin.models import PluginRequestCompiler
from ..plugin.typing_compiler import TypingCompiler

WRAPPER_TYPES: Dict[str, Type] = {
WRAPPER_TYPES: dict[str, type] = {
".google.protobuf.DoubleValue": google_protobuf.DoubleValue,
".google.protobuf.FloatValue": google_protobuf.FloatValue,
".google.protobuf.Int32Value": google_protobuf.Int32Value,
Expand All @@ -31,7 +26,7 @@
}


def parse_source_type_name(field_type_name: str, request: "PluginRequestCompiler") -> Tuple[str, str]:
def parse_source_type_name(field_type_name: str, request: PluginRequestCompiler) -> tuple[str, str]:
"""
Split full source type name into package and type name.
E.g. 'root.package.Message' -> ('root.package', 'Message')
Expand Down Expand Up @@ -77,7 +72,7 @@ def get_type_reference(
imports: set,
source_type: str,
typing_compiler: TypingCompiler,
request: "PluginRequestCompiler",
request: PluginRequestCompiler,
unwrap: bool = True,
pydantic: bool = False,
) -> str:
Expand All @@ -98,8 +93,8 @@ def get_type_reference(

source_package, source_type = parse_source_type_name(source_type, request)

current_package: List[str] = package.split(".") if package else []
py_package: List[str] = source_package.split(".") if source_package else []
current_package: list[str] = package.split(".") if package else []
py_package: list[str] = source_package.split(".") if source_package else []
py_type: str = pythonize_class_name(source_type)

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


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


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


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


def reference_cousin(current_package: List[str], imports: Set[str], py_package: List[str], py_type: str) -> str:
def reference_cousin(current_package: list[str], imports: set[str], py_package: list[str], py_type: str) -> str:
"""
Returns a reference to a python type in a package that is not descendent, ancestor
or sibling, and adds the required import that is aliased to avoid name conflicts.
Expand Down
11 changes: 4 additions & 7 deletions src/betterproto2_compiler/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
from typing import (
TYPE_CHECKING,
Any,
Dict,
Optional,
Tuple,
)

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

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

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

name: Optional[str]
name: str | None
value: int

if not TYPE_CHECKING:

def __new__(cls, *, name: Optional[str], value: int) -> Self:
def __new__(cls, *, name: str | None, value: int) -> Self:
self = super().__new__(cls, value)
super().__setattr__(self, "name", name)
super().__setattr__(self, "value", value)
Expand Down
Empty file.
172 changes: 0 additions & 172 deletions src/betterproto2_compiler/grpc/grpclib_client.py

This file was deleted.

32 changes: 0 additions & 32 deletions src/betterproto2_compiler/grpc/grpclib_server.py

This file was deleted.

Empty file.
Loading
Loading