confirmation: Use the correct type hints for create_confirmation_link.

Previously we annotate the first argument as `ContentType`, which
is wrong as suggested by django-stubs.
This commit is contained in:
PIG208
2021-07-25 01:40:01 +08:00
committed by Tim Abbott
parent beadb5ec7f
commit cf8687662f
2 changed files with 16 additions and 5 deletions

View File

@@ -16,10 +16,19 @@ from django.http import HttpRequest, HttpResponse
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse from django.urls import reverse
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from typing_extensions import Protocol
from zerver.models import EmailChangeStatus, MultiuseInvite, PreregistrationUser, Realm, UserProfile from zerver.models import EmailChangeStatus, MultiuseInvite, PreregistrationUser, Realm, UserProfile
class HasRealmObject(Protocol):
realm: Realm
class OptionalHasRealmObject(Protocol):
realm: Optional[Realm]
class ConfirmationKeyException(Exception): class ConfirmationKeyException(Exception):
WRONG_LENGTH = 1 WRONG_LENGTH = 1
EXPIRED = 2 EXPIRED = 2
@@ -74,14 +83,16 @@ def get_object_from_key(
def create_confirmation_link( def create_confirmation_link(
obj: ContentType, confirmation_type: int, url_args: Mapping[str, str] = {} obj: Union[Realm, HasRealmObject, OptionalHasRealmObject],
confirmation_type: int,
url_args: Mapping[str, str] = {},
) -> str: ) -> str:
key = generate_key() key = generate_key()
realm = None realm = None
if hasattr(obj, "realm"): if isinstance(obj, Realm):
realm = obj.realm
elif isinstance(obj, Realm):
realm = obj realm = obj
elif hasattr(obj, "realm"):
realm = obj.realm
Confirmation.objects.create( Confirmation.objects.create(
content_object=obj, content_object=obj,

View File

@@ -112,7 +112,7 @@ def create_preregistration_user(
password_required: bool = True, password_required: bool = True,
full_name: Optional[str] = None, full_name: Optional[str] = None,
full_name_validated: bool = False, full_name_validated: bool = False,
) -> HttpResponse: ) -> PreregistrationUser:
realm = None realm = None
if not realm_creation: if not realm_creation:
try: try: