mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Merge pull request #1 from muhammedfurkan/sourcery/dev-2.x
Sourcery refactored dev-2.x branch
This commit is contained in:
commit
49efa25664
18 changed files with 50 additions and 85 deletions
|
|
@ -206,12 +206,5 @@ class MongoStorage(BaseStorage):
|
|||
:return: list of tuples where first element is chat id and second is user id
|
||||
"""
|
||||
db = await self.get_db()
|
||||
result = []
|
||||
|
||||
items = await db[STATE].find().to_list()
|
||||
for item in items:
|
||||
result.append(
|
||||
(int(item['chat']), int(item['user']))
|
||||
)
|
||||
|
||||
return result
|
||||
return [(int(item['chat']), int(item['user'])) for item in items]
|
||||
|
|
|
|||
|
|
@ -219,15 +219,10 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
|
|||
:return:
|
||||
"""
|
||||
if fast:
|
||||
tasks = []
|
||||
for update in updates:
|
||||
tasks.append(self.updates_handler.notify(update))
|
||||
tasks = [self.updates_handler.notify(update) for update in updates]
|
||||
return await asyncio.gather(*tasks)
|
||||
|
||||
results = []
|
||||
for update in updates:
|
||||
results.append(await self.updates_handler.notify(update))
|
||||
return results
|
||||
return [await self.updates_handler.notify(update) for update in updates]
|
||||
|
||||
async def process_update(self, update: types.Update):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -278,9 +278,11 @@ class Text(Filter):
|
|||
elif check == 0:
|
||||
raise ValueError(f"No one mode is specified!")
|
||||
|
||||
equals, contains, endswith, startswith = map(lambda e: [e] if isinstance(e, str) or isinstance(e, LazyProxy)
|
||||
else e,
|
||||
(equals, contains, endswith, startswith))
|
||||
equals, contains, endswith, startswith = map(
|
||||
lambda e: [e] if isinstance(e, (str, LazyProxy)) else e,
|
||||
(equals, contains, endswith, startswith),
|
||||
)
|
||||
|
||||
self.equals = equals
|
||||
self.contains = contains
|
||||
self.endswith = endswith
|
||||
|
|
|
|||
|
|
@ -48,9 +48,13 @@ class FiltersFactory:
|
|||
:param full_config:
|
||||
:return:
|
||||
"""
|
||||
filters_set = []
|
||||
filters_set.extend(self._resolve_registered(event_handler,
|
||||
{k: v for k, v in full_config.items() if v is not None}))
|
||||
filters_set = list(
|
||||
self._resolve_registered(
|
||||
event_handler,
|
||||
{k: v for k, v in full_config.items() if v is not None},
|
||||
)
|
||||
)
|
||||
|
||||
if custom_filters:
|
||||
filters_set.extend(custom_filters)
|
||||
|
||||
|
|
|
|||
|
|
@ -242,7 +242,7 @@ class NotFilter(_LogicFilter):
|
|||
class AndFilter(_LogicFilter):
|
||||
|
||||
def __init__(self, *targets):
|
||||
self.targets = list(wrap_async(target) for target in targets)
|
||||
self.targets = [wrap_async(target) for target in targets]
|
||||
|
||||
async def check(self, *args):
|
||||
"""
|
||||
|
|
@ -268,7 +268,7 @@ class AndFilter(_LogicFilter):
|
|||
|
||||
class OrFilter(_LogicFilter):
|
||||
def __init__(self, *targets):
|
||||
self.targets = list(wrap_async(target) for target in targets)
|
||||
self.targets = [wrap_async(target) for target in targets]
|
||||
|
||||
async def check(self, *args):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -25,8 +25,7 @@ class CancelHandler(Exception):
|
|||
def _get_spec(func: callable):
|
||||
while hasattr(func, '__wrapped__'): # Try to resolve decorated callbacks
|
||||
func = func.__wrapped__
|
||||
spec = inspect.getfullargspec(func)
|
||||
return spec
|
||||
return inspect.getfullargspec(func)
|
||||
|
||||
|
||||
def _check_spec(spec: inspect.FullArgSpec, kwargs: dict):
|
||||
|
|
|
|||
|
|
@ -54,9 +54,9 @@ class BaseStorage:
|
|||
if chat is None and user is None:
|
||||
raise ValueError('`user` or `chat` parameter is required but no one is provided!')
|
||||
|
||||
if user is None and chat is not None:
|
||||
if user is None:
|
||||
user = chat
|
||||
elif user is not None and chat is None:
|
||||
elif chat is None:
|
||||
chat = user
|
||||
return chat, user
|
||||
|
||||
|
|
|
|||
|
|
@ -116,8 +116,7 @@ class WebhookRequestHandler(web.View):
|
|||
:return: :class:`aiogram.types.Update`
|
||||
"""
|
||||
data = await self.request.json()
|
||||
update = types.Update(**data)
|
||||
return update
|
||||
return types.Update(**data)
|
||||
|
||||
async def post(self):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -242,8 +242,7 @@ class TelegramObject(ContextInstanceMixin, metaclass=MetaTelegramObject):
|
|||
|
||||
:return:
|
||||
"""
|
||||
for item in self.to_python().items():
|
||||
yield item
|
||||
yield from self.to_python().items()
|
||||
|
||||
def iter_keys(self) -> typing.Generator[typing.Any, None, None]:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -129,18 +129,12 @@ class ListField(Field):
|
|||
super(ListField, self).__init__(*args, default=default, **kwargs)
|
||||
|
||||
def serialize(self, value):
|
||||
result = []
|
||||
serialize = super(ListField, self).serialize
|
||||
for item in value:
|
||||
result.append(serialize(item))
|
||||
return result
|
||||
return [serialize(item) for item in value]
|
||||
|
||||
def deserialize(self, value, parent=None):
|
||||
result = []
|
||||
deserialize = super(ListField, self).deserialize
|
||||
for item in value:
|
||||
result.append(deserialize(item, parent=parent))
|
||||
return result
|
||||
return [deserialize(item, parent=parent) for item in value]
|
||||
|
||||
|
||||
class ListOfLists(Field):
|
||||
|
|
@ -148,9 +142,7 @@ class ListOfLists(Field):
|
|||
result = []
|
||||
serialize = super(ListOfLists, self).serialize
|
||||
for row in value:
|
||||
row_result = []
|
||||
for item in row:
|
||||
row_result.append(serialize(item))
|
||||
row_result = [serialize(item) for item in row]
|
||||
result.append(row_result)
|
||||
return result
|
||||
|
||||
|
|
@ -159,9 +151,7 @@ class ListOfLists(Field):
|
|||
deserialize = super(ListOfLists, self).deserialize
|
||||
if hasattr(value, '__iter__'):
|
||||
for row in value:
|
||||
row_result = []
|
||||
for item in row:
|
||||
row_result.append(deserialize(item, parent=parent))
|
||||
row_result = [deserialize(item, parent=parent) for item in row]
|
||||
result.append(row_result)
|
||||
return result
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ class InlineKeyboardMarkup(base.TelegramObject):
|
|||
if index % self.row_width == 0:
|
||||
self.inline_keyboard.append(row)
|
||||
row = []
|
||||
if len(row) > 0:
|
||||
if row:
|
||||
self.inline_keyboard.append(row)
|
||||
return self
|
||||
|
||||
|
|
@ -62,9 +62,7 @@ class InlineKeyboardMarkup(base.TelegramObject):
|
|||
:return: self
|
||||
:rtype: :obj:`types.InlineKeyboardMarkup`
|
||||
"""
|
||||
btn_array = []
|
||||
for button in args:
|
||||
btn_array.append(button)
|
||||
btn_array = [button for button in args]
|
||||
self.inline_keyboard.append(btn_array)
|
||||
return self
|
||||
|
||||
|
|
|
|||
|
|
@ -39,10 +39,7 @@ class InputFile(base.TelegramObject):
|
|||
self._path = path_or_bytesio
|
||||
if filename is None:
|
||||
filename = os.path.split(path_or_bytesio)[-1]
|
||||
elif isinstance(path_or_bytesio, io.IOBase):
|
||||
self._path = None
|
||||
self._file = path_or_bytesio
|
||||
elif isinstance(path_or_bytesio, _WebPipe):
|
||||
elif isinstance(path_or_bytesio, (io.IOBase, _WebPipe)):
|
||||
self._path = None
|
||||
self._file = path_or_bytesio
|
||||
else:
|
||||
|
|
@ -166,10 +163,7 @@ class _WebPipe:
|
|||
def name(self):
|
||||
if not self._name:
|
||||
*_, part = self.url.rpartition('/')
|
||||
if part:
|
||||
self._name = part
|
||||
else:
|
||||
self._name = secrets.token_urlsafe(24)
|
||||
self._name = part or secrets.token_urlsafe(24)
|
||||
return self._name
|
||||
|
||||
async def open(self):
|
||||
|
|
|
|||
|
|
@ -30,11 +30,7 @@ class MessageEntity(base.TelegramObject):
|
|||
if sys.maxunicode == 0xffff:
|
||||
return text[self.offset:self.offset + self.length]
|
||||
|
||||
if not isinstance(text, bytes):
|
||||
entity_text = text.encode('utf-16-le')
|
||||
else:
|
||||
entity_text = text
|
||||
|
||||
entity_text = text.encode('utf-16-le') if not isinstance(text, bytes) else text
|
||||
entity_text = entity_text[self.offset * 2:(self.offset + self.length) * 2]
|
||||
return entity_text.decode('utf-16-le')
|
||||
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ class ReplyKeyboardMarkup(base.TelegramObject):
|
|||
if index % self.row_width == 0:
|
||||
self.keyboard.append(row)
|
||||
row = []
|
||||
if len(row) > 0:
|
||||
if row:
|
||||
self.keyboard.append(row)
|
||||
return self
|
||||
|
||||
|
|
@ -70,9 +70,7 @@ class ReplyKeyboardMarkup(base.TelegramObject):
|
|||
:return: self
|
||||
:rtype: :obj:`types.ReplyKeyboardMarkup`
|
||||
"""
|
||||
btn_array = []
|
||||
for button in args:
|
||||
btn_array.append(button)
|
||||
btn_array = [button for button in args]
|
||||
self.keyboard.append(btn_array)
|
||||
return self
|
||||
|
||||
|
|
|
|||
|
|
@ -70,10 +70,7 @@ class HelperMode(Helper):
|
|||
return text
|
||||
result = ''
|
||||
for pos, symbol in enumerate(text):
|
||||
if symbol.isupper() and pos > 0:
|
||||
result += '_' + symbol
|
||||
else:
|
||||
result += symbol.upper()
|
||||
result += '_' + symbol if symbol.isupper() and pos > 0 else symbol.upper()
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
|
|
@ -103,10 +100,7 @@ class HelperMode(Helper):
|
|||
if symbol == '_' and pos > 0:
|
||||
need_upper = True
|
||||
else:
|
||||
if need_upper:
|
||||
result += symbol.upper()
|
||||
else:
|
||||
result += symbol.lower()
|
||||
result += symbol.upper() if need_upper else symbol.lower()
|
||||
need_upper = False
|
||||
if first_upper:
|
||||
result = result[0].upper() + result[1:]
|
||||
|
|
@ -153,9 +147,8 @@ class Item:
|
|||
def __set_name__(self, owner, name):
|
||||
if not name.isupper():
|
||||
raise NameError('Name for helper item must be in uppercase!')
|
||||
if not self._value:
|
||||
if hasattr(owner, 'mode'):
|
||||
self._value = HelperMode.apply(name, getattr(owner, 'mode'))
|
||||
if not self._value and hasattr(owner, 'mode'):
|
||||
self._value = HelperMode.apply(name, getattr(owner, 'mode'))
|
||||
|
||||
|
||||
class ListItem(Item):
|
||||
|
|
@ -201,10 +194,15 @@ class OrderedHelperMeta(type):
|
|||
def __new__(mcs, name, bases, namespace, **kwargs):
|
||||
cls = super().__new__(mcs, name, bases, namespace)
|
||||
|
||||
props_keys = []
|
||||
props_keys = [
|
||||
prop_name
|
||||
for prop_name in (
|
||||
name
|
||||
for name, prop in namespace.items()
|
||||
if isinstance(prop, (Item, ListItem))
|
||||
)
|
||||
]
|
||||
|
||||
for prop_name in (name for name, prop in namespace.items() if isinstance(prop, (Item, ListItem))):
|
||||
props_keys.append(prop_name)
|
||||
|
||||
setattr(cls, PROPS_KEYS_ATTR_NAME, props_keys)
|
||||
|
||||
|
|
|
|||
|
|
@ -55,12 +55,12 @@ class TextDecoration(ABC):
|
|||
:param entities: Array of MessageEntities
|
||||
:return:
|
||||
"""
|
||||
result = "".join(
|
||||
return "".join(
|
||||
self._unparse_entities(
|
||||
self._add_surrogates(text), sorted(entities, key=lambda item: item.offset) if entities else []
|
||||
self._add_surrogates(text),
|
||||
sorted(entities, key=lambda item: item.offset) if entities else [],
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
||||
def _unparse_entities(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -492,7 +492,7 @@ async def test_get_my_commands(bot: Bot, event_loop):
|
|||
async with FakeTelegram(message_data=commands, loop=event_loop):
|
||||
result = await bot.get_my_commands()
|
||||
assert isinstance(result, list)
|
||||
assert all([isinstance(command, types.BotCommand) for command in result])
|
||||
assert all(isinstance(command, types.BotCommand) for command in result)
|
||||
|
||||
|
||||
async def test_edit_message_text_by_bot(bot: Bot, event_loop):
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ def test_DeprecatedReadOnlyClassVarCD():
|
|||
assert DeprecatedReadOnlyClassVar.__slots__ == ("_new_value_getter", "_warning_message")
|
||||
|
||||
new_value_of_deprecated_cls_cd = "mpa"
|
||||
pseudo_owner_cls = type("OpekaCla$$", (), {})
|
||||
deprecated_cd = DeprecatedReadOnlyClassVar("mopekaa", lambda owner: new_value_of_deprecated_cls_cd)
|
||||
|
||||
with pytest.warns(DeprecationWarning):
|
||||
pseudo_owner_cls = type("OpekaCla$$", (), {})
|
||||
assert deprecated_cd.__get__(None, pseudo_owner_cls) == new_value_of_deprecated_cls_cd
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue