typing: Add none-checks for miscellaneous cases.

Signed-off-by: Zixuan James Li <359101898@qq.com>
This commit is contained in:
Zixuan James Li 2022-05-30 19:32:29 -04:00 committed by Tim Abbott
parent e338ada66c
commit 4a5043dd6e
7 changed files with 19 additions and 9 deletions

View File

@ -82,6 +82,7 @@ def handle_checkout_session_completed_event(
]:
ensure_realm_does_not_have_active_plan(user.realm)
update_or_create_stripe_customer(user, payment_method)
assert session.payment_intent is not None
session.payment_intent.status = PaymentIntent.PROCESSING
session.payment_intent.last_payment_error = ()
session.payment_intent.save(update_fields=["status", "last_payment_error"])

View File

@ -40,11 +40,12 @@ def do_change_bot_owner(
user_id=user_profile.id,
),
)
previous_owner_id = previous_owner.id
transaction.on_commit(
lambda: send_event(
user_profile.realm,
delete_event,
{previous_owner.id},
{previous_owner_id},
)
)
# Do not send update event for previous bot owner.

View File

@ -23,7 +23,9 @@ session_engine = cast(SessionEngine, import_module(settings.SESSION_ENGINE))
def get_session_dict_user(session_dict: Mapping[str, int]) -> Optional[int]:
# Compare django.contrib.auth._get_user_session_key
try:
return get_user_model()._meta.pk.to_python(session_dict[SESSION_KEY])
pk = get_user_model()._meta.pk
assert pk is not None
return pk.to_python(session_dict[SESSION_KEY])
except KeyError:
return None

View File

@ -237,6 +237,7 @@ def init_worker(counter: "multiprocessing.sharedctypes.Synchronized[int]") -> No
from zproject.dev_urls import avatars_url
assert settings.LOCAL_UPLOADS_DIR is not None
assert avatars_url.default_args is not None
new_root = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars")
avatars_url.default_args["document_root"] = new_root

View File

@ -53,6 +53,7 @@ from zerver.lib.stream_traffic import get_streams_traffic
from zerver.lib.streams import create_stream_if_needed
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.types import RealmPlaygroundDict
from zerver.lib.utils import assert_is_not_none
from zerver.models import (
Message,
Realm,
@ -120,7 +121,7 @@ class TestRealmAuditLog(ZulipTestCase):
event_time__gte=now,
event_time__lte=now + timedelta(minutes=60),
):
extra_data = orjson.loads(event.extra_data)
extra_data = orjson.loads(assert_is_not_none(event.extra_data))
self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
self.assertNotIn(RealmAuditLog.OLD_VALUE, extra_data)
@ -149,7 +150,7 @@ class TestRealmAuditLog(ZulipTestCase):
event_time__gte=now,
event_time__lte=now + timedelta(minutes=60),
):
extra_data = orjson.loads(event.extra_data)
extra_data = orjson.loads(assert_is_not_none(event.extra_data))
self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
self.assertIn(RealmAuditLog.OLD_VALUE, extra_data)
self.assertIn(RealmAuditLog.NEW_VALUE, extra_data)
@ -332,14 +333,14 @@ class TestRealmAuditLog(ZulipTestCase):
log_entry = RealmAuditLog.objects.get(
realm=realm, event_type=RealmAuditLog.REALM_DEACTIVATED, acting_user=user
)
extra_data = orjson.loads(log_entry.extra_data)
extra_data = orjson.loads(assert_is_not_none(log_entry.extra_data))
self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
do_reactivate_realm(realm)
log_entry = RealmAuditLog.objects.get(
realm=realm, event_type=RealmAuditLog.REALM_REACTIVATED
)
extra_data = orjson.loads(log_entry.extra_data)
extra_data = orjson.loads(assert_is_not_none(log_entry.extra_data))
self.check_role_count_schema(extra_data[RealmAuditLog.ROLE_COUNT])
def test_create_stream_if_needed(self) -> None:
@ -407,7 +408,7 @@ class TestRealmAuditLog(ZulipTestCase):
acting_user=user,
)
self.assertEqual(realm_audit_logs.count(), 1)
extra_data = orjson.loads(realm_audit_logs[0].extra_data)
extra_data = orjson.loads(assert_is_not_none(realm_audit_logs[0].extra_data))
expected_new_value = auth_method_dict
self.assertEqual(extra_data[RealmAuditLog.OLD_VALUE], expected_old_value)
self.assertEqual(extra_data[RealmAuditLog.NEW_VALUE], expected_new_value)
@ -449,7 +450,8 @@ class TestRealmAuditLog(ZulipTestCase):
).order_by("id")
self.assertEqual(realm_audit_logs.count(), 2)
self.assertEqual(
[orjson.loads(entry.extra_data) for entry in realm_audit_logs], values_expected
[orjson.loads(assert_is_not_none(entry.extra_data)) for entry in realm_audit_logs],
values_expected,
)
def test_set_realm_notifications_stream(self) -> None:

View File

@ -39,6 +39,7 @@ def accounts_accept_terms(request: HttpRequest) -> HttpResponse:
if request.method == "POST":
form = ToSForm(request.POST)
if form.is_valid():
assert settings.TERMS_OF_SERVICE_VERSION is not None
do_change_tos_version(request.user, settings.TERMS_OF_SERVICE_VERSION)
return redirect(home)
else:
@ -81,10 +82,11 @@ def detect_narrowed_window(
narrow_stream = None
narrow_topic = request.GET.get("topic")
if request.GET.get("stream"):
if "stream" in request.GET:
try:
# TODO: We should support stream IDs and PMs here as well.
narrow_stream_name = request.GET.get("stream")
assert narrow_stream_name is not None
(narrow_stream, ignored_sub) = access_stream_by_name(user_profile, narrow_stream_name)
narrow = [["stream", narrow_stream.name]]
except Exception:

View File

@ -570,6 +570,7 @@ class LDAPReverseEmailSearch(_LDAPUser):
search = settings.AUTH_LDAP_REVERSE_EMAIL_SEARCH
USERNAME_ATTR = settings.AUTH_LDAP_USERNAME_ATTR
assert search is not None
results = search.execute(self.connection, {"email": email})
ldap_users = []