python: Use Python 3.8 typing.{Protocol,TypedDict}.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-04-26 17:23:56 -07:00 committed by Tim Abbott
parent 098a514599
commit a2825e5984
26 changed files with 53 additions and 46 deletions

View File

@ -4,7 +4,7 @@ __revision__ = "$Id: models.py 28 2009-10-22 15:03:02Z jarek.zgoda $"
import datetime import datetime
import secrets import secrets
from base64 import b32encode from base64 import b32encode
from typing import List, Mapping, Optional, Union from typing import List, Mapping, Optional, Protocol, Union
from urllib.parse import urljoin from urllib.parse import urljoin
from django.conf import settings from django.conf import settings
@ -16,7 +16,6 @@ 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.lib.types import UnspecifiedValue from zerver.lib.types import UnspecifiedValue
from zerver.models import EmailChangeStatus, MultiuseInvite, PreregistrationUser, Realm, UserProfile from zerver.models import EmailChangeStatus, MultiuseInvite, PreregistrationUser, Realm, UserProfile

View File

@ -5,7 +5,7 @@
# Django itself # Django itself
Django[argon2]==3.2.* Django[argon2]==3.2.*
# needed for Literal, TypedDict # needed for NotRequired, ParamSpec
typing-extensions typing-extensions
# Needed for rendering backend templates # Needed for rendering backend templates

View File

@ -18,9 +18,10 @@ from scripts.lib.setup_path import setup_path
setup_path() setup_path()
os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.settings" os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.settings"
from typing import TypedDict
import django import django
from django.conf import settings from django.conf import settings
from typing_extensions import TypedDict
from urllib3.util import Retry from urllib3.util import Retry
django.setup() django.setup()

View File

@ -1,5 +1,5 @@
import datetime import datetime
from typing import Any, Dict, Iterable, List, Optional, Set from typing import Any, Dict, Iterable, List, Optional, Set, TypedDict
from django.conf import settings from django.conf import settings
from django.db import transaction from django.db import transaction
@ -7,7 +7,6 @@ from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy from django.utils.translation import gettext_lazy
from django.utils.translation import override as override_language from django.utils.translation import override as override_language
from typing_extensions import TypedDict
from zerver.actions.message_flags import do_update_mobile_push_notification from zerver.actions.message_flags import do_update_mobile_push_notification
from zerver.actions.message_send import ( from zerver.actions.message_send import (

View File

@ -12,6 +12,7 @@ from typing import (
Sequence, Sequence,
Set, Set,
Tuple, Tuple,
TypedDict,
Union, Union,
) )
@ -24,7 +25,6 @@ from django.utils.html import escape
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.utils.translation import override as override_language from django.utils.translation import override as override_language
from typing_extensions import TypedDict
from zerver.actions.uploads import do_claim_attachments from zerver.actions.uploads import do_claim_attachments
from zerver.lib.addressee import Addressee from zerver.lib.addressee import Addressee

View File

@ -4,12 +4,23 @@ import os
import random import random
import shutil import shutil
from functools import partial from functools import partial
from typing import AbstractSet, Any, Callable, Dict, Iterable, List, Optional, Set, Tuple, TypeVar from typing import (
AbstractSet,
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Protocol,
Set,
Tuple,
TypeVar,
)
import orjson import orjson
import requests import requests
from django.forms.models import model_to_dict from django.forms.models import model_to_dict
from typing_extensions import Protocol
from zerver.data_import.sequencer import NEXT_ID from zerver.data_import.sequencer import NEXT_ID
from zerver.lib.avatar_hash import user_avatar_path_from_ids from zerver.lib.avatar_hash import user_avatar_path_from_ids

View File

@ -1,6 +1,4 @@
from typing import Dict, List, Optional, Set, Tuple from typing import Dict, List, Optional, Set, Tuple, TypedDict
from typing_extensions import TypedDict
from zerver.lib.cache import ( from zerver.lib.cache import (
bulk_cached_fetch, bulk_cached_fetch,

View File

@ -14,7 +14,7 @@ import shutil
import subprocess import subprocess
import tempfile import tempfile
from functools import lru_cache from functools import lru_cache
from typing import Any, Callable, Dict, List, Optional, Set, Tuple from typing import Any, Callable, Dict, List, Optional, Set, Tuple, TypedDict
import orjson import orjson
from django.apps import apps from django.apps import apps
@ -24,7 +24,6 @@ from django.forms.models import model_to_dict
from django.utils.timezone import is_naive as timezone_is_naive from django.utils.timezone import is_naive as timezone_is_naive
from django.utils.timezone import make_aware as timezone_make_aware from django.utils.timezone import make_aware as timezone_make_aware
from mypy_boto3_s3.service_resource import Object from mypy_boto3_s3.service_resource import Object
from typing_extensions import TypedDict
import zerver.lib.upload import zerver.lib.upload
from analytics.models import RealmCount, StreamCount, UserCount from analytics.models import RealmCount, StreamCount, UserCount

View File

@ -21,6 +21,7 @@ from typing import (
Pattern, Pattern,
Set, Set,
Tuple, Tuple,
TypedDict,
TypeVar, TypeVar,
Union, Union,
) )
@ -45,7 +46,6 @@ from markdown.blockparser import BlockParser
from markdown.extensions import codehilite, nl2br, sane_lists, tables from markdown.extensions import codehilite, nl2br, sane_lists, tables
from soupsieve import escape as css_escape from soupsieve import escape as css_escape
from tlds import tld_set from tlds import tld_set
from typing_extensions import TypedDict
from zerver.lib import mention as mention from zerver.lib import mention as mention
from zerver.lib.cache import cache_with_key from zerver.lib.cache import cache_with_key

View File

@ -2,7 +2,7 @@ import copy
import datetime import datetime
import zlib import zlib
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Any, Dict, List, Mapping, Optional, Sequence, Set, Tuple, Union from typing import Any, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypedDict, Union
import ahocorasick import ahocorasick
import orjson import orjson
@ -12,7 +12,6 @@ from django.db.models import Max, Sum
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from psycopg2.sql import SQL from psycopg2.sql import SQL
from typing_extensions import TypedDict
from analytics.lib.counts import COUNT_STATS from analytics.lib.counts import COUNT_STATS
from analytics.models import RealmCount from analytics.models import RealmCount

View File

@ -10,6 +10,7 @@ from typing import (
Dict, Dict,
Generic, Generic,
List, List,
Literal,
MutableMapping, MutableMapping,
Optional, Optional,
Sequence, Sequence,
@ -24,7 +25,6 @@ import orjson
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from typing_extensions import Literal
import zerver.lib.rate_limiter as rate_limiter import zerver.lib.rate_limiter as rate_limiter
import zerver.tornado.handlers as handlers import zerver.tornado.handlers as handlers

View File

@ -1,14 +1,13 @@
import logging import logging
from datetime import timedelta from datetime import timedelta
from importlib import import_module from importlib import import_module
from typing import Any, List, Mapping, Optional, Type, cast from typing import Any, List, Mapping, Optional, Protocol, Type, cast
from django.conf import settings from django.conf import settings
from django.contrib.auth import SESSION_KEY, get_user_model from django.contrib.auth import SESSION_KEY, get_user_model
from django.contrib.sessions.backends.base import SessionBase from django.contrib.sessions.backends.base import SessionBase
from django.contrib.sessions.models import Session from django.contrib.sessions.models import Session
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.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime
from zerver.models import Realm, UserProfile, get_user_profile_by_id from zerver.models import Realm, UserProfile, get_user_profile_by_id

View File

@ -1,11 +1,10 @@
from typing import Any, Collection, Dict, List, Optional, Set, Tuple, Union from typing import Any, Collection, Dict, List, Optional, Set, Tuple, TypedDict, Union
from django.db import transaction from django.db import transaction
from django.db.models import Exists, OuterRef, Q from django.db.models import Exists, OuterRef, Q
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from typing_extensions import TypedDict
from zerver.actions.default_streams import get_default_streams_for_realm from zerver.actions.default_streams import get_default_streams_for_realm
from zerver.lib.exceptions import ( from zerver.lib.exceptions import (

View File

@ -1,10 +1,10 @@
import datetime import datetime
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union from typing import Any, Callable, Dict, List, Optional, Tuple, TypedDict, TypeVar, Union
from django.http import HttpResponse from django.http import HttpResponse
from django.utils.functional import Promise from django.utils.functional import Promise
from typing_extensions import NotRequired, TypedDict from typing_extensions import NotRequired
ViewFuncT = TypeVar("ViewFuncT", bound=Callable[..., HttpResponse]) ViewFuncT = TypeVar("ViewFuncT", bound=Callable[..., HttpResponse])

View File

@ -1,7 +1,5 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional from typing import Literal, Optional
from typing_extensions import Literal
@dataclass @dataclass

View File

@ -1,7 +1,7 @@
import re import re
import unicodedata import unicodedata
from collections import defaultdict from collections import defaultdict
from typing import Any, Dict, List, Optional, Sequence, Union, cast from typing import Any, Dict, List, Optional, Sequence, TypedDict, Union, cast
import dateutil.parser as date_parser import dateutil.parser as date_parser
from django.conf import settings from django.conf import settings
@ -9,7 +9,6 @@ from django.core.exceptions import ValidationError
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.forms.models import model_to_dict from django.forms.models import model_to_dict
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from typing_extensions import TypedDict
from zulip_bots.custom_exceptions import ConfigValidationError from zulip_bots.custom_exceptions import ConfigValidationError
from zerver.lib.avatar import avatar_url, get_avatar_field from zerver.lib.avatar import avatar_url, get_avatar_field

View File

@ -4,7 +4,7 @@ import os
import platform import platform
import subprocess import subprocess
import traceback import traceback
from typing import Any, Dict, Optional from typing import Any, Dict, Optional, Protocol, runtime_checkable
from urllib.parse import SplitResult from urllib.parse import SplitResult
from django.conf import settings from django.conf import settings
@ -12,7 +12,6 @@ from django.http import HttpRequest
from django.utils.translation import override as override_language from django.utils.translation import override as override_language
from django.views.debug import get_exception_reporter_filter from django.views.debug import get_exception_reporter_filter
from sentry_sdk import capture_exception from sentry_sdk import capture_exception
from typing_extensions import Protocol, runtime_checkable
from version import ZULIP_VERSION from version import ZULIP_VERSION
from zerver.lib.logging_util import find_log_caller_module from zerver.lib.logging_util import find_log_caller_module

View File

@ -1,12 +1,11 @@
import time import time
from typing import List, Optional from typing import List, Optional, TypedDict
import orjson import orjson
from django.db import migrations, transaction from django.db import migrations, transaction
from django.db.backends.postgresql.schema import DatabaseSchemaEditor from django.db.backends.postgresql.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps from django.db.migrations.state import StateApps
from django.db.models import Min, Model from django.db.models import Min, Model
from typing_extensions import TypedDict
BATCH_SIZE = 10000 BATCH_SIZE = 10000
STREAM = 2 STREAM = 2

View File

@ -14,6 +14,7 @@ from typing import (
Sequence, Sequence,
Set, Set,
Tuple, Tuple,
TypedDict,
TypeVar, TypeVar,
Union, Union,
) )
@ -48,7 +49,6 @@ from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy from django.utils.translation import gettext_lazy
from django_cte import CTEManager from django_cte import CTEManager
from typing_extensions import TypedDict
from confirmation import settings as confirmation_settings from confirmation import settings as confirmation_settings
from zerver.lib import cache from zerver.lib import cache

View File

@ -28,6 +28,7 @@ from typing import (
Sequence, Sequence,
Set, Set,
Tuple, Tuple,
TypedDict,
Union, Union,
cast, cast,
) )
@ -36,7 +37,6 @@ import orjson
import tornado.ioloop import tornado.ioloop
from django.conf import settings from django.conf import settings
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from typing_extensions import TypedDict
from version import API_FEATURE_LEVEL, ZULIP_MERGE_BASE, ZULIP_VERSION from version import API_FEATURE_LEVEL, ZULIP_MERGE_BASE, ZULIP_VERSION
from zerver.lib.exceptions import JsonableError from zerver.lib.exceptions import JsonableError

View File

@ -2,10 +2,9 @@
import re import re
import string import string
from functools import partial from functools import partial
from typing import Dict, List, Optional from typing import Dict, List, Optional, Protocol
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from typing_extensions import Protocol
from zerver.decorator import log_unsupported_webhook_event, webhook_view from zerver.decorator import log_unsupported_webhook_event, webhook_view
from zerver.lib.exceptions import UnsupportedWebhookEventType from zerver.lib.exceptions import UnsupportedWebhookEventType

View File

@ -1,9 +1,8 @@
import string import string
from functools import partial from functools import partial
from typing import Dict, List, Optional from typing import Dict, List, Optional, Protocol
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from typing_extensions import Protocol
from zerver.decorator import webhook_view from zerver.decorator import webhook_view
from zerver.lib.exceptions import UnsupportedWebhookEventType from zerver.lib.exceptions import UnsupportedWebhookEventType

View File

@ -1,8 +1,7 @@
# vim:fenc=utf-8 # vim:fenc=utf-8
from typing import Any, Dict, Optional from typing import Any, Dict, Optional, Protocol
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from typing_extensions import Protocol
from zerver.decorator import webhook_view from zerver.decorator import webhook_view
from zerver.lib.exceptions import UnsupportedWebhookEventType from zerver.lib.exceptions import UnsupportedWebhookEventType

View File

@ -1,7 +1,7 @@
from datetime import datetime from datetime import datetime
from typing import Protocol
from django.core.management.commands.runserver import Command as DjangoCommand from django.core.management.commands.runserver import Command as DjangoCommand
from typing_extensions import Protocol
class Writable(Protocol): class Writable(Protocol):

View File

@ -16,7 +16,20 @@ import binascii
import json import json
import logging import logging
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, TypeVar, Union, cast from typing import (
Any,
Callable,
Dict,
List,
Optional,
Set,
Tuple,
Type,
TypedDict,
TypeVar,
Union,
cast,
)
from urllib.parse import urlencode from urllib.parse import urlencode
import magic import magic
@ -55,7 +68,6 @@ from social_core.exceptions import (
SocialAuthBaseException, SocialAuthBaseException,
) )
from social_core.pipeline.partial import partial from social_core.pipeline.partial import partial
from typing_extensions import TypedDict
from zxcvbn import zxcvbn from zxcvbn import zxcvbn
from zerver.actions.create_user import do_create_user, do_reactivate_user from zerver.actions.create_user import do_create_user, do_reactivate_user

View File

@ -1,5 +1,5 @@
import os import os
from typing import TYPE_CHECKING, Any, Dict, List, Optional from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypedDict
from scripts.lib.zulip_tools import deport from scripts.lib.zulip_tools import deport
@ -7,7 +7,6 @@ from .config import DEVELOPMENT, PRODUCTION, get_secret
if TYPE_CHECKING: if TYPE_CHECKING:
from django_auth_ldap.config import LDAPSearch from django_auth_ldap.config import LDAPSearch
from typing_extensions import TypedDict
from zerver.lib.types import SAMLIdPConfigDict from zerver.lib.types import SAMLIdPConfigDict