Skip to content

Commit

Permalink
Add pyupgrade to the pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
unmade committed Oct 20, 2024
1 parent 1085ec7 commit 5f43b73
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
6 changes: 5 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion example/app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions src/thriftpyi/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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(
Expand Down Expand Up @@ -139,7 +139,7 @@ def as_ast(self) -> AnyFunctionDef:
@dataclass
class Field:
name: str
type: Optional[str]
type: str | None
value: FieldValue
required: bool

Expand All @@ -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())],
Expand Down
5 changes: 2 additions & 3 deletions src/thriftpyi/files.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 9 additions & 9 deletions src/thriftpyi/proxies.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,35 +12,35 @@ 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()
if value in self.tmodule.__thrift_meta__["consts"]
)
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"]
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions src/thriftpyi/stubs.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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()
Expand All @@ -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 [
Expand Down

0 comments on commit 5f43b73

Please sign in to comment.