Skip to content

Commit

Permalink
refactor: update ruff rules list
Browse files Browse the repository at this point in the history
thanks to the Hydrogram project for prividing these rules
  • Loading branch information
alissonlauffer committed Jun 1, 2024
1 parent 3ff68a9 commit a9107d3
Show file tree
Hide file tree
Showing 20 changed files with 88 additions and 55 deletions.
9 changes: 7 additions & 2 deletions eduu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
from subprocess import run

__commit__ = (
run(["git", "rev-parse", "--short", "HEAD"], capture_output=True).stdout.decode().strip()
run(["git", "rev-parse", "--short", "HEAD"], capture_output=True, check=False)
.stdout.decode()
.strip()
or "None"
)

__version_number__ = (
run(["git", "rev-list", "--count", "HEAD"], capture_output=True).stdout.decode().strip() or "0"
run(["git", "rev-list", "--count", "HEAD"], capture_output=True, check=False)
.stdout.decode()
.strip()
or "0"
)
2 changes: 1 addition & 1 deletion eduu/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def start(self):
self.me.username,
)

from .database.restarted import del_restarted, get_restarted
from .database.restarted import del_restarted, get_restarted # noqa: PLC0415

wr = await get_restarted()
await del_restarted()
Expand Down
6 changes: 3 additions & 3 deletions eduu/database/admins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2024 Amano LLC

from typing import Optional
from __future__ import annotations

from .core import database

Expand All @@ -15,7 +15,7 @@ async def check_if_del_service(chat_id: int) -> bool:
return row[0]


async def toggle_del_service(chat_id: int, mode: Optional[bool]) -> None:
async def toggle_del_service(chat_id: int, mode: bool | None) -> None:
await conn.execute("UPDATE groups SET delservicemsgs = ? WHERE chat_id = ?", (mode, chat_id))
await conn.commit()

Expand All @@ -27,6 +27,6 @@ async def check_if_antichannelpin(chat_id: int) -> bool:
return row[0]


async def toggle_antichannelpin(chat_id: int, mode: Optional[bool]) -> None:
async def toggle_antichannelpin(chat_id: int, mode: bool | None) -> None:
await conn.execute("UPDATE groups SET antichannelpin = ? WHERE chat_id = ?", (mode, chat_id))
await conn.commit()
2 changes: 1 addition & 1 deletion eduu/database/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


async def set_db_lang(chat_id: int, chat_type: str, lang_code: str):
if chat_type in [ChatType.PRIVATE, ChatType.BOT]:
if chat_type in {ChatType.PRIVATE, ChatType.BOT}:
await conn.execute(
"UPDATE users SET chat_lang = ? WHERE user_id = ?", (lang_code, chat_id)
)
Expand Down
6 changes: 3 additions & 3 deletions eduu/database/warns.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2024 Amano LLC

from typing import Optional
from __future__ import annotations

from .core import database

conn = database.get_conn()


async def get_warn_action(chat_id: int) -> tuple[Optional[str], bool]:
async def get_warn_action(chat_id: int) -> tuple[str | None, bool]:
cursor = await conn.execute("SELECT warn_action FROM groups WHERE chat_id = (?)", (chat_id,))
res = (await cursor.fetchone())[0]
return "ban" if res is None else res


async def set_warn_action(chat_id: int, action: Optional[str]):
async def set_warn_action(chat_id: int, action: str | None):
await conn.execute("UPDATE groups SET warn_action = ? WHERE chat_id = ?", (action, chat_id))
await conn.commit()

Expand Down
6 changes: 3 additions & 3 deletions eduu/database/welcome.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2024 Amano LLC

from typing import Optional
from __future__ import annotations

from .core import database

conn = database.get_conn()


async def get_welcome(chat_id: int) -> tuple[Optional[str], bool]:
async def get_welcome(chat_id: int) -> tuple[str | None, bool]:
cursor = await conn.execute(
"SELECT welcome, welcome_enabled FROM groups WHERE chat_id = (?)", (chat_id,)
)
return await cursor.fetchone()


async def set_welcome(chat_id: int, welcome: Optional[str]):
async def set_welcome(chat_id: int, welcome: str | None):
await conn.execute("UPDATE groups SET welcome = ? WHERE chat_id = ?", (welcome, chat_id))
await conn.commit()

Expand Down
8 changes: 4 additions & 4 deletions eduu/plugins/inlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
)


@Client.on_inline_query(filters.regex(r"^face", re.I))
@Client.on_inline_query(filters.regex(r"^face", re.IGNORECASE))
async def faces_inline(c: Client, q: InlineQuery):
results: list[InlineQueryResultArticle] = [
InlineQueryResultArticle(title=i, input_message_content=InputTextMessageContent(i))
Expand All @@ -67,7 +67,7 @@ async def faces_inline(c: Client, q: InlineQuery):
await q.answer(results)


@Client.on_inline_query(filters.regex(r"^markdown .+", re.I))
@Client.on_inline_query(filters.regex(r"^markdown .+", re.IGNORECASE))
@use_chat_lang
async def markdown_inline(c: Client, q: InlineQuery, strings):
queryinputres = q.query.split(None, 1)[1]
Expand All @@ -81,7 +81,7 @@ async def markdown_inline(c: Client, q: InlineQuery, strings):
])


@Client.on_inline_query(filters.regex(r"^html .+", re.I))
@Client.on_inline_query(filters.regex(r"^html .+", re.IGNORECASE))
@use_chat_lang
async def html_inline(c: Client, q: InlineQuery, strings):
queryinputres = q.query.split(None, 1)[1]
Expand All @@ -97,7 +97,7 @@ async def html_inline(c: Client, q: InlineQuery, strings):
])


@Client.on_inline_query(filters.regex(r"^info .+", re.I))
@Client.on_inline_query(filters.regex(r"^info .+", re.IGNORECASE))
@use_chat_lang
async def info_inline(c: Client, q: InlineQuery, strings):
try:
Expand Down
2 changes: 1 addition & 1 deletion eduu/plugins/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def ip_callback(c: Client, cb: CallbackQuery, strings):
await cb.edit_message_text(format_api_return(await get_api_return(ip), strings))


@Client.on_inline_query(filters.regex(r"^ip .+", re.I))
@Client.on_inline_query(filters.regex(r"^ip .+", re.IGNORECASE))
@use_chat_lang
async def ip_inline(c: Client, q: InlineQuery, strings):
if len(q.query.split()) == 1:
Expand Down
5 changes: 3 additions & 2 deletions eduu/plugins/langs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2024 Amano LLC

from __future__ import annotations

from itertools import zip_longest
from typing import Union

from hydrogram import Client, filters
from hydrogram.enums import ChatType
Expand Down Expand Up @@ -37,7 +38,7 @@ def gen_langs_kb():
@Client.on_message(filters.command(["setchatlang", "setlang"], PREFIXES) & filters.group)
@require_admin(allow_in_private=True)
@use_chat_lang
async def chlang(c: Client, m: Union[CallbackQuery, Message], strings):
async def chlang(c: Client, m: CallbackQuery | Message, strings):
keyboard = InlineKeyboardMarkup(
inline_keyboard=[
*gen_langs_kb(),
Expand Down
2 changes: 1 addition & 1 deletion eduu/plugins/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def reddit(c: Client, m: Message, strings):

r = await http.get(f"https://www.reddit.com/r/{subreddit}/.json?limit=6")

if r.status_code in (404, 403):
if r.status_code in {404, 403}:
await m.reply_text(strings("not_found"))
return

Expand Down
6 changes: 3 additions & 3 deletions eduu/plugins/start.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2024 Amano LLC

from typing import Union
from __future__ import annotations

from hydrogram import Client, filters
from hydrogram.types import (
Expand All @@ -21,7 +21,7 @@
@Client.on_message(filters.command("start", PREFIXES) & filters.private, group=2)
@Client.on_callback_query(filters.regex("^start_back$"))
@use_chat_lang
async def start_pvt(c: Client, m: Union[Message, CallbackQuery], strings):
async def start_pvt(c: Client, m: Message | CallbackQuery, strings):
if isinstance(m, CallbackQuery):
msg = m.message
send = msg.edit_text
Expand Down Expand Up @@ -49,7 +49,7 @@ async def start_pvt(c: Client, m: Union[Message, CallbackQuery], strings):

@Client.on_message(filters.command("start", PREFIXES) & filters.group, group=2)
@use_chat_lang
async def start_grp(c: Client, m: Union[Message, CallbackQuery], strings):
async def start_grp(c: Client, m: Message | CallbackQuery, strings):
keyboard = InlineKeyboardMarkup(
inline_keyboard=[
[
Expand Down
10 changes: 7 additions & 3 deletions eduu/plugins/sudos.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2024 Amano LLC

from __future__ import annotations

import asyncio
import html
import io
Expand All @@ -11,14 +13,13 @@
import traceback
from contextlib import redirect_stdout, suppress
from sqlite3 import IntegrityError, OperationalError
from typing import Union
from typing import TYPE_CHECKING

import humanfriendly
import speedtest
from hydrogram import Client, filters
from hydrogram.enums import ChatType
from hydrogram.errors import RPCError
from hydrogram.types import Message
from meval import meval

from config import DATABASE_PATH
Expand All @@ -28,7 +29,10 @@
from eduu.utils.localization import use_chat_lang
from eduu.utils.utils import shell_exec

prefix: Union[list, str] = "!"
if TYPE_CHECKING:
from hydrogram.types import Message

prefix: list | str = "!"

conn = database.get_conn()

Expand Down
2 changes: 1 addition & 1 deletion eduu/plugins/tiorunexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async def exec_tio_run_code(c: Client, m: Message, strings):
)


@Client.on_inline_query(filters.regex(r"^(run|exec)", re.I))
@Client.on_inline_query(filters.regex(r"^(run|exec)", re.IGNORECASE))
@use_chat_lang
async def exec_tio_run_code_inline(c: Client, q: InlineQuery, strings):
execlanguage = q.query.lower().split()[1]
Expand Down
2 changes: 1 addition & 1 deletion eduu/plugins/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async def translate(c: Client, m: Message, strings):
)


@Client.on_inline_query(filters.regex(r"^tr .+", re.I))
@Client.on_inline_query(filters.regex(r"^tr .+", re.IGNORECASE))
@use_chat_lang
async def tr_inline(c: Client, q: InlineQuery, strings):
to_tr = q.query.split(None, 2)[2]
Expand Down
2 changes: 1 addition & 1 deletion eduu/plugins/warns.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async def set_warns_action_cmd(c: Client, m: Message, strings):
await m.reply_text(strings("warn_action_status").format(action=warn_act))
return

if m.command[1] not in ("ban", "mute", "kick"):
if m.command[1] not in {"ban", "mute", "kick"}:
await m.reply_text(strings("warns_action_set_invlaid"))
return

Expand Down
7 changes: 4 additions & 3 deletions eduu/plugins/weather.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2024 Amano LLC

from __future__ import annotations

import re
from typing import Union

from hydrogram import Client, filters
from hydrogram.types import (
Expand Down Expand Up @@ -81,9 +82,9 @@ def get_status_emoji(status_code: int) -> str:


@Client.on_message(filters.command(["clima", "weather"], PREFIXES))
@Client.on_inline_query(filters.regex(r"^(clima|weather) .+", re.I))
@Client.on_inline_query(filters.regex(r"^(clima|weather) .+", re.IGNORECASE))
@use_chat_lang
async def weather(c: Client, m: Union[InlineQuery, Message], strings):
async def weather(c: Client, m: InlineQuery | Message, strings):
text = m.text if isinstance(m, Message) else m.query
if len(text.split(maxsplit=1)) == 1:
if isinstance(m, Message):
Expand Down
8 changes: 5 additions & 3 deletions eduu/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2024 Amano LLC

from __future__ import annotations

import asyncio
from functools import partial, wraps
from typing import Callable, Optional, Union
from typing import Callable

from hydrogram import Client, StopPropagation
from hydrogram.enums import ChatType
Expand All @@ -30,7 +32,7 @@ async def run(*args, loop=None, executor=None, **kwargs):


def require_admin(
permissions: Optional[ChatPrivileges] = None,
permissions: ChatPrivileges | None = None,
allow_in_private: bool = False,
complain_missing_perms: bool = True,
):
Expand All @@ -54,7 +56,7 @@ def require_admin(

def decorator(func):
@wraps(func)
async def wrapper(client: Client, message: Union[CallbackQuery, Message], *args, **kwargs):
async def wrapper(client: Client, message: CallbackQuery | Message, *args, **kwargs):
lang = await get_lang(message)
strings = partial(
get_locale_string,
Expand Down
8 changes: 5 additions & 3 deletions eduu/utils/localization.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2018-2024 Amano LLC

from __future__ import annotations

import json
from functools import partial
from pathlib import Path
from typing import Callable, Optional, Union
from typing import Callable

from hydrogram.enums import ChatType
from hydrogram.types import CallbackQuery, InlineQuery, Message
Expand Down Expand Up @@ -67,7 +69,7 @@ def get_locale_string(
language: str,
default_context: str,
key: str,
context: Optional[str] = None,
context: str | None = None,
) -> str:
if context:
default_context = context
Expand All @@ -76,7 +78,7 @@ def get_locale_string(
return res


async def get_lang(message: Union[CallbackQuery, Message, InlineQuery]) -> str:
async def get_lang(message: CallbackQuery | Message | InlineQuery) -> str:
if isinstance(message, CallbackQuery):
chat = message.message.chat if message.message else message.from_user
chat_type = message.message.chat.type if message.message else ChatType.PRIVATE
Expand Down
Loading

0 comments on commit a9107d3

Please sign in to comment.