From affb729615328c78844bba7cc0d80ac2bc68bdd8 Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov Date: Tue, 16 Jul 2024 13:43:08 +0400 Subject: [PATCH 1/5] 1. rewrite initial args in oneline, replacement if else None 2. islice package itrtools more effective works with large keys, yield by one --- mmpy_bot/function.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/mmpy_bot/function.py b/mmpy_bot/function.py index 1149d526..5401a915 100644 --- a/mmpy_bot/function.py +++ b/mmpy_bot/function.py @@ -6,6 +6,7 @@ import re import shlex from abc import ABC, abstractmethod +from itertools import islice from typing import TYPE_CHECKING, Callable, Optional, Sequence, Union import click @@ -85,15 +86,11 @@ def __init__( self.needs_mention = needs_mention self.silence_fail_msg = silence_fail_msg - if allowed_users is None: - self.allowed_users = [] - else: - self.allowed_users = [user.lower() for user in allowed_users] + self.allowed_users = [user.lower() for user in (allowed_users or [])] - if allowed_channels is None: - self.allowed_channels = [] - else: - self.allowed_channels = [channel.lower() for channel in allowed_channels] + self.allowed_channels = [ + channel.lower() for channel in (allowed_channels or []) + ] # Default for non-click functions _function: Union[Callable, click.Command] = self.function @@ -115,7 +112,7 @@ def __init__( if _function is not None: self.name = _function.__qualname__ - argspec = list(inspect.signature(_function).parameters.keys()) + argspec = list(islice(inspect.signature(_function).parameters.keys(), 2)) if not argspec[:2] == ["self", "message"]: raise TypeError( "Any message listener function should at least have the positional" @@ -187,11 +184,9 @@ def listen_to( """Wrap the given function in a MessageFunction class so we can register some properties.""" - if allowed_users is None: - allowed_users = [] + allowed_users = allowed_users or [] - if allowed_channels is None: - allowed_channels = [] + allowed_channels = allowed_users or [] def wrapped_func(func): reg = regexp From 08b573ef52f9ed50d2d4f4c9a4a5b7bfaab21c86 Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov <118372045+ArtemIsmagilov@users.noreply.github.com> Date: Fri, 19 Jul 2024 23:23:26 +0400 Subject: [PATCH 2/5] extra slice great point, thanks for noticing Co-authored-by: Renato Alves --- mmpy_bot/function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmpy_bot/function.py b/mmpy_bot/function.py index 5401a915..28ce0ea0 100644 --- a/mmpy_bot/function.py +++ b/mmpy_bot/function.py @@ -113,7 +113,7 @@ def __init__( self.name = _function.__qualname__ argspec = list(islice(inspect.signature(_function).parameters.keys(), 2)) - if not argspec[:2] == ["self", "message"]: + if not argspec == ["self", "message"]: raise TypeError( "Any message listener function should at least have the positional" f" arguments `self` and `message`, but function {self.name} has" From acba80e45dfc0306d077bfabc4bbbe9e7efa0294 Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov Date: Fri, 19 Jul 2024 23:32:10 +0400 Subject: [PATCH 3/5] fix wrong if Statements --- mmpy_bot/function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmpy_bot/function.py b/mmpy_bot/function.py index 28ce0ea0..a393f2da 100644 --- a/mmpy_bot/function.py +++ b/mmpy_bot/function.py @@ -113,7 +113,7 @@ def __init__( self.name = _function.__qualname__ argspec = list(islice(inspect.signature(_function).parameters.keys(), 2)) - if not argspec == ["self", "message"]: + if argspec != ["self", "message"]: raise TypeError( "Any message listener function should at least have the positional" f" arguments `self` and `message`, but function {self.name} has" From ef935366d3efcf6858ae32d8ef521002d09cd717 Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov Date: Fri, 19 Jul 2024 23:48:42 +0400 Subject: [PATCH 4/5] revert line `islice` --- mmpy_bot/function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmpy_bot/function.py b/mmpy_bot/function.py index a393f2da..737754ef 100644 --- a/mmpy_bot/function.py +++ b/mmpy_bot/function.py @@ -112,7 +112,7 @@ def __init__( if _function is not None: self.name = _function.__qualname__ - argspec = list(islice(inspect.signature(_function).parameters.keys(), 2)) + argspec = list(inspect.signature(_function).parameters)[:2] if argspec != ["self", "message"]: raise TypeError( "Any message listener function should at least have the positional" From 13a18028f0955ce8f91a78f6e8b54a60433d6a53 Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov Date: Fri, 19 Jul 2024 23:49:47 +0400 Subject: [PATCH 5/5] fix check reply --- tests/integration_tests/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests/utils.py b/tests/integration_tests/utils.py index 589bce96..fe8a589c 100644 --- a/tests/integration_tests/utils.py +++ b/tests/integration_tests/utils.py @@ -34,7 +34,7 @@ def expect_reply(driver: Driver, post: Dict, wait=RESPONSE_TIMEOUT, retries=1): reply = thread_info["posts"][reply_id] break - if not reply: + if reply is None: raise ValueError("Expected a response, but didn't get any!") return reply