python: Prevent bare timedelta(), which defaults to days.

This commit is contained in:
Alex Vandiver 2023-11-28 18:33:10 +00:00 committed by Tim Abbott
parent 0f132cef4d
commit 82c08dd153
3 changed files with 24 additions and 6 deletions

View File

@ -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

View File

@ -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)

View File

@ -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]