mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-11 03:09:31 +00:00
Use modern typehint syntax
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import weakref
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
|
||||
class FakeFs:
|
||||
def __init__(self) -> None:
|
||||
self._files: Dict[Path, bytearray] = {}
|
||||
self._files: dict[Path, bytearray] = {}
|
||||
|
||||
def open(self, path: Path) -> "SourceWriter":
|
||||
return SourceWriter(self, path)
|
||||
|
@@ -1,5 +1,4 @@
|
||||
from pathlib import Path
|
||||
from typing import Set
|
||||
|
||||
from ..tl_parser import NormalParameter, ParsedTl
|
||||
from .fakefs import FakeFs, SourceWriter
|
||||
@@ -19,7 +18,7 @@ from .serde.serialization import generate_function, generate_write
|
||||
|
||||
|
||||
def generate_init(
|
||||
writer: SourceWriter, namespaces: Set[str], classes: Set[str]
|
||||
writer: SourceWriter, namespaces: set[str], classes: set[str]
|
||||
) -> None:
|
||||
sorted_cls = list(sorted(classes))
|
||||
sorted_ns = list(sorted(namespaces))
|
||||
@@ -46,14 +45,14 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None:
|
||||
|
||||
ignored_types = {"true", "boolTrue", "boolFalse"} # also "compiler built-ins"
|
||||
|
||||
abc_namespaces: Set[str] = set()
|
||||
type_namespaces: Set[str] = set()
|
||||
function_namespaces: Set[str] = set()
|
||||
abc_namespaces: set[str] = set()
|
||||
type_namespaces: set[str] = set()
|
||||
function_namespaces: set[str] = set()
|
||||
|
||||
abc_class_names: Set[str] = set()
|
||||
type_class_names: Set[str] = set()
|
||||
function_def_names: Set[str] = set()
|
||||
generated_type_names: Set[str] = set()
|
||||
abc_class_names: set[str] = set()
|
||||
type_class_names: set[str] = set()
|
||||
function_def_names: set[str] = set()
|
||||
generated_type_names: set[str] = set()
|
||||
|
||||
for typedef in tl.typedefs:
|
||||
if typedef.ty.full_name not in generated_types:
|
||||
@@ -193,10 +192,10 @@ def generate(fs: FakeFs, tl: ParsedTl) -> None:
|
||||
writer.write(
|
||||
"from .core import Serializable, Reader, deserialize_bool, deserialize_i32_list, deserialize_i64_list, deserialize_identity, single_deserializer, list_deserializer"
|
||||
)
|
||||
writer.write("from typing import cast, Tuple, Type")
|
||||
writer.write("from typing import cast, Type")
|
||||
writer.write(f"LAYER = {tl.layer!r}")
|
||||
writer.write(
|
||||
"TYPE_MAPPING = {t.constructor_id(): t for t in cast(Tuple[Type[Serializable]], ("
|
||||
"TYPE_MAPPING = {t.constructor_id(): t for t in cast(tuple[Type[Serializable]], ("
|
||||
)
|
||||
for name in sorted(generated_type_names):
|
||||
writer.write(f" types.{name},")
|
||||
|
@@ -1,10 +1,10 @@
|
||||
import re
|
||||
from typing import Iterator, List
|
||||
from collections.abc import Iterator
|
||||
|
||||
from ....tl_parser import BaseParameter, FlagsParameter, NormalParameter, Type
|
||||
|
||||
|
||||
def split_words(name: str) -> List[str]:
|
||||
def split_words(name: str) -> list[str]:
|
||||
return re.findall(
|
||||
r"""
|
||||
^$
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import struct
|
||||
from itertools import groupby
|
||||
from typing import Optional, Tuple
|
||||
from typing import Optional
|
||||
|
||||
from ....tl_parser import Definition, NormalParameter, Parameter, Type
|
||||
from ..fakefs import SourceWriter
|
||||
@@ -14,7 +14,7 @@ SPECIAL_CASED_OBJECT_READS = {
|
||||
}
|
||||
|
||||
|
||||
def reader_read_fmt(ty: Type, constructor_id: int) -> Tuple[str, Optional[str]]:
|
||||
def reader_read_fmt(ty: Type, constructor_id: int) -> tuple[str, Optional[str]]:
|
||||
if is_trivial(NormalParameter(ty=ty, flag=None)):
|
||||
fmt = trivial_struct_fmt(NormalParameter(ty=ty, flag=None))
|
||||
size = struct.calcsize(f"<{fmt}")
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import struct
|
||||
from collections.abc import Iterator
|
||||
from itertools import groupby
|
||||
from typing import Iterator
|
||||
|
||||
from ....tl_parser import Definition, FlagsParameter, NormalParameter, Parameter, Type
|
||||
from ..fakefs import SourceWriter
|
||||
@@ -104,9 +104,11 @@ def generate_write(writer: SourceWriter, defn: Definition) -> None:
|
||||
for param in group:
|
||||
if isinstance(param.ty, FlagsParameter):
|
||||
flags = " | ".join(
|
||||
f"({1 << p.ty.flag.index} if self.{p.name} else 0)"
|
||||
if p.ty.ty.name == "true"
|
||||
else f"(0 if self.{p.name} is None else {1 << p.ty.flag.index})"
|
||||
(
|
||||
f"({1 << p.ty.flag.index} if self.{p.name} else 0)"
|
||||
if p.ty.ty.name == "true"
|
||||
else f"(0 if self.{p.name} is None else {1 << p.ty.flag.index})"
|
||||
)
|
||||
for p in defn.params
|
||||
if isinstance(p.ty, NormalParameter)
|
||||
and p.ty.flag
|
||||
@@ -140,9 +142,11 @@ def generate_function(writer: SourceWriter, defn: Definition) -> None:
|
||||
for param in group:
|
||||
if isinstance(param.ty, FlagsParameter):
|
||||
flags = " | ".join(
|
||||
f"({1 << p.ty.flag.index} if {p.name} else 0)"
|
||||
if p.ty.ty.name == "true"
|
||||
else f"(0 if {p.name} is None else {1 << p.ty.flag.index})"
|
||||
(
|
||||
f"({1 << p.ty.flag.index} if {p.name} else 0)"
|
||||
if p.ty.ty.name == "true"
|
||||
else f"(0 if {p.name} is None else {1 << p.ty.flag.index})"
|
||||
)
|
||||
for p in defn.params
|
||||
if isinstance(p.ty, NormalParameter)
|
||||
and p.ty.flag
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import List, Optional, Union
|
||||
from typing import Optional
|
||||
|
||||
from .tl import Definition
|
||||
from .tl_iterator import FunctionDef, TypeDef, iterate
|
||||
@@ -10,13 +10,13 @@ from .tl_iterator import FunctionDef, TypeDef, iterate
|
||||
@dataclass
|
||||
class ParsedTl:
|
||||
layer: Optional[int]
|
||||
typedefs: List[Definition]
|
||||
functiondefs: List[Definition]
|
||||
typedefs: list[Definition]
|
||||
functiondefs: list[Definition]
|
||||
|
||||
|
||||
def load_tl_file(path: Union[str, Path]) -> ParsedTl:
|
||||
typedefs: List[TypeDef] = []
|
||||
functiondefs: List[FunctionDef] = []
|
||||
def load_tl_file(path: str | Path) -> ParsedTl:
|
||||
typedefs: list[TypeDef] = []
|
||||
functiondefs: list[FunctionDef] = []
|
||||
with open(path, "r", encoding="utf-8") as fd:
|
||||
contents = fd.read()
|
||||
|
||||
|
@@ -1,5 +1,5 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Self, Set
|
||||
from typing import Self
|
||||
|
||||
from ..utils import infer_id
|
||||
from .parameter import Parameter, TypeDefNotImplemented
|
||||
@@ -9,10 +9,10 @@ from .ty import Type
|
||||
|
||||
@dataclass
|
||||
class Definition:
|
||||
namespace: List[str]
|
||||
namespace: list[str]
|
||||
name: str
|
||||
id: int
|
||||
params: List[Parameter]
|
||||
params: list[Parameter]
|
||||
ty: Type
|
||||
|
||||
@classmethod
|
||||
@@ -58,9 +58,9 @@ class Definition:
|
||||
except ValueError:
|
||||
raise ValueError("invalid id")
|
||||
|
||||
type_defs: List[str] = []
|
||||
flag_defs: List[str] = []
|
||||
params: List[Parameter] = []
|
||||
type_defs: list[str] = []
|
||||
flag_defs: list[str] = []
|
||||
params: list[Parameter] = []
|
||||
|
||||
for param_str in middle.split():
|
||||
try:
|
||||
@@ -102,7 +102,7 @@ class Definition:
|
||||
res += f"{ns}."
|
||||
res += f"{self.name}#{self.id:x}"
|
||||
|
||||
def_set: Set[str] = set()
|
||||
def_set: set[str] = set()
|
||||
for param in self.params:
|
||||
if isinstance(param.ty, NormalParameter):
|
||||
def_set.update(param.ty.ty.find_generic_refs())
|
||||
|
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Union
|
||||
from typing import Optional
|
||||
|
||||
from .flag import Flag
|
||||
from .ty import Type
|
||||
@@ -8,7 +10,7 @@ from .ty import Type
|
||||
|
||||
class BaseParameter(ABC):
|
||||
@staticmethod
|
||||
def from_str(ty: str) -> Union["FlagsParameter", "NormalParameter"]:
|
||||
def from_str(ty: str) -> FlagsParameter | NormalParameter:
|
||||
if not ty:
|
||||
raise ValueError("empty")
|
||||
if ty == "#":
|
||||
|
@@ -1,10 +1,11 @@
|
||||
from collections.abc import Iterator
|
||||
from dataclasses import dataclass
|
||||
from typing import Iterator, List, Optional, Self
|
||||
from typing import Optional, Self
|
||||
|
||||
|
||||
@dataclass
|
||||
class Type:
|
||||
namespace: List[str]
|
||||
namespace: list[str]
|
||||
name: str
|
||||
bare: bool
|
||||
generic_ref: bool
|
||||
|
@@ -1,4 +1,5 @@
|
||||
from typing import Iterator, Type
|
||||
from collections.abc import Iterator
|
||||
from typing import Type
|
||||
|
||||
from .tl.definition import Definition
|
||||
from .utils import remove_tl_comments
|
||||
|
Reference in New Issue
Block a user