From 82c08dd153a658eb1d497bf9338b656735ecd7cf Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Tue, 28 Nov 2023 18:33:10 +0000 Subject: [PATCH] python: Prevent bare timedelta(), which defaults to days. --- tools/semgrep-py.yml | 16 ++++++++++++++++ zerver/lib/test_classes.py | 2 +- zerver/tests/test_retention.py | 12 +++++++----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/tools/semgrep-py.yml b/tools/semgrep-py.yml index ccd8c849bd..822a1ee6a5 100644 --- a/tools/semgrep-py.yml +++ b/tools/semgrep-py.yml @@ -266,3 +266,19 @@ rules: message: "Replace functools.partial with returns.curry.partial for type safety" languages: [python] severity: ERROR + + - id: timedelta-positional-argument + patterns: + - pattern: timedelta(...) + - pattern-not: timedelta(0) + - pattern-not: timedelta(..., days=..., ...) + - pattern-not: timedelta(..., seconds=..., ...) + - pattern-not: timedelta(..., microseconds=..., ...) + - pattern-not: timedelta(..., milliseconds=..., ...) + - pattern-not: timedelta(..., minutes=..., ...) + - pattern-not: timedelta(..., hours=..., ...) + - pattern-not: timedelta(..., weeks=..., ...) + message: | + Specify timedelta with named arguments. + languages: [python] + severity: ERROR diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index a39366191b..14bc7379af 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -1709,7 +1709,7 @@ Output: def set_age(user_name: str, age: int) -> None: user = self.example_user(user_name) - user.date_joined = timezone_now() - timedelta(age) + user.date_joined = timezone_now() - timedelta(days=age) user.save() do_set_realm_property(realm, "waiting_period_threshold", 1000, acting_user=None) diff --git a/zerver/tests/test_retention.py b/zerver/tests/test_retention.py index 424e19dd52..6523897d37 100644 --- a/zerver/tests/test_retention.py +++ b/zerver/tests/test_retention.py @@ -172,7 +172,7 @@ class ArchiveMessagesTestingBase(RetentionTestingBase): ) self._change_messages_date_sent( msg_ids, - timezone_now() - timedelta(ZULIP_REALM_DAYS + 1), + timezone_now() - timedelta(days=ZULIP_REALM_DAYS + 1), ) return msg_ids @@ -240,7 +240,7 @@ class TestArchiveMessagesGeneral(ArchiveMessagesTestingBase): ) self._change_messages_date_sent( expired_zulip_msg_ids, - timezone_now() - timedelta(ZULIP_REALM_DAYS + 1), + timezone_now() - timedelta(days=ZULIP_REALM_DAYS + 1), ) expired_msg_ids = expired_mit_msg_ids + expired_zulip_msg_ids @@ -273,7 +273,7 @@ class TestArchiveMessagesGeneral(ArchiveMessagesTestingBase): ) self._change_messages_date_sent( zulip_msg_ids, - timezone_now() - timedelta(ZULIP_REALM_DAYS + 1), + timezone_now() - timedelta(days=ZULIP_REALM_DAYS + 1), ) # Only MIT has a retention policy: @@ -323,7 +323,9 @@ class TestArchiveMessagesGeneral(ArchiveMessagesTestingBase): msg_ids += [self._send_personal_message_to_cross_realm_bot() for i in range(1, 7)] usermsg_ids = self._get_usermessage_ids(msg_ids) # Make the message expired in the Zulip realm.: - self._change_messages_date_sent(msg_ids, timezone_now() - timedelta(ZULIP_REALM_DAYS + 1)) + self._change_messages_date_sent( + msg_ids, timezone_now() - timedelta(days=ZULIP_REALM_DAYS + 1) + ) archive_messages() self._verify_archive_data(msg_ids, usermsg_ids) @@ -376,7 +378,7 @@ class TestArchiveMessagesGeneral(ArchiveMessagesTestingBase): # Make the message expired in the recipient's realm: self._change_messages_date_sent( [expired_crossrealm_msg_id], - timezone_now() - timedelta(ZULIP_REALM_DAYS + 1), + timezone_now() - timedelta(days=ZULIP_REALM_DAYS + 1), ) expired_msg_ids = [*expired_mit_msg_ids, *expired_zulip_msg_ids, expired_crossrealm_msg_id]