Skip to content

Commit

Permalink
refactor: use future annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
lumina37 committed Jul 13, 2024
1 parent e2a1c08 commit b828f19
Show file tree
Hide file tree
Showing 61 changed files with 519 additions and 419 deletions.
8 changes: 5 additions & 3 deletions aiotieba/api/_classdef/container.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import dataclasses as dcs
from typing import Generic, Iterator, List, SupportsIndex, TypeVar, overload
from typing import Generic, Iterator, SupportsIndex, TypeVar, overload

TypeContainer = TypeVar('TypeContainer')

Expand All @@ -14,7 +16,7 @@ class Containers(Generic[TypeContainer]):
objs (list[TypeContainer]): 内容列表
"""

objs: List[TypeContainer] = dcs.field(default_factory=list)
objs: list[TypeContainer] = dcs.field(default_factory=list)

def __iter__(self) -> Iterator[TypeContainer]:
return self.objs.__iter__()
Expand All @@ -23,7 +25,7 @@ def __iter__(self) -> Iterator[TypeContainer]:
def __getitem__(self, idx: SupportsIndex) -> TypeContainer: ...

@overload
def __getitem__(self, idx: slice) -> List[TypeContainer]: ...
def __getitem__(self, idx: slice) -> list[TypeContainer]: ...

def __getitem__(self, idx):
return self.objs.__getitem__(idx)
Expand Down
20 changes: 11 additions & 9 deletions aiotieba/api/_classdef/contents.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import dataclasses as dcs
import re
from functools import cached_property
Expand All @@ -22,7 +24,7 @@ class FragText:
text: str = ""

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "FragText":
def from_tbdata(data_proto: TypeMessage) -> FragText:
text = data_proto.text
return FragText(text)

Expand All @@ -45,7 +47,7 @@ class FragEmoji:
desc: str = ""

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "FragEmoji":
def from_tbdata(data_proto: TypeMessage) -> FragEmoji:
id_ = data_proto.text
desc = data_proto.c
return FragEmoji(id_, desc)
Expand Down Expand Up @@ -83,7 +85,7 @@ class FragImage:
hash: str = ""

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "FragImage":
def from_tbdata(data_proto: TypeMessage) -> FragImage:
src = data_proto.cdn_src
big_src = data_proto.big_cdn_src
origin_src = data_proto.origin_src
Expand Down Expand Up @@ -119,7 +121,7 @@ class FragAt:
user_id: int = 0

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "FragAt":
def from_tbdata(data_proto: TypeMessage) -> FragAt:
text = data_proto.text
user_id = data_proto.uid
return FragAt(text, user_id)
Expand All @@ -144,7 +146,7 @@ class FragVoice:
duration: int = 0

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "FragVoice":
def from_tbdata(data_proto: TypeMessage) -> FragVoice:
md5 = data_proto.voice_md5
duration = data_proto.during_time / 1000
return FragVoice(md5, duration)
Expand Down Expand Up @@ -180,7 +182,7 @@ class FragVideo:
view_num: int = 0

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "FragVideo":
def from_tbdata(data_proto: TypeMessage) -> FragVideo:
src = data_proto.video_url
cover_src = data_proto.thumbnail_url
duration = data_proto.video_duration
Expand Down Expand Up @@ -220,7 +222,7 @@ class FragLink:
raw_url: yarl.URL = dcs.field(default_factory=yarl.URL)

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "FragLink":
def from_tbdata(data_proto: TypeMessage) -> FragLink:
text = data_proto.link
title = data_proto.text
raw_url = yarl.URL(text)
Expand Down Expand Up @@ -265,7 +267,7 @@ class FragTiebaPlus:
url: yarl.URL = dcs.field(default_factory=yarl.URL)

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "FragTiebaPlus":
def from_tbdata(data_proto: TypeMessage) -> FragTiebaPlus:
text = data_proto.tiebaplus_info.desc
url = yarl.URL(data_proto.tiebaplus_info.jump_url)
return FragTiebaPlus(text, url)
Expand All @@ -288,7 +290,7 @@ class FragItem:
text: str = ""

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "FragItem":
def from_tbdata(data_proto: TypeMessage) -> FragItem:
text = data_proto.item.item_name
return FragItem(text)

Expand Down
9 changes: 5 additions & 4 deletions aiotieba/api/_classdef/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import dataclasses as dcs
from typing import List

from ...enums import Gender, PrivLike, PrivReply
from .vimage import VirtualImage
Expand Down Expand Up @@ -59,7 +60,7 @@ class UserInfo:
forum_num: int = 0
sign: str = ""
ip: str = ''
icons: List[str] = dcs.field(default_factory=list)
icons: list[str] = dcs.field(default_factory=list)
vimage: VirtualImage = dcs.field(default_factory=VirtualImage)

is_vip: bool = False
Expand All @@ -71,7 +72,7 @@ class UserInfo:
def __str__(self) -> str:
return self.user_name or self.portrait or str(self.user_id)

def __eq__(self, obj: "UserInfo") -> bool:
def __eq__(self, obj: UserInfo) -> bool:
return self.user_id == obj.user_id

def __hash__(self) -> int:
Expand All @@ -80,7 +81,7 @@ def __hash__(self) -> int:
def __bool__(self) -> bool:
return bool(self.user_id)

def __ior__(self, obj) -> "UserInfo":
def __ior__(self, obj) -> UserInfo:
for field in dcs.fields(obj):
if hasattr(self, field.name):
val = getattr(obj, field.name)
Expand Down
9 changes: 5 additions & 4 deletions aiotieba/api/_classdef/vote.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import dataclasses as dcs
from typing import List

from .common import TypeMessage

Expand All @@ -18,7 +19,7 @@ class VoteOption:
text: str = ""

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "VoteOption":
def from_tbdata(data_proto: TypeMessage) -> VoteOption:
vote_num = data_proto.num
text = data_proto.text
return VoteOption(vote_num, text)
Expand All @@ -39,12 +40,12 @@ class VoteInfo:

title: str = ""
is_multi: bool = False
options: List[VoteOption] = dcs.field(default_factory=list)
options: list[VoteOption] = dcs.field(default_factory=list)
total_vote: int = 0
total_user: int = 0

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "VoteInfo":
def from_tbdata(data_proto: TypeMessage) -> VoteInfo:
title = data_proto.title
is_multi = bool(data_proto.is_multi)
options = [VoteOption.from_tbdata(p) for p in data_proto.options]
Expand Down
4 changes: 2 additions & 2 deletions aiotieba/api/del_posts/_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from __future__ import annotations

import yarl

Expand All @@ -14,7 +14,7 @@ def parse_body(body: bytes) -> None:
raise TiebaServerError(code, res_json['error_msg'])


async def request(http_core: HttpCore, fid: int, tid: int, pids: List[int], block: bool) -> BoolResponse:
async def request(http_core: HttpCore, fid: int, tid: int, pids: list[int], block: bool) -> BoolResponse:
data = [
('BDUSS', http_core.account.BDUSS),
('forum_id', fid),
Expand Down
4 changes: 2 additions & 2 deletions aiotieba/api/del_threads/_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from __future__ import annotations

import yarl

Expand All @@ -14,7 +14,7 @@ def parse_body(body: bytes) -> None:
raise TiebaServerError(code, res_json['error_msg'])


async def request(http_core: HttpCore, fid: int, tids: List[int], block: bool) -> BoolResponse:
async def request(http_core: HttpCore, fid: int, tids: list[int], block: bool) -> BoolResponse:
data = [
('BDUSS', http_core.account.BDUSS),
('forum_id', fid),
Expand Down
14 changes: 8 additions & 6 deletions aiotieba/api/get_ats/_classdef.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import dataclasses as dcs
from functools import cached_property
from typing import Mapping
Expand Down Expand Up @@ -25,7 +27,7 @@ class Page_at:
has_prev: int = False

@staticmethod
def from_tbdata(data_map: Mapping) -> "Page_at":
def from_tbdata(data_map: Mapping) -> Page_at:
current_page = int(data_map['current_page'])
has_more = bool(int(data_map['has_more']))
has_prev = bool(int(data_map['has_prev']))
Expand Down Expand Up @@ -60,7 +62,7 @@ class UserInfo_at:
priv_reply: PrivReply = PrivReply.ALL

@staticmethod
def from_tbdata(data_map: Mapping) -> "UserInfo_at":
def from_tbdata(data_map: Mapping) -> UserInfo_at:
user_id = int(data_map['id'])
portrait = data_map['portrait']
if '?' in portrait:
Expand All @@ -79,7 +81,7 @@ def from_tbdata(data_map: Mapping) -> "UserInfo_at":
def __str__(self) -> str:
return self.user_name or self.portrait or str(self.user_id)

def __eq__(self, obj: "UserInfo_at") -> bool:
def __eq__(self, obj: UserInfo_at) -> bool:
return self.user_id == obj.user_id

def __hash__(self) -> int:
Expand Down Expand Up @@ -139,7 +141,7 @@ class At:
create_time: int = 0

@staticmethod
def from_tbdata(data_map: Mapping) -> "At":
def from_tbdata(data_map: Mapping) -> At:
text = data_map['content']
fname = data_map['fname']
tid = int(data_map['thread_id'])
Expand All @@ -150,7 +152,7 @@ def from_tbdata(data_map: Mapping) -> "At":
create_time = int(data_map['time'])
return At(text, fname, tid, pid, user, is_comment, is_thread, create_time)

def __eq__(self, obj: "At") -> bool:
def __eq__(self, obj: At) -> bool:
return self.pid == obj.pid

def __hash__(self) -> int:
Expand All @@ -177,7 +179,7 @@ class Ats(TbErrorExt, Containers[At]):
page: Page_at = dcs.field(default_factory=Page_at)

@staticmethod
def from_tbdata(data_map: Mapping) -> "Ats":
def from_tbdata(data_map: Mapping) -> Ats:
objs = [At.from_tbdata(m) for m in data_map.get('at_list', [])]
page = Page_at.from_tbdata(data_map['page'])
return Ats(objs, page)
Expand Down
33 changes: 17 additions & 16 deletions aiotieba/api/get_bawu_info/_classdef.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import dataclasses as dcs
from functools import cached_property
from typing import List

from .._classdef import TypeMessage

Expand Down Expand Up @@ -31,7 +32,7 @@ class UserInfo_bawu:
level: int = 0

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "UserInfo_bawu":
def from_tbdata(data_proto: TypeMessage) -> UserInfo_bawu:
user_id = data_proto.user_id
portrait = data_proto.portrait
user_name = data_proto.user_name
Expand All @@ -42,7 +43,7 @@ def from_tbdata(data_proto: TypeMessage) -> "UserInfo_bawu":
def __str__(self) -> str:
return self.user_name or self.portrait or str(self.user_id)

def __eq__(self, obj: "UserInfo_bawu") -> bool:
def __eq__(self, obj: UserInfo_bawu) -> bool:
return self.user_id == obj.user_id

def __hash__(self) -> int:
Expand Down Expand Up @@ -89,26 +90,26 @@ class BawuInfo:
fourth_admin (list[UserInfo_bawu]): 第四吧主
"""

all: List[UserInfo_bawu] = dcs.field(default_factory=list, repr=False)
all: list[UserInfo_bawu] = dcs.field(default_factory=list, repr=False)

admin: List[UserInfo_bawu] = dcs.field(default_factory=list)
manager: List[UserInfo_bawu] = dcs.field(default_factory=list)
voice_editor: List[UserInfo_bawu] = dcs.field(default_factory=list)
image_editor: List[UserInfo_bawu] = dcs.field(default_factory=list)
video_editor: List[UserInfo_bawu] = dcs.field(default_factory=list)
broadcast_editor: List[UserInfo_bawu] = dcs.field(default_factory=list)
journal_chief_editor: List[UserInfo_bawu] = dcs.field(default_factory=list)
journal_editor: List[UserInfo_bawu] = dcs.field(default_factory=list)
profess_admin: List[UserInfo_bawu] = dcs.field(default_factory=list)
fourth_admin: List[UserInfo_bawu] = dcs.field(default_factory=list)
admin: list[UserInfo_bawu] = dcs.field(default_factory=list)
manager: list[UserInfo_bawu] = dcs.field(default_factory=list)
voice_editor: list[UserInfo_bawu] = dcs.field(default_factory=list)
image_editor: list[UserInfo_bawu] = dcs.field(default_factory=list)
video_editor: list[UserInfo_bawu] = dcs.field(default_factory=list)
broadcast_editor: list[UserInfo_bawu] = dcs.field(default_factory=list)
journal_chief_editor: list[UserInfo_bawu] = dcs.field(default_factory=list)
journal_editor: list[UserInfo_bawu] = dcs.field(default_factory=list)
profess_admin: list[UserInfo_bawu] = dcs.field(default_factory=list)
fourth_admin: list[UserInfo_bawu] = dcs.field(default_factory=list)

@staticmethod
def from_tbdata(data_proto: TypeMessage) -> "BawuInfo":
def from_tbdata(data_proto: TypeMessage) -> BawuInfo:
all_ = []
r_protos = data_proto.bawu_team_info.bawu_team_list
_dict = {r_proto.role_name: [UserInfo_bawu.from_tbdata(p) for p in r_proto.role_info] for r_proto in r_protos}

def extract(role_name: str) -> List[UserInfo_bawu]:
def extract(role_name: str) -> list[UserInfo_bawu]:
if users := _dict.get(role_name):
all_.extend(users)
else:
Expand Down
4 changes: 3 additions & 1 deletion aiotieba/api/get_bawu_perm/_classdef.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import dataclasses as dcs
from typing import Mapping

Expand All @@ -17,7 +19,7 @@ class BawuPerm(TbErrorExt):

perms: BawuPermType = BawuPermType.NULL

def from_tbdata(data_map: Mapping) -> "BawuPerm":
def from_tbdata(data_map: Mapping) -> BawuPerm:
perms = BawuPermType.NULL

for cate in ['category_user', 'category_thread']:
Expand Down
7 changes: 4 additions & 3 deletions aiotieba/api/get_bawu_postlogs/_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import datetime
import time
from typing import Optional
from urllib.parse import quote

import bs4
Expand All @@ -25,8 +26,8 @@ async def request(
pn: int,
search_value: str,
search_type: BawuSearchType,
start_dt: Optional[datetime.datetime],
end_dt: Optional[datetime.datetime],
start_dt: datetime.datetime | None,
end_dt: datetime.datetime | None,
op_type: int,
) -> Postlogs:
params = [
Expand Down
Loading

0 comments on commit b828f19

Please sign in to comment.