diff --git a/pyproject.toml b/pyproject.toml index d7302b5..9f13f92 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,6 +49,7 @@ build-backend = "poetry.core.masonry.api" [tool.ruff] +target-version = "py39" include = ["src/**/*.py", "tests/**/*.py"] extend-exclude = ["tests/**/expected_gen/**.py"] force-exclude = true diff --git a/src/pyprotostuben/codegen/mypy/builder.py b/src/pyprotostuben/codegen/mypy/builder.py index d87c5eb..dfed4fc 100644 --- a/src/pyprotostuben/codegen/mypy/builder.py +++ b/src/pyprotostuben/codegen/mypy/builder.py @@ -153,7 +153,7 @@ def __build_protobuf_message_has_field_method_stub(self, scope: ScopeInfo) -> as ) def __build_which_oneof_method_stubs(self, scope: ScopeInfo) -> t.Iterable[ast.stmt]: - oneofs: t.DefaultDict[str, t.List[ast.expr]] = defaultdict(list) + oneofs = defaultdict[str, list[ast.expr]](list) for field in scope.fields: if field.oneof_group is not None: oneofs[field.oneof_group].append(self.__inner.build_const(field.name)) @@ -429,7 +429,7 @@ def __build_stub_method_def(self, info: MethodInfo) -> ast.stmt: doc=info.doc, ) - def __build_servicer_method_inout_refs(self, info: MethodInfo) -> t.Tuple[ast.expr, ast.expr]: + def __build_servicer_method_inout_refs(self, info: MethodInfo) -> tuple[ast.expr, ast.expr]: if not info.server_input_streaming and not info.server_output_streaming: return ( self.__build_message_ref(info.server_input), @@ -457,7 +457,7 @@ def __build_servicer_method_inout_refs(self, info: MethodInfo) -> t.Tuple[ast.ex msg = "invalid method streaming options" raise ValueError(msg, info) - def __build_stub_method_inout_refs(self, info: MethodInfo) -> t.Tuple[ast.expr, ast.expr]: + def __build_stub_method_inout_refs(self, info: MethodInfo) -> tuple[ast.expr, ast.expr]: if not info.server_input_streaming and not info.server_output_streaming: return ( self.__build_message_ref(info.server_input), diff --git a/src/pyprotostuben/logging.py b/src/pyprotostuben/logging.py index 4669ffb..4245550 100644 --- a/src/pyprotostuben/logging.py +++ b/src/pyprotostuben/logging.py @@ -60,16 +60,17 @@ def configure(cls) -> None: def get( # allow callee to pass custom `cls` kwarg __cls, # noqa: N804 - __name: str, + name: str, + /, **kwargs: object, ) -> "Logger": - return __cls(getLogger(__name), kwargs) + return __cls(getLogger(name), kwargs) def process( self, msg: object, kwargs: t.MutableMapping[str, object], - ) -> t.Tuple[object, t.MutableMapping[str, object]]: + ) -> tuple[object, t.MutableMapping[str, object]]: exc_info = kwargs.pop("exc_info", None) return msg, { "exc_info": exc_info, diff --git a/src/pyprotostuben/protobuf/context.py b/src/pyprotostuben/protobuf/context.py index cea5d4c..d794332 100644 --- a/src/pyprotostuben/protobuf/context.py +++ b/src/pyprotostuben/protobuf/context.py @@ -40,9 +40,9 @@ class CodeGeneratorContext: @dataclass() class BuildContext: - files: t.Dict[str, ProtoFile] = field(default_factory=dict) - types: t.Dict[str, t.Union[EnumInfo, MessageInfo]] = field(default_factory=dict) - map_entries: t.Dict[str, MapEntryPlaceholder] = field(default_factory=dict) + files: dict[str, ProtoFile] = field(default_factory=dict) + types: dict[str, t.Union[EnumInfo, MessageInfo]] = field(default_factory=dict) + map_entries: dict[str, MapEntryPlaceholder] = field(default_factory=dict) class ContextBuilder(ProtoVisitor[BuildContext], LoggerMixin): @@ -141,7 +141,7 @@ def __build_type( EnumContext[BuildContext], DescriptorContext[BuildContext], ], - ) -> t.Tuple[str, ModuleInfo, t.Sequence[str]]: + ) -> tuple[str, ModuleInfo, t.Sequence[str]]: ns = [part.proto.name for part in context.parts[1:]] proto_path = ".".join(ns) diff --git a/src/pyprotostuben/protobuf/location.py b/src/pyprotostuben/protobuf/location.py index dbfc2d4..6184c70 100644 --- a/src/pyprotostuben/protobuf/location.py +++ b/src/pyprotostuben/protobuf/location.py @@ -7,7 +7,7 @@ def get_comment_blocks(location: t.Optional[SourceCodeInfo.Location]) -> t.Seque if location is None: return [] - blocks: t.List[str] = [] + blocks: list[str] = [] blocks.extend(comment.strip() for comment in location.leading_detached_comments) if location.HasField("leading_comments"): diff --git a/src/pyprotostuben/python/ast_builder.py b/src/pyprotostuben/python/ast_builder.py index 1e5dde0..ad82181 100644 --- a/src/pyprotostuben/python/ast_builder.py +++ b/src/pyprotostuben/python/ast_builder.py @@ -46,7 +46,7 @@ def get_dependencies(self) -> t.Sequence[ModuleInfo]: class ModuleDependencyResolver(DependencyResolver): def __init__(self, module: ModuleInfo) -> None: self.__module = module - self.__deps: t.Set[ModuleInfo] = set() + self.__deps: set[ModuleInfo] = set() def resolve(self, info: TypeInfo) -> TypeInfo: if info.module == self.__module: @@ -508,7 +508,7 @@ def build_call_stmt( def build_with_stmt( self, *, - items: t.Sequence[t.Tuple[str, TypeRef]], + items: t.Sequence[tuple[str, TypeRef]], body: t.Sequence[ast.stmt], is_async: bool = False, ) -> t.Union[ast.With, ast.AsyncWith]: @@ -660,7 +660,7 @@ def build_module( def _build_decorators( self, *decorators: t.Optional[t.Sequence[t.Union[ast.expr, TypeInfo, None]]], - ) -> t.List[ast.expr]: + ) -> list[ast.expr]: return [ self.build_ref(dec) for block in (decorators or ()) if block for dec in (block or ()) if dec is not None ] @@ -696,14 +696,14 @@ def _build_func_args(self, args: t.Optional[t.Sequence[FuncArgInfo]]) -> ast.arg ], ) - def _build_body(self, doc: t.Optional[str], body: t.Optional[t.Sequence[ast.stmt]]) -> t.List[ast.stmt]: - result: t.List[ast.stmt] = list(body or ()) + def _build_body(self, doc: t.Optional[str], body: t.Optional[t.Sequence[ast.stmt]]) -> list[ast.stmt]: + result: list[ast.stmt] = list(body or ()) if doc: result.insert(0, self.build_docstring(doc)) return result - def _build_stub_body(self, doc: t.Optional[str]) -> t.List[ast.stmt]: + def _build_stub_body(self, doc: t.Optional[str]) -> list[ast.stmt]: return ( [ ast.Expr(value=ast.Constant(value=...)), diff --git a/src/pyprotostuben/python/info.py b/src/pyprotostuben/python/info.py index e1607dc..5a19d3e 100644 --- a/src/pyprotostuben/python/info.py +++ b/src/pyprotostuben/python/info.py @@ -81,7 +81,7 @@ def from_str(cls, ref: str) -> "TypeInfo": return cls(ModuleInfo.from_str(module), ns.split(".")) @classmethod - def from_type(cls, type_: t.Type[object]) -> "TypeInfo": + def from_type(cls, type_: type[object]) -> "TypeInfo": return cls(ModuleInfo.from_str(type_.__module__), type_.__qualname__.split(".")) @classmethod diff --git a/src/pyprotostuben/stack.py b/src/pyprotostuben/stack.py index 28b7142..4a4a365 100644 --- a/src/pyprotostuben/stack.py +++ b/src/pyprotostuben/stack.py @@ -19,7 +19,7 @@ def get_last(self) -> V_co: class MutableStack(Stack[T]): def __init__(self, items: t.Optional[t.Sequence[T]] = None) -> None: - self.__impl: t.Deque[T] = deque(items or []) + self.__impl = deque[T](items or []) def __repr__(self) -> str: return f"{self.__class__.__name__}({self.__impl!r})" diff --git a/tests/stub/plugin.py b/tests/stub/plugin.py index c07b010..1b9c70a 100644 --- a/tests/stub/plugin.py +++ b/tests/stub/plugin.py @@ -11,7 +11,7 @@ class CustomPluginError(Exception): class ProtocPluginStub(ProtocPlugin): def __init__(self, side_effect: t.Optional[Exception], return_value: t.Optional[CodeGeneratorResponse]) -> None: - self.requests: t.List[CodeGeneratorRequest] = [] + self.requests: list[CodeGeneratorRequest] = [] self.__side_effect = side_effect self.__return_value = return_value