diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 707cef7..4093714 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,10 @@ default_language_version: - python: python3 + python: python3.9 repos: +- repo: https://github.com/asottile/pyupgrade + rev: v3.18.0 + hooks: + - id: pyupgrade - repo: https://github.com/ambv/black rev: 22.6.0 hooks: diff --git a/example/app/server.py b/example/app/server.py index 30b3d3a..3128636 100644 --- a/example/app/server.py +++ b/example/app/server.py @@ -10,7 +10,7 @@ todos: Dict[int, "TodoItem"] = {} -class Dispatcher(object): +class Dispatcher: def create(self, text: str, type: int) -> int: todo_id = max(todos.keys() or [0]) + 1 created = datetime.datetime.now() diff --git a/src/thriftpyi/entities.py b/src/thriftpyi/entities.py index 595dbcb..52e236f 100644 --- a/src/thriftpyi/entities.py +++ b/src/thriftpyi/entities.py @@ -2,7 +2,7 @@ import ast from dataclasses import dataclass, field -from typing import TYPE_CHECKING, List, Optional, Sequence, Type, Union +from typing import TYPE_CHECKING, Sequence, Type, Union if TYPE_CHECKING: AnyFunctionDef = Union[ast.AsyncFunctionDef, ast.FunctionDef] @@ -15,8 +15,8 @@ @dataclass class ModuleItem: name: str - fields: List[Field] = field(default_factory=list) - methods: List[Method] = field(default_factory=list) + fields: list[Field] = field(default_factory=list) + methods: list[Method] = field(default_factory=list) def with_options( self, @@ -53,7 +53,7 @@ def as_ast( bases = bases or [] decorators = decorators or [] - body: List[ast.stmt] = [] + body: list[ast.stmt] = [] body.extend(entry.as_ast() for entry in self.fields) body.extend(entry.as_ast() for entry in self.methods) @@ -74,8 +74,8 @@ def as_ast( @dataclass class Method: name: str - args: List[Field] = field(default_factory=list) - returns: List[Field] = field(default_factory=list) + args: list[Field] = field(default_factory=list) + returns: list[Field] = field(default_factory=list) is_async: bool = False def with_options( @@ -139,7 +139,7 @@ def as_ast(self) -> AnyFunctionDef: @dataclass class Field: name: str - type: Optional[str] + type: str | None value: FieldValue required: bool @@ -157,7 +157,7 @@ def with_options(self, *, ignore_optional: bool, ignore_required: bool) -> Field required=required, ) - def as_ast(self) -> Union[ast.AnnAssign, ast.Assign]: + def as_ast(self) -> ast.AnnAssign | ast.Assign: if self.type is None: return ast.Assign( targets=[ast.Name(id=self.name, ctx=ast.Store())], diff --git a/src/thriftpyi/files.py b/src/thriftpyi/files.py index eaee9e7..792d40d 100644 --- a/src/thriftpyi/files.py +++ b/src/thriftpyi/files.py @@ -1,14 +1,13 @@ from __future__ import annotations from pathlib import Path -from typing import List, Union -def list_interfaces(interfaces_dir: Union[str, Path]) -> List[Path]: +def list_interfaces(interfaces_dir: str | Path) -> list[Path]: return list(Path(interfaces_dir).glob("**/*.thrift")) -def save(data: str, to: Union[str, Path]) -> None: +def save(data: str, to: str | Path) -> None: Path.mkdir(Path(to).parent, exist_ok=True) with open(to, "w+", encoding="utf-8") as f: f.write(data) diff --git a/src/thriftpyi/proxies.py b/src/thriftpyi/proxies.py index 466ebd1..ce8df16 100644 --- a/src/thriftpyi/proxies.py +++ b/src/thriftpyi/proxies.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import List, Tuple, cast +from typing import cast from thriftpyi.entities import Field, FieldValue, Method, ModuleItem from thriftpyi.utils import get_python_type, guess_type @@ -12,7 +12,7 @@ class TModuleProxy: def __init__(self, tmodule) -> None: self.tmodule = tmodule - def get_consts(self) -> List[Field]: + def get_consts(self) -> list[Field]: tconsts = ( (name, value) for name, value in vars(self.tmodule).items() @@ -20,27 +20,27 @@ def get_consts(self) -> List[Field]: ) return [self._make_const(tconst) for tconst in tconsts] - def get_enums(self) -> List[ModuleItem]: + def get_enums(self) -> list[ModuleItem]: return [ self._make_enum(tenum) for tenum in self.tmodule.__thrift_meta__["enums"] ] - def get_exceptions(self) -> List[ModuleItem]: + def get_exceptions(self) -> list[ModuleItem]: return [ self._make_exception(texc) for texc in self.tmodule.__thrift_meta__["exceptions"] ] - def get_imports(self) -> List[str]: + def get_imports(self) -> list[str]: return [item.__name__ for item in self.tmodule.__thrift_meta__["includes"]] - def get_services(self) -> List[ModuleItem]: + def get_services(self) -> list[ModuleItem]: return [ self._make_service(tservice) for tservice in self.tmodule.__thrift_meta__["services"] ] - def get_structs(self) -> List[ModuleItem]: + def get_structs(self) -> list[ModuleItem]: return [ self._make_struct(tstruct) for tstruct in self.tmodule.__thrift_meta__["structs"] @@ -141,7 +141,7 @@ def _make_struct(tclass) -> ModuleItem: class TSpecItemProxy: __slots__ = ("ttype", "name", "meta", "required") - def __init__(self, item: Tuple): + def __init__(self, item: tuple): ttype, name, *meta, required = item self.ttype = ttype self.name = name @@ -157,7 +157,7 @@ def __init__(self, module_name: str, thrift_spec, default_spec): self.thrift_spec = [TSpecItemProxy(thrift_spec[k]) for k in sorted(thrift_spec)] self.default_spec = default_spec - def get_fields(self, *, ignore_type: bool = False) -> List[Field]: + def get_fields(self, *, ignore_type: bool = False) -> list[Field]: return [ Field( name=item.name, diff --git a/src/thriftpyi/stubs.py b/src/thriftpyi/stubs.py index b6f67f6..ced9ac0 100644 --- a/src/thriftpyi/stubs.py +++ b/src/thriftpyi/stubs.py @@ -1,7 +1,7 @@ from __future__ import annotations import ast -from typing import TYPE_CHECKING, Iterable, List +from typing import TYPE_CHECKING, Iterable if TYPE_CHECKING: from thriftpyi.proxies import TModuleProxy @@ -30,7 +30,7 @@ def build_init(imports: Iterable[str]) -> ast.Module: ) -def _make_imports(proxy: TModuleProxy) -> List[ast.ImportFrom]: +def _make_imports(proxy: TModuleProxy) -> list[ast.ImportFrom]: imports = [] if proxy.has_structs(): imports.append(_make_absolute_import("dataclasses", "dataclass")) @@ -61,22 +61,22 @@ def _make_relative_import(names: Iterable[str]) -> ast.ImportFrom: ) -def _make_consts(interface: TModuleProxy) -> List[ast.stmt]: +def _make_consts(interface: TModuleProxy) -> list[ast.stmt]: return [item.as_ast() for item in interface.get_consts()] -def _make_exceptions(interface: TModuleProxy, strict: bool) -> List[ast.ClassDef]: +def _make_exceptions(interface: TModuleProxy, strict: bool) -> list[ast.ClassDef]: return [ item.with_options(ignore_required=not strict).as_ast(bases=["Exception"]) for item in interface.get_exceptions() ] -def _make_enums(interface: TModuleProxy) -> List[ast.ClassDef]: +def _make_enums(interface: TModuleProxy) -> list[ast.ClassDef]: return [item.as_ast(bases=["IntEnum"]) for item in interface.get_enums()] -def _make_structs(interface: TModuleProxy, strict: bool) -> List[ast.ClassDef]: +def _make_structs(interface: TModuleProxy, strict: bool) -> list[ast.ClassDef]: return [ item.with_options(ignore_required=not strict).as_ast(decorators=["dataclass"]) for item in interface.get_structs() @@ -85,7 +85,7 @@ def _make_structs(interface: TModuleProxy, strict: bool) -> List[ast.ClassDef]: def _make_service( interface: TModuleProxy, is_async: bool, strict: bool -) -> List[ast.ClassDef]: +) -> list[ast.ClassDef]: services = interface.get_services() return [