From d748ec8d522fa8ecdf6bd3142b481fe9abe6a3fd Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 29 Feb 2024 17:56:37 -0800 Subject: [PATCH] ruff: Fix PLW0108 Lambda may be unnecessary. This is a preview rule, not yet enabled by default. Signed-off-by: Anders Kaseorg --- tools/lib/capitalization.py | 2 +- tools/test-backend | 2 +- zerver/lib/bot_lib.py | 4 ++-- zerver/lib/cache_helpers.py | 4 ++-- zerver/lib/test_classes.py | 4 +--- zerver/tests/test_events.py | 8 ++++---- zerver/tests/test_sessions.py | 4 ++-- zerver/tests/test_timeout.py | 2 +- zerver/tornado/application.py | 2 +- zerver/transaction_tests/test_user_groups.py | 2 +- zerver/worker/queue_processors.py | 2 +- 11 files changed, 17 insertions(+), 19 deletions(-) diff --git a/tools/lib/capitalization.py b/tools/lib/capitalization.py index 255f821c5a..3744880548 100644 --- a/tools/lib/capitalization.py +++ b/tools/lib/capitalization.py @@ -156,7 +156,7 @@ IGNORED_PHRASES = [ # Sort regexes in descending order of their lengths. As a result, the # longer phrases will be ignored first. -IGNORED_PHRASES.sort(key=lambda regex: len(regex), reverse=True) +IGNORED_PHRASES.sort(key=len, reverse=True) # Compile regexes to improve performance. This also extracts the # text using BeautifulSoup and then removes extra whitespaces from diff --git a/tools/test-backend b/tools/test-backend index a82c1731e7..02227afb10 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -388,7 +388,7 @@ def main() -> None: if not options.no_cov_cleanup: import atexit - atexit.register(lambda: cov.erase()) # Ensure the data file gets cleaned up at the end. + atexit.register(cov.erase) # Ensure the data file gets cleaned up at the end. cov.start() if options.profile: import cProfile diff --git a/zerver/lib/bot_lib.py b/zerver/lib/bot_lib.py index ae7cebf0e1..6709888741 100644 --- a/zerver/lib/bot_lib.py +++ b/zerver/lib/bot_lib.py @@ -42,8 +42,8 @@ class StateHandler: def __init__(self, user_profile: UserProfile) -> None: self.user_profile = user_profile - self.marshal: Callable[[object], str] = lambda obj: json.dumps(obj) - self.demarshal: Callable[[str], object] = lambda obj: json.loads(obj) + self.marshal: Callable[[object], str] = json.dumps + self.demarshal: Callable[[str], object] = json.loads def get(self, key: str) -> object: return self.demarshal(get_bot_storage(self.user_profile, key)) diff --git a/zerver/lib/cache_helpers.py b/zerver/lib/cache_helpers.py index 7d96cb7ad1..e54b27a4b9 100644 --- a/zerver/lib/cache_helpers.py +++ b/zerver/lib/cache_helpers.py @@ -90,12 +90,12 @@ cache_fillers: Dict[ ] = { "user": (get_users, user_cache_items, 3600 * 24 * 7, 10000), "client": ( - lambda: Client.objects.all(), + Client.objects.all, client_cache_items, 3600 * 24 * 7, 10000, ), - "session": (lambda: Session.objects.all(), session_cache_items, 3600 * 24 * 7, 10000), + "session": (Session.objects.all, session_cache_items, 3600 * 24 * 7, 10000), } diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 922ca64665..86eeea509d 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -1971,9 +1971,7 @@ class ZulipTestCase(ZulipTestCaseMixin, TestCase): # Some code might call process_notification using keyword arguments, # so mypy doesn't allow assigning lst.append to process_notification # So explicitly change parameter name to 'notice' to work around this problem - with mock.patch( - "zerver.tornado.event_queue.process_notification", lambda notice: lst.append(notice) - ): + with mock.patch("zerver.tornado.event_queue.process_notification", lst.append): # Some `send_event` calls need to be executed only after the current transaction # commits (using `on_commit` hooks). Because the transaction in Django tests never # commits (rather, gets rolled back after the test completes), such events would diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index fcf407abf0..7b57a796c9 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -3345,7 +3345,7 @@ class NormalActionsTest(BaseAction): base = "/user_uploads/" self.assertEqual(base, url[: len(base)]) - events = self.verify_action(lambda: do_upload(), num_events=1, state_change_expected=False) + events = self.verify_action(do_upload, num_events=1, state_change_expected=False) check_attachment_add("events[0]", events[0]) self.assertEqual(events[0]["upload_space_used"], 6) @@ -3481,21 +3481,21 @@ class NormalActionsTest(BaseAction): def test_restart_event(self) -> None: self.verify_action( - lambda: send_restart_events(), + send_restart_events, num_events=1, state_change_expected=False, ) def test_web_reload_client_event(self) -> None: self.verify_action( - lambda: send_web_reload_client_events(), + send_web_reload_client_events, client_is_old=False, num_events=0, state_change_expected=False, ) with self.assertLogs(level="WARNING") as logs: self.verify_action( - lambda: send_web_reload_client_events(), + send_web_reload_client_events, client_is_old=True, num_events=1, state_change_expected=False, diff --git a/zerver/tests/test_sessions.py b/zerver/tests/test_sessions.py index 1d77a28a27..6c371b7ed1 100644 --- a/zerver/tests/test_sessions.py +++ b/zerver/tests/test_sessions.py @@ -76,7 +76,7 @@ class TestSessions(ZulipTestCase): def test_delete_all_user_sessions(self) -> None: self.do_test_session( self.example_user("hamlet"), - lambda: delete_all_user_sessions(), + delete_all_user_sessions, get_realm("zulip"), True, ) @@ -90,7 +90,7 @@ class TestSessions(ZulipTestCase): ) self.do_test_session( self.lear_user("cordelia"), - lambda: delete_all_user_sessions(), + delete_all_user_sessions, lear_realm, True, ) diff --git a/zerver/tests/test_timeout.py b/zerver/tests/test_timeout.py index 945aa37a98..b03f94ab62 100644 --- a/zerver/tests/test_timeout.py +++ b/zerver/tests/test_timeout.py @@ -34,7 +34,7 @@ class TimeoutTestCase(ZulipTestCase): def test_timeout_raises(self) -> None: try: - timeout(1, lambda: self.something_exceptional()) + timeout(1, self.something_exceptional) raise AssertionError("Failed to raise an exception") except ValueError as exc: tb = traceback.format_tb(exc.__traceback__) diff --git a/zerver/tornado/application.py b/zerver/tornado/application.py index 5d9dc392aa..e180ab150d 100644 --- a/zerver/tornado/application.py +++ b/zerver/tornado/application.py @@ -9,7 +9,7 @@ from zerver.tornado.handlers import AsyncDjangoHandler def setup_tornado_rabbitmq(queue_client: TornadoQueueClient) -> None: # nocoverage # When tornado is shut down, disconnect cleanly from RabbitMQ - autoreload.add_reload_hook(lambda: queue_client.close()) + autoreload.add_reload_hook(queue_client.close) def create_tornado_application(*, autoreload: bool = False) -> tornado.web.Application: diff --git a/zerver/transaction_tests/test_user_groups.py b/zerver/transaction_tests/test_user_groups.py index 0a5b639e38..7655530047 100644 --- a/zerver/transaction_tests/test_user_groups.py +++ b/zerver/transaction_tests/test_user_groups.py @@ -73,7 +73,7 @@ class UserGroupRaceConditionTestCase(ZulipTransactionTestCase): with transaction.atomic(): for group in self.created_user_groups: group.delete() - transaction.on_commit(lambda: self.created_user_groups.clear()) + transaction.on_commit(self.created_user_groups.clear) super().tearDown() diff --git a/zerver/worker/queue_processors.py b/zerver/worker/queue_processors.py index 70f4bb47d9..87abe91cae 100644 --- a/zerver/worker/queue_processors.py +++ b/zerver/worker/queue_processors.py @@ -683,7 +683,7 @@ class MissedMessageWorker(QueueProcessingWorker): def start(self) -> None: with self.cv: self.stopping = False - self.worker_thread = threading.Thread(target=lambda: self.work()) + self.worker_thread = threading.Thread(target=self.work) self.worker_thread.start() super().start()