Skip to content

Commit

Permalink
♻️ correct type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
YousefEZ committed Dec 26, 2023
1 parent 9ebc00a commit cf39314
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion qalib/translators/element/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class EmbedData(EmbedBaseData):
def render(embed_data: Union[EmbedData, EmbedAdapter]) -> discord.Embed:
embed = discord.Embed(
title=embed_data.title,
colour=COLOURS[embed_data.colour] if type(embed_data.colour) == str else cast(int, embed_data.colour),
colour=embed_data.colour,
type=embed_data.type,
description=embed_data.description,
timestamp=embed_data.timestamp
Expand Down
4 changes: 2 additions & 2 deletions qalib/translators/element/expansive.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def compile_field(end: Optional[int] = None) -> Field:

for i in range(len(lines)):
diff_char = key is not None and len(str(len(values))) - len(key)
var_char: int = int(key is not None) and lines[start: i + 1].count(key) * diff_char
var_char: int = key is not None and lines[start: i + 1].count(key) * diff_char
if sum(map(len, lines[start: i + 1])) + var_char >= MAX_FIELD_LENGTH:
values.append(compile_field(i))
start = i
Expand All @@ -62,7 +62,7 @@ def compile_field(end: Optional[int] = None) -> Field:

def _page_key_guard(
func: Callable[[str, _T, int], _T]
) -> Callable[[str, _T, int], _T]:
) -> Callable[[Optional[str], _T, int], _T]:
"""A decorator that guards a function so that it only runs if the embed proxy has a page key.
Args:
Expand Down
6 changes: 3 additions & 3 deletions qalib/translators/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from qalib.translators.menu import Menu, MenuActions
from qalib.translators.message_parsing import ButtonComponent, apply, \
create_button, make_emoji, make_channel_types, create_channel_select, create_select, \
CustomSelects, create_type_select, TextInputComponent, create_text_input
CustomSelects, create_type_select, TextInputComponent, create_text_input, pipe
from qalib.translators.modal import QalibModal, ModalEvents, ModalEventsCallbacks
from qalib.translators.templater import Templater
from qalib.translators.view import QalibView
Expand Down Expand Up @@ -158,9 +158,9 @@ def deserialize_message(
nonce=apply(message_tree.get("nonce"), int),
delete_after=apply(message_tree.get("delete_after"), float),
suppress_embeds=message_tree.get("suppress_embeds"),
file=apply(message_tree.get("file"), lambda file: self._render_file(file)),
file=pipe(message_tree.get("file"), self._render_file),
files=apply(message_tree.get("files"), lambda files: list(map(self._render_file, files))),
allowed_mentions=apply(message_tree.get("allowed_mentions"), lambda am: self._render_allowed_mentions(am)),
allowed_mentions=pipe(message_tree.get("allowed_mentions"), self._render_allowed_mentions),
reference=apply(message_tree.get("message_reference"), lambda reference: discord.MessageReference(
message_id=reference["message_id"],
channel_id=reference["channel_id"],
Expand Down
6 changes: 6 additions & 0 deletions qalib/translators/message_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ def create_text_input(text_input_component: TextInputComponent) -> ui.TextInput:
)


def pipe(element: Optional[M], func: Callable[[M], N]) -> Optional[N]:
if element is None:
return None
return func(element)


def apply(
element: Optional[M],
func: Callable[Concatenate[M, P], N],
Expand Down

0 comments on commit cf39314

Please sign in to comment.