From f0ecb935155f9d703efaa5a8b4c28353cf25ea82 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 2 Feb 2019 14:53:55 -0800 Subject: [PATCH] zerver core: Remove unused imports. Signed-off-by: Anders Kaseorg --- zerver/context_processors.py | 2 +- zerver/decorator.py | 6 ++---- zerver/forms.py | 6 ++---- zerver/lib/actions.py | 21 +++++++++------------ zerver/lib/addressee.py | 2 -- zerver/lib/api_test_helpers.py | 1 - zerver/lib/bot_lib.py | 13 ++----------- zerver/lib/bulk_create.py | 6 +++--- zerver/lib/cache.py | 3 +-- zerver/lib/cache_helpers.py | 2 +- zerver/lib/create_user.py | 3 --- zerver/lib/digest.py | 5 ++--- zerver/lib/email_mirror.py | 5 ++--- zerver/lib/error_notify.py | 4 +--- zerver/lib/events.py | 5 +---- zerver/lib/exceptions.py | 2 +- zerver/lib/feedback.py | 3 +-- zerver/lib/generate_test_data.py | 2 +- zerver/lib/integrations.py | 6 +----- zerver/lib/json_encoder_for_html.py | 2 +- zerver/lib/management.py | 1 - zerver/lib/mdiff.py | 1 - zerver/lib/message.py | 1 - zerver/lib/migrate.py | 4 +--- zerver/lib/notifications.py | 13 ++++--------- zerver/lib/onboarding.py | 6 +++--- zerver/lib/outgoing_webhook.py | 10 ++-------- zerver/lib/push_notifications.py | 19 ++++++------------- zerver/lib/rate_limiter.py | 3 +-- zerver/lib/realm_icon.py | 2 +- zerver/lib/retention.py | 1 - zerver/lib/send_email.py | 2 +- zerver/lib/soft_deactivation.py | 2 +- zerver/lib/statistics.py | 5 +---- zerver/lib/stream_topic.py | 2 +- zerver/lib/streams.py | 1 - zerver/lib/test_classes.py | 8 ++++---- zerver/lib/test_helpers.py | 28 ++++------------------------ zerver/lib/test_runner.py | 9 +++------ zerver/lib/upload.py | 5 ++--- zerver/lib/user_groups.py | 3 +-- zerver/lib/users.py | 3 +-- zerver/lib/utils.py | 2 -- zerver/lib/validator.py | 3 +-- zerver/lib/webhooks/common.py | 3 +-- zerver/lib/widget.py | 3 +-- zerver/logging_handlers.py | 5 +---- zerver/middleware.py | 4 ++-- zerver/models.py | 15 +++++---------- zerver/signals.py | 4 +--- zerver/templatetags/app_filters.py | 5 +---- zerver/worker/queue_processors.py | 9 +++------ 52 files changed, 85 insertions(+), 196 deletions(-) diff --git a/zerver/context_processors.py b/zerver/context_processors.py index 50e4e7109c..6cc89d1d6d 100644 --- a/zerver/context_processors.py +++ b/zerver/context_processors.py @@ -1,5 +1,5 @@ -from typing import Any, Dict, List, Optional +from typing import Any, Dict, Optional from django.http import HttpRequest from django.conf import settings diff --git a/zerver/decorator.py b/zerver/decorator.py index 27cf9fee60..daf60c7d46 100644 --- a/zerver/decorator.py +++ b/zerver/decorator.py @@ -2,7 +2,6 @@ import django_otp from two_factor.utils import default_device from django_otp import user_has_device, _user_is_authenticated -from django_otp.conf import settings as otp_settings from django.contrib.auth.decorators import user_passes_test as django_user_passes_test from django.contrib.auth.models import AnonymousUser @@ -29,8 +28,7 @@ from zerver.lib.types import ViewFuncT from zerver.lib.rate_limiter import incr_ratelimit, is_ratelimited, \ api_calls_left, RateLimitedUser, RateLimiterLockingException -from zerver.lib.request import REQ, has_request_variables, JsonableError, RequestVariableMissingError -from django.core.handlers import base +from zerver.lib.request import REQ, has_request_variables, RequestVariableMissingError from functools import wraps import base64 @@ -40,7 +38,7 @@ import logging from io import BytesIO import urllib -from typing import Union, Any, Callable, Sequence, Dict, Optional, TypeVar, Tuple, cast +from typing import Union, Any, Callable, Dict, Optional, TypeVar, Tuple from zerver.lib.logging_util import log_to_file # This is a hack to ensure that RemoteZulipServer always exists even diff --git a/zerver/forms.py b/zerver/forms.py index 0471b3f10e..40c1e3c729 100644 --- a/zerver/forms.py +++ b/zerver/forms.py @@ -7,10 +7,8 @@ from django.contrib.auth.forms import SetPasswordForm, AuthenticationForm, \ from django.core.exceptions import ValidationError from django.urls import reverse from django.core.validators import validate_email -from django.db.models.query import QuerySet from django.utils.translation import ugettext as _ from django.contrib.auth.tokens import default_token_generator -from django.contrib.sites.shortcuts import get_current_site from django.utils.http import urlsafe_base64_encode from django.utils.encoding import force_bytes from django.contrib.auth.tokens import PasswordResetTokenGenerator @@ -22,7 +20,7 @@ from zerver.lib.actions import do_change_password, email_not_system_bot, \ from zerver.lib.name_restrictions import is_reserved_subdomain, is_disposable_domain from zerver.lib.request import JsonableError from zerver.lib.send_email import send_email, FromAddress -from zerver.lib.subdomains import get_subdomain, user_matches_subdomain, is_root_domain_available +from zerver.lib.subdomains import get_subdomain, is_root_domain_available from zerver.lib.users import check_full_name from zerver.models import Realm, get_user_by_delivery_email, UserProfile, get_realm, \ email_to_domain, \ @@ -34,7 +32,7 @@ import logging import re import DNS -from typing import Any, Callable, List, Optional, Dict +from typing import Any, List, Optional, Dict from two_factor.forms import AuthenticationTokenForm as TwoFactorAuthenticationTokenForm from two_factor.utils import totp_digits diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 388d93e0a0..db9b4f4137 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -1,6 +1,6 @@ from typing import ( - AbstractSet, Any, AnyStr, Callable, Dict, Iterable, List, Mapping, MutableMapping, - Optional, Sequence, Set, Tuple, TypeVar, Union, cast + AbstractSet, Any, Callable, Dict, Iterable, List, Mapping, MutableMapping, + Optional, Sequence, Set, Tuple, Union, cast ) from mypy_extensions import TypedDict @@ -77,7 +77,6 @@ from zerver.lib.users import ( check_bot_name_available, check_full_name, get_api_key, - user_ids_to_users ) from zerver.lib.user_status import ( update_user_status, @@ -88,10 +87,10 @@ from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, RealmDomain, Service, SubMessage, \ Subscription, Recipient, Message, Attachment, UserMessage, RealmAuditLog, \ UserHotspot, MultiuseInvite, ScheduledMessage, UserStatus, \ - Client, DefaultStream, DefaultStreamGroup, UserPresence, PushDeviceToken, \ + Client, DefaultStream, DefaultStreamGroup, UserPresence, \ ScheduledEmail, MAX_TOPIC_NAME_LENGTH, \ - MAX_MESSAGE_LENGTH, get_client, get_stream, get_personal_recipient, get_huddle, \ - get_user_profile_by_id, PreregistrationUser, get_display_recipient, \ + MAX_MESSAGE_LENGTH, get_client, get_stream, get_personal_recipient, \ + get_user_profile_by_id, PreregistrationUser, \ get_realm, bulk_get_recipients, get_stream_recipient, get_stream_recipients, \ email_allowed_for_realm, email_to_username, display_recipient_cache_key, \ get_user_by_delivery_email, get_stream_cache_key, active_non_guest_user_ids, \ @@ -101,7 +100,7 @@ from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, Reaction, EmailChangeStatus, CustomProfileField, \ custom_profile_fields_for_realm, get_huddle_user_ids, \ CustomProfileFieldValue, validate_attachment_request, get_system_bot, \ - get_display_recipient_by_id, query_for_ids, get_huddle_recipient, \ + query_for_ids, get_huddle_recipient, \ UserGroup, UserGroupMembership, get_default_stream_groups, \ get_bot_services, get_bot_dicts_in_realm, DomainNotAllowedForRealmError, \ DisposableEmailError, EmailContainsPlusError, \ @@ -130,23 +129,21 @@ from zerver.lib.utils import generate_api_key from zerver.lib.create_user import create_user from zerver.lib import bugdown from zerver.lib.cache import cache_with_key, cache_set, \ - user_profile_by_email_cache_key, user_profile_cache_key, \ + user_profile_by_email_cache_key, \ cache_set_many, cache_delete, cache_delete_many from zerver.decorator import statsd_increment from zerver.lib.utils import log_statsd_event, statsd -from zerver.lib.html_diff import highlight_html_differences from zerver.lib.i18n import get_language_name -from zerver.lib.alert_words import user_alert_words, add_user_alert_words, \ +from zerver.lib.alert_words import add_user_alert_words, \ remove_user_alert_words, set_user_alert_words from zerver.lib.notifications import clear_scheduled_emails, \ clear_scheduled_invitation_emails, enqueue_welcome_emails -from zerver.lib.narrow import check_supported_events_narrow_filter from zerver.lib.exceptions import JsonableError, ErrorCode, BugdownRenderingException from zerver.lib.sessions import delete_user_sessions from zerver.lib.upload import attachment_url_re, attachment_url_to_path_id, \ claim_attachment, delete_message_image, upload_emoji_image, delete_avatar_image from zerver.lib.video_calls import request_zoom_video_call_url -from zerver.tornado.event_queue import request_event_queue, send_event +from zerver.tornado.event_queue import send_event from zerver.lib.types import ProfileFieldData from analytics.models import StreamCount diff --git a/zerver/lib/addressee.py b/zerver/lib/addressee.py index 5352234e0b..131c61cebb 100644 --- a/zerver/lib/addressee.py +++ b/zerver/lib/addressee.py @@ -1,10 +1,8 @@ from typing import Iterable, List, Optional, Sequence, Union, cast -from django.core.exceptions import ValidationError from django.utils.translation import ugettext as _ from zerver.lib.exceptions import JsonableError -from zerver.lib.request import JsonableError from zerver.models import ( Realm, UserProfile, diff --git a/zerver/lib/api_test_helpers.py b/zerver/lib/api_test_helpers.py index e870fb5752..9edf2cdc5a 100644 --- a/zerver/lib/api_test_helpers.py +++ b/zerver/lib/api_test_helpers.py @@ -1,5 +1,4 @@ from typing import Dict, Any, Optional, Iterable -from io import StringIO import json import os diff --git a/zerver/lib/bot_lib.py b/zerver/lib/bot_lib.py index 387a29a15f..abd80b8738 100644 --- a/zerver/lib/bot_lib.py +++ b/zerver/lib/bot_lib.py @@ -1,27 +1,18 @@ import json -import logging import os -import signal -import sys -import time -import re import importlib from zerver.lib.actions import internal_send_private_message, \ internal_send_stream_message, internal_send_huddle_message from zerver.models import UserProfile, get_active_user from zerver.lib.bot_storage import get_bot_storage, set_bot_storage, \ - is_key_in_bot_storage, get_bot_storage_size, remove_bot_storage + is_key_in_bot_storage, remove_bot_storage from zerver.lib.bot_config import get_bot_config, ConfigError from zerver.lib.integrations import EMBEDDED_BOTS from zerver.lib.topic import get_topic_from_message_info from django.utils.translation import ugettext as _ -import configparser - -from mypy_extensions import NoReturn -from typing import Any, Optional, List, Dict -from types import ModuleType +from typing import Any, Dict our_dir = os.path.dirname(os.path.abspath(__file__)) diff --git a/zerver/lib/bulk_create.py b/zerver/lib/bulk_create.py index 32fda32b3c..97e746e977 100644 --- a/zerver/lib/bulk_create.py +++ b/zerver/lib/bulk_create.py @@ -1,8 +1,8 @@ -from typing import Any, Dict, Iterable, List, Mapping, Optional, Set, Tuple +from typing import Any, Dict, List, Optional, Set, Tuple from zerver.lib.initial_password import initial_password -from zerver.models import Realm, Stream, UserProfile, Huddle, \ - Subscription, Recipient, Client, RealmAuditLog, get_huddle_hash +from zerver.models import Realm, Stream, UserProfile, \ + Subscription, Recipient, RealmAuditLog from zerver.lib.create_user import create_user_profile from zerver.lib.bugdown import convert as bugdown_convert diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 94bdfa5a45..43461b9a70 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -8,9 +8,8 @@ from django.conf import settings from django.db.models import Q from django.core.cache.backends.base import BaseCache from django.http import HttpRequest -from django import template -from typing import cast, Any, Callable, Dict, Iterable, List, Optional, Union, Set, TypeVar, Tuple +from typing import Any, Callable, Dict, Iterable, List, Optional, TypeVar, Tuple from zerver.lib.utils import statsd, statsd_key, make_safe_digest import time diff --git a/zerver/lib/cache_helpers.py b/zerver/lib/cache_helpers.py index e9e2471048..95083af881 100644 --- a/zerver/lib/cache_helpers.py +++ b/zerver/lib/cache_helpers.py @@ -13,7 +13,7 @@ from django.conf import settings from zerver.models import Message, UserProfile, Stream, get_stream_cache_key, \ Recipient, get_recipient_cache_key, Client, get_client_cache_key, \ Huddle, huddle_hash_cache_key -from zerver.lib.cache import cache_with_key, cache_set, \ +from zerver.lib.cache import \ user_profile_by_api_key_cache_key, \ user_profile_cache_key, get_remote_cache_time, get_remote_cache_requests, \ cache_set_many, to_dict_cache_key_id diff --git a/zerver/lib/create_user.py b/zerver/lib/create_user.py index 46a3855e71..47b15a5dbc 100644 --- a/zerver/lib/create_user.py +++ b/zerver/lib/create_user.py @@ -6,10 +6,7 @@ from zerver.lib.upload import copy_avatar from zerver.lib.hotspots import copy_hotpots from zerver.lib.utils import generate_api_key -import base64 import ujson -import os -import string from typing import Optional diff --git a/zerver/lib/digest.py b/zerver/lib/digest.py index f8594f8d8b..1269a9f880 100644 --- a/zerver/lib/digest.py +++ b/zerver/lib/digest.py @@ -1,4 +1,4 @@ -from typing import Any, Callable, Dict, Iterable, List, Set, Tuple, Union +from typing import Any, Dict, List, Set, Tuple, Union from collections import defaultdict import datetime @@ -6,7 +6,6 @@ import logging import pytz from django.db.models import Q, QuerySet -from django.template import loader from django.conf import settings from django.utils.timezone import now as timezone_now @@ -14,7 +13,7 @@ from confirmation.models import one_click_unsubscribe_link from zerver.lib.notifications import build_message_list from zerver.lib.send_email import send_future_email, FromAddress from zerver.lib.url_encoding import encode_stream -from zerver.models import UserProfile, UserMessage, Recipient, Stream, \ +from zerver.models import UserProfile, UserMessage, Recipient, \ Subscription, UserActivity, get_active_streams, get_user_profile_by_id, \ Realm, Message from zerver.context_processors import common_context diff --git a/zerver/lib/email_mirror.py b/zerver/lib/email_mirror.py index 6c679d6232..74d1150804 100644 --- a/zerver/lib/email_mirror.py +++ b/zerver/lib/email_mirror.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Optional, Union, cast +from typing import Any, Dict, Optional import logging import re @@ -21,8 +21,7 @@ from zerver.lib.utils import generate_random_token from zerver.lib.send_email import FromAddress from zerver.models import Stream, Recipient, \ get_user_profile_by_id, get_display_recipient, get_personal_recipient, \ - Message, Realm, UserProfile, get_system_bot, get_user, MAX_TOPIC_NAME_LENGTH, \ - MAX_MESSAGE_LENGTH + Message, Realm, UserProfile, get_system_bot, get_user logger = logging.getLogger(__name__) diff --git a/zerver/lib/error_notify.py b/zerver/lib/error_notify.py index d1bf2903a7..5363ba6e6c 100644 --- a/zerver/lib/error_notify.py +++ b/zerver/lib/error_notify.py @@ -1,13 +1,11 @@ # System documented in https://zulip.readthedocs.io/en/latest/subsystems/logging.html -import logging - from collections import defaultdict from django.conf import settings from django.core.mail import mail_admins from django.http import HttpResponse from django.utils.translation import ugettext as _ -from typing import cast, Any, Dict, Optional +from typing import Any, Dict, Optional from zerver.filters import clean_data_from_query_parameters from zerver.models import get_system_bot diff --git a/zerver/lib/events.py b/zerver/lib/events.py index 60dfa4a0d6..0826b41822 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -2,20 +2,18 @@ # high-level documentation on how this system works. import copy -import ujson from collections import defaultdict from django.utils.translation import ugettext as _ from django.conf import settings from importlib import import_module from typing import ( - cast, Any, Callable, Dict, Iterable, List, Optional, Sequence, Set, Tuple, Union + Any, Callable, Dict, Iterable, Optional, Sequence, Set ) session_engine = import_module(settings.SESSION_ENGINE) from zerver.lib.alert_words import user_alert_words -from zerver.lib.attachments import user_attachments from zerver.lib.avatar import avatar_url, get_avatar_field from zerver.lib.bot_config import load_bot_config_template from zerver.lib.hotspots import get_next_hotspots @@ -35,7 +33,6 @@ from zerver.lib.request import JsonableError from zerver.lib.topic import TOPIC_NAME from zerver.lib.topic_mutes import get_topic_mutes from zerver.lib.actions import ( - validate_user_access_to_subscribers_helper, do_get_streams, get_default_streams_for_realm, gather_subscriptions_helper, get_cross_realm_dicts, get_status_dict, streams_to_dicts_sorted, diff --git a/zerver/lib/exceptions.py b/zerver/lib/exceptions.py index 39cd8d5e82..5a5e086c09 100644 --- a/zerver/lib/exceptions.py +++ b/zerver/lib/exceptions.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Any, Dict, List, Optional, Type +from typing import Any, Dict, List, Type from mypy_extensions import NoReturn from django.core.exceptions import PermissionDenied diff --git a/zerver/lib/feedback.py b/zerver/lib/feedback.py index eabd430fbe..fd9b0ad87e 100644 --- a/zerver/lib/feedback.py +++ b/zerver/lib/feedback.py @@ -6,8 +6,7 @@ from typing import Any, Mapping, Optional from zerver.lib.actions import internal_send_message from zerver.lib.send_email import FromAddress from zerver.lib.redis_utils import get_redis_client -from zerver.models import get_realm, get_system_bot, \ - UserProfile, Realm +from zerver.models import get_system_bot import time diff --git a/zerver/lib/generate_test_data.py b/zerver/lib/generate_test_data.py index 8c3d0b6eb0..0c89cbac42 100644 --- a/zerver/lib/generate_test_data.py +++ b/zerver/lib/generate_test_data.py @@ -1,7 +1,7 @@ import itertools import ujson import random -from typing import List, Dict, Any, Optional +from typing import List, Dict, Any def load_config() -> Dict[str, Any]: with open("zerver/tests/fixtures/config.generate_data.json", "r") as infile: diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py index 1687694e69..ab9878da03 100644 --- a/zerver/lib/integrations.py +++ b/zerver/lib/integrations.py @@ -1,16 +1,12 @@ import os import pathlib -from typing import Dict, List, Optional, TypeVar, Any +from typing import Dict, List, Optional, Any from django.conf import settings from django.conf.urls import url from django.urls.resolvers import LocaleRegexProvider from django.utils.module_loading import import_string -from django.utils.safestring import mark_safe from django.utils.translation import ugettext as _ -from django.template import loader - -from zerver.templatetags.app_filters import render_markdown_path """This module declares all of the (documented) integrations available diff --git a/zerver/lib/json_encoder_for_html.py b/zerver/lib/json_encoder_for_html.py index 44cc1f4530..7e3ab73636 100644 --- a/zerver/lib/json_encoder_for_html.py +++ b/zerver/lib/json_encoder_for_html.py @@ -1,5 +1,5 @@ import json -from typing import Any, Dict, Iterator, Optional +from typing import Any, Iterator # Taken from # https://github.com/simplejson/simplejson/blob/8edc82afcf6f7512b05fba32baa536fe756bd273/simplejson/encoder.py#L378-L402 diff --git a/zerver/lib/management.py b/zerver/lib/management.py index 3fafd5b944..35e042ef81 100644 --- a/zerver/lib/management.py +++ b/zerver/lib/management.py @@ -1,6 +1,5 @@ # Library code for use in management commands -import sys import time from argparse import ArgumentParser, RawTextHelpFormatter diff --git a/zerver/lib/mdiff.py b/zerver/lib/mdiff.py index 52595eb021..09a2a93098 100755 --- a/zerver/lib/mdiff.py +++ b/zerver/lib/mdiff.py @@ -1,7 +1,6 @@ import os import subprocess import logging -import difflib class DiffException(Exception): pass diff --git a/zerver/lib/message.py b/zerver/lib/message.py index 8041f4b222..349eac84a3 100644 --- a/zerver/lib/message.py +++ b/zerver/lib/message.py @@ -16,7 +16,6 @@ from zerver.lib.cache import ( generic_bulk_cached_fetch, to_dict_cache_key, to_dict_cache_key_id, - cache_set, ) from zerver.lib.request import JsonableError from zerver.lib.stream_subscription import ( diff --git a/zerver/lib/migrate.py b/zerver/lib/migrate.py index ca66e60518..26da4a1490 100644 --- a/zerver/lib/migrate.py +++ b/zerver/lib/migrate.py @@ -1,8 +1,6 @@ -from django.db.models.query import QuerySet from psycopg2.extensions import cursor -from typing import Any, Callable, Dict, List, Tuple, TypeVar +from typing import List, TypeVar -import re import time CursorObj = TypeVar('CursorObj', bound=cursor) diff --git a/zerver/lib/notifications.py b/zerver/lib/notifications.py index 2f79789a5c..917f55f42f 100644 --- a/zerver/lib/notifications.py +++ b/zerver/lib/notifications.py @@ -1,11 +1,9 @@ -from typing import cast, Any, Dict, Iterable, List, Mapping, Optional, Sequence, Tuple +from typing import Any, Dict, Iterable, List, Optional, Tuple -from confirmation.models import Confirmation, one_click_unsubscribe_link +from confirmation.models import one_click_unsubscribe_link from django.conf import settings -from django.template import loader from django.utils.timezone import now as timezone_now -from django.utils.translation import ugettext as _ from django.contrib.auth import get_backends from django_auth_ldap.backend import LDAPBackend @@ -22,21 +20,18 @@ from zerver.models import ( Stream, get_display_recipient, UserProfile, - get_user, get_user_profile_by_id, receives_offline_email_notifications, get_context_for_message, Message, - Realm, ) -from datetime import timedelta, datetime +from datetime import timedelta from email.utils import formataddr from lxml.cssselect import CSSSelector import lxml.html import re import subprocess -import ujson from collections import defaultdict import pytz @@ -529,7 +524,7 @@ def enqueue_welcome_emails(user: UserProfile, realm_creation: bool=False) -> Non else: context['getting_started_link'] = "https://zulipchat.com" - from zproject.backends import email_belongs_to_ldap, require_email_format_usernames + from zproject.backends import email_belongs_to_ldap if email_belongs_to_ldap(user.realm, user.email): context["ldap"] = True diff --git a/zerver/lib/onboarding.py b/zerver/lib/onboarding.py index 893d6a97e1..71392cc632 100644 --- a/zerver/lib/onboarding.py +++ b/zerver/lib/onboarding.py @@ -1,12 +1,12 @@ from django.conf import settings -from zerver.lib.actions import set_default_streams, bulk_add_subscriptions, \ +from zerver.lib.actions import set_default_streams, \ internal_prep_stream_message, internal_send_private_message, \ - create_stream_if_needed, create_streams_if_needed, do_send_messages, \ + create_streams_if_needed, do_send_messages, \ do_add_reaction_legacy, create_users, missing_any_realm_internal_bots from zerver.lib.topic import get_turtle_message -from zerver.models import Realm, UserProfile, Message, Reaction, get_system_bot +from zerver.models import Realm, UserProfile, get_system_bot from typing import Any, Dict, List, Mapping diff --git a/zerver/lib/outgoing_webhook.py b/zerver/lib/outgoing_webhook.py index 5842cbf7fe..f7bb82fe29 100644 --- a/zerver/lib/outgoing_webhook.py +++ b/zerver/lib/outgoing_webhook.py @@ -1,24 +1,18 @@ -from typing import Any, AnyStr, Iterable, Dict, Tuple, Callable, Mapping, Optional +from typing import Any, AnyStr, Dict, Optional import requests import json -import sys -import inspect import logging -import re -import urllib -from functools import reduce from requests import Response from django.utils.translation import ugettext as _ -from zerver.models import Realm, UserProfile, get_user_profile_by_id, get_client, \ +from zerver.models import UserProfile, get_user_profile_by_id, get_client, \ GENERIC_INTERFACE, Service, SLACK_INTERFACE, email_to_domain from zerver.lib.actions import check_send_message from zerver.lib.queue import retry_event from zerver.lib.topic import get_topic_from_message_info from zerver.lib.url_encoding import near_message_url -from zerver.lib.validator import check_dict, check_string from zerver.decorator import JsonableError from version import ZULIP_VERSION diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py index 0e1b5ef3d0..72355deaac 100644 --- a/zerver/lib/push_notifications.py +++ b/zerver/lib/push_notifications.py @@ -2,15 +2,12 @@ import base64 import binascii -from functools import partial import logging import lxml.html -import os import re import time -import random -from typing import Any, Dict, List, Optional, SupportsInt, Tuple, Type, Union, cast +from typing import Any, Dict, List, Optional, Tuple, Union, cast from django.conf import settings from django.db import IntegrityError, transaction @@ -18,21 +15,17 @@ from django.utils.timezone import now as timezone_now from django.utils.translation import ugettext as _ from gcm import GCM import requests -import urllib -import ujson from zerver.decorator import statsd_increment from zerver.lib.avatar import absolute_avatar_url -from zerver.lib.exceptions import ErrorCode, JsonableError +from zerver.lib.exceptions import JsonableError from zerver.lib.message import access_message, huddle_users from zerver.lib.queue import retry_event -from zerver.lib.remote_server import send_to_push_bouncer, send_json_to_push_bouncer, \ - PushNotificationBouncerException -from zerver.lib.timestamp import datetime_to_timestamp, timestamp_to_datetime -from zerver.lib.utils import generate_random_token +from zerver.lib.remote_server import send_to_push_bouncer, send_json_to_push_bouncer +from zerver.lib.timestamp import datetime_to_timestamp from zerver.models import PushDeviceToken, Message, Recipient, UserProfile, \ - UserMessage, get_display_recipient, receives_offline_push_notifications, \ - receives_online_notifications, receives_stream_notifications, get_user_profile_by_id, \ + get_display_recipient, receives_offline_push_notifications, \ + receives_online_notifications, get_user_profile_by_id, \ ArchivedMessage logger = logging.getLogger(__name__) diff --git a/zerver/lib/rate_limiter.py b/zerver/lib/rate_limiter.py index 0d673058f9..b998307f98 100644 --- a/zerver/lib/rate_limiter.py +++ b/zerver/lib/rate_limiter.py @@ -1,7 +1,7 @@ import os -from typing import Any, Iterator, List, Optional, Tuple +from typing import List, Optional, Tuple from django.conf import settings from zerver.lib.redis_utils import get_redis_client @@ -10,7 +10,6 @@ from zerver.models import UserProfile import redis import time -import logging # Implement a rate-limiting scheme inspired by the one described here, but heavily modified # http://blog.domaintools.com/2013/04/rate-limiting-with-redis/ diff --git a/zerver/lib/realm_icon.py b/zerver/lib/realm_icon.py index ff98c3c6fc..b925247e4d 100644 --- a/zerver/lib/realm_icon.py +++ b/zerver/lib/realm_icon.py @@ -1,6 +1,6 @@ from django.conf import settings -from zerver.lib.avatar_hash import gravatar_hash, user_avatar_hash +from zerver.lib.avatar_hash import gravatar_hash from zerver.lib.upload import upload_backend from zerver.models import Realm diff --git a/zerver/lib/retention.py b/zerver/lib/retention.py index e5e38bf0fa..6dfe60b4ac 100644 --- a/zerver/lib/retention.py +++ b/zerver/lib/retention.py @@ -2,7 +2,6 @@ from datetime import timedelta from django.db import connection, transaction -from django.forms.models import model_to_dict from django.utils.timezone import now as timezone_now from zerver.models import Realm, Message, UserMessage, ArchivedMessage, ArchivedUserMessage, \ Attachment, ArchivedAttachment diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.py index e814833221..af492c9255 100644 --- a/zerver/lib/send_email.py +++ b/zerver/lib/send_email.py @@ -13,7 +13,7 @@ import logging import ujson import os -from typing import Any, Dict, Iterable, List, Mapping, Optional, Tuple +from typing import Any, Dict, List, Mapping, Optional, Tuple from zerver.lib.logging_util import log_to_file from confirmation.models import generate_key diff --git a/zerver/lib/soft_deactivation.py b/zerver/lib/soft_deactivation.py index 8eb9a34905..570e261359 100644 --- a/zerver/lib/soft_deactivation.py +++ b/zerver/lib/soft_deactivation.py @@ -9,7 +9,7 @@ from django.utils.timezone import now as timezone_now from typing import DefaultDict, List, Union, Any from zerver.models import UserProfile, UserMessage, RealmAuditLog, \ - Subscription, Message, Recipient, UserActivity, Realm + Subscription, Message, Recipient, UserActivity logger = logging.getLogger("zulip.soft_deactivation") log_to_file(logger, settings.SOFT_DEACTIVATION_LOG_PATH) diff --git a/zerver/lib/statistics.py b/zerver/lib/statistics.py index aacb272924..c55ceedaae 100644 --- a/zerver/lib/statistics.py +++ b/zerver/lib/statistics.py @@ -1,9 +1,6 @@ # -*- coding: utf-8 -*- -from zerver.models import UserProfile, UserActivity, UserActivityInterval, Message - -from django.utils.timezone import utc -from typing import Any, Dict, List, Sequence, Set +from zerver.models import UserProfile, UserActivityInterval from datetime import datetime, timedelta diff --git a/zerver/lib/stream_topic.py b/zerver/lib/stream_topic.py index c3540599de..654aeb98af 100644 --- a/zerver/lib/stream_topic.py +++ b/zerver/lib/stream_topic.py @@ -1,4 +1,4 @@ -from typing import (Dict, List, Set) +from typing import Set from django.db.models.query import QuerySet from zerver.lib.stream_subscription import ( diff --git a/zerver/lib/streams.py b/zerver/lib/streams.py index 26c4b894f8..d8f4ba45fb 100644 --- a/zerver/lib/streams.py +++ b/zerver/lib/streams.py @@ -1,7 +1,6 @@ from typing import Any, Iterable, List, Mapping, Set, Tuple, Optional -from django.http import HttpRequest, HttpResponse from django.utils.translation import ugettext as _ from zerver.lib.actions import check_stream_name, create_streams_if_needed diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 69eba8fddf..f470379778 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -1,6 +1,6 @@ from contextlib import contextmanager -from typing import (cast, Any, Callable, Dict, Iterable, Iterator, List, Mapping, Optional, - Sized, Tuple, Union) +from typing import (cast, Any, Dict, Iterable, Iterator, List, Optional, + Tuple, Union) from django.apps import apps from django.db.migrations.state import StateApps @@ -24,7 +24,7 @@ from zerver.lib.users import get_api_key from zerver.lib.actions import ( check_send_message, create_stream_if_needed, bulk_add_subscriptions, - get_display_recipient, bulk_remove_subscriptions, do_create_user, + bulk_remove_subscriptions, check_send_stream_message, gather_subscriptions, get_default_value_for_history_public_to_subscribers, ) @@ -40,13 +40,13 @@ from zerver.lib.test_helpers import ( from zerver.models import ( get_stream, get_client, + get_display_recipient, get_user, get_realm, Client, Message, Realm, Recipient, - Service, Stream, Subscription, UserProfile, diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.py index 4684b62d27..f1b5994c3e 100644 --- a/zerver/lib/test_helpers.py +++ b/zerver/lib/test_helpers.py @@ -1,28 +1,23 @@ from contextlib import contextmanager from typing import ( - cast, Any, Callable, Dict, Generator, Iterable, Iterator, List, Mapping, - Optional, Set, Sized, Tuple, Union, IO, TypeVar + Any, Callable, Dict, Generator, Iterable, Iterator, List, Mapping, + Optional, Tuple, Union, IO, TypeVar ) from django.core import signing from django.urls.resolvers import LocaleRegexURLResolver from django.conf import settings -from django.test import TestCase, override_settings -from django.test.client import ( - BOUNDARY, MULTIPART_CONTENT, encode_multipart, -) +from django.test import override_settings from django.template import loader from django.http import HttpResponse, HttpResponseRedirect -from django.db.utils import IntegrityError from django.db.migrations.state import StateApps -from boto.s3.connection import Location, S3Connection +from boto.s3.connection import S3Connection from boto.s3.bucket import Bucket import zerver.lib.upload from zerver.lib.upload import S3UploadBackend, LocalUploadBackend from zerver.lib.avatar import avatar_url from zerver.lib.cache import get_cache_backend -from zerver.lib.initial_password import initial_password from zerver.lib.db import TimeTrackingCursor from zerver.lib import cache from zerver.tornado import event_queue @@ -30,40 +25,29 @@ from zerver.tornado.handlers import allocate_handler_id from zerver.worker import queue_processors from zerver.lib.actions import ( - check_send_message, create_stream_if_needed, bulk_add_subscriptions, get_stream_recipient, ) from zerver.models import ( - get_recipient, get_stream, - get_user, Client, Message, - Realm, - Recipient, - Stream, Subscription, UserMessage, UserProfile, ) -from zerver.lib.request import JsonableError - if False: # Avoid an import cycle; we only need these for type annotations. from zerver.lib.test_classes import ZulipTestCase, MigrationsTestCase import collections -import base64 import mock import os import re import sys import time import ujson -import unittest -import urllib from moto import mock_s3_deprecated import fakeldap @@ -581,9 +565,6 @@ def use_db_models(method: Callable[..., None]) -> Callable[..., None]: 'zerver.lib.test_helpers', Client=Client, Message=Message, - Realm=Realm, - Recipient=Recipient, - Stream=Stream, Subscription=Subscription, UserMessage=UserMessage, UserProfile=UserProfile, @@ -595,7 +576,6 @@ def use_db_models(method: Callable[..., None]) -> Callable[..., None]: Message=Message, Realm=Realm, Recipient=Recipient, - Service=Service, Stream=Stream, Subscription=Subscription, UserProfile=UserProfile, diff --git a/zerver/lib/test_runner.py b/zerver/lib/test_runner.py index 8282108380..d7a2fda8dc 100644 --- a/zerver/lib/test_runner.py +++ b/zerver/lib/test_runner.py @@ -2,8 +2,8 @@ from functools import partial import random -from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Tuple, \ - Type, cast, Union, TypeVar +from typing import Any, Callable, Dict, List, Optional, Set, Tuple, \ + Type, cast, TypeVar from unittest import loader, runner # type: ignore # Mypy cannot pick these up. from unittest.result import TestResult @@ -21,15 +21,12 @@ from zerver.lib.rate_limiter import bounce_redis_key_prefix_for_testing from zerver.lib.test_classes import flush_caches_for_testing from zerver.lib.sqlalchemy_utils import get_sqlalchemy_connection from zerver.lib.test_helpers import ( - get_all_templates, write_instrumentation_reports, + write_instrumentation_reports, append_instrumentation_data ) import os -import subprocess -import sys import time -import traceback import unittest from multiprocessing.sharedctypes import Synchronized diff --git a/zerver/lib/upload.py b/zerver/lib/upload.py index 26f0aac8d7..6195a5755f 100644 --- a/zerver/lib/upload.py +++ b/zerver/lib/upload.py @@ -1,8 +1,7 @@ -from typing import Any, Dict, Mapping, Optional, Tuple +from typing import Dict, Optional, Tuple from django.utils.translation import ugettext as _ from django.conf import settings -from django.template.defaultfilters import slugify from django.core.files import File from django.http import HttpRequest from django.db.models import Sum @@ -17,7 +16,7 @@ from boto.s3.key import Key from boto.s3.connection import S3Connection from mimetypes import guess_type, guess_extension -from zerver.models import get_user_profile_by_id, RealmEmoji +from zerver.models import get_user_profile_by_id from zerver.models import Attachment from zerver.models import Realm, RealmEmoji, UserProfile, Message diff --git a/zerver/lib/user_groups.py b/zerver/lib/user_groups.py index 415e463d54..cc9aa1153b 100644 --- a/zerver/lib/user_groups.py +++ b/zerver/lib/user_groups.py @@ -1,11 +1,10 @@ from __future__ import absolute_import -from collections import defaultdict from django.db import transaction from django.utils.translation import ugettext as _ from zerver.lib.exceptions import JsonableError from zerver.models import UserProfile, Realm, UserGroupMembership, UserGroup -from typing import Dict, Iterable, List, Tuple, Any +from typing import Dict, List, Any def access_user_group_by_id(user_group_id: int, user_profile: UserProfile) -> UserGroup: try: diff --git a/zerver/lib/users.py b/zerver/lib/users.py index 6c56099e60..d74cbcabc9 100644 --- a/zerver/lib/users.py +++ b/zerver/lib/users.py @@ -2,14 +2,13 @@ from typing import Dict, List, Optional, Union, cast from django.db.models.query import QuerySet from django.utils.translation import ugettext as _ -from django.conf import settings from zerver.lib.cache import generic_bulk_cached_fetch, user_profile_cache_key_id, \ user_profile_by_id_cache_key from zerver.lib.request import JsonableError from zerver.lib.avatar import avatar_url from zerver.models import UserProfile, Service, Realm, \ - get_user_profile_by_id, query_for_ids, get_user_profile_by_id_in_realm, \ + get_user_profile_by_id_in_realm, \ CustomProfileField from zulip_bots.custom_exceptions import ConfigValidationError diff --git a/zerver/lib/utils.py b/zerver/lib/utils.py index 22d7055b16..8eba3809be 100644 --- a/zerver/lib/utils.py +++ b/zerver/lib/utils.py @@ -2,13 +2,11 @@ from typing import Any, Callable, List, Optional, Sequence, TypeVar, Iterable, Set, Tuple import base64 -import errno import hashlib import heapq import itertools import os import string -import sys from time import sleep from itertools import zip_longest diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py index 2aff2d0a5a..81867c6429 100644 --- a/zerver/lib/validator.py +++ b/zerver/lib/validator.py @@ -30,8 +30,7 @@ import ujson from django.utils.translation import ugettext as _ from django.core.exceptions import ValidationError from django.core.validators import validate_email, URLValidator -from typing import Callable, Iterable, Optional, Tuple, TypeVar, cast, \ - Dict +from typing import Iterable, Optional, Tuple, cast from datetime import datetime from zerver.lib.request import JsonableError diff --git a/zerver/lib/webhooks/common.py b/zerver/lib/webhooks/common.py index 52ffa48bd7..7926f8ee13 100644 --- a/zerver/lib/webhooks/common.py +++ b/zerver/lib/webhooks/common.py @@ -1,6 +1,5 @@ from urllib.parse import unquote -from django.conf import settings from django.http import HttpRequest from django.utils.translation import ugettext as _ from typing import Optional @@ -11,7 +10,7 @@ from zerver.lib.exceptions import StreamDoesNotExistError, JsonableError, \ ErrorCode from zerver.lib.request import REQ, has_request_variables from zerver.lib.send_email import FromAddress -from zerver.models import UserProfile, get_system_bot +from zerver.models import UserProfile MISSING_EVENT_HEADER_MESSAGE = """ diff --git a/zerver/lib/widget.py b/zerver/lib/widget.py index 98fd048c5d..bd40ffb99d 100644 --- a/zerver/lib/widget.py +++ b/zerver/lib/widget.py @@ -1,5 +1,4 @@ -from typing import MutableMapping, Any, Optional, List, Tuple -from django.conf import settings +from typing import MutableMapping, Any, Optional, Tuple import re import json diff --git a/zerver/logging_handlers.py b/zerver/logging_handlers.py index e945778fee..8510159305 100644 --- a/zerver/logging_handlers.py +++ b/zerver/logging_handlers.py @@ -8,10 +8,8 @@ import traceback from typing import Any, Dict, Optional from django.conf import settings -from django.core import mail from django.http import HttpRequest -from django.utils.log import AdminEmailHandler -from django.views.debug import ExceptionReporter, get_exception_reporter_filter +from django.views.debug import get_exception_reporter_filter from zerver.lib.logging_util import find_log_caller_module from zerver.lib.queue import queue_json_publish @@ -113,7 +111,6 @@ class AdminNotifyHandler(logging.Handler): if record.exc_info: stack_trace = ''.join(traceback.format_exception(*record.exc_info)) message = str(record.exc_info[1]) - from zerver.lib.exceptions import BugdownRenderingException is_bugdown_rendering_exception = record.msg.startswith('Exception in Markdown parser') else: stack_trace = 'No stack trace available' diff --git a/zerver/middleware.py b/zerver/middleware.py index 55c309cbb0..e23197800b 100644 --- a/zerver/middleware.py +++ b/zerver/middleware.py @@ -3,7 +3,7 @@ import cProfile import logging import time import traceback -from typing import Any, AnyStr, Callable, Dict, \ +from typing import Any, AnyStr, Dict, \ Iterable, List, MutableMapping, Optional from bs4 import BeautifulSoup @@ -13,7 +13,7 @@ from django.contrib.sessions.middleware import SessionMiddleware from django.core.exceptions import DisallowedHost, SuspiciousOperation from django.db import connection from django.http import HttpRequest, HttpResponse, StreamingHttpResponse -from django.shortcuts import redirect, render +from django.shortcuts import render from django.utils.cache import patch_vary_headers from django.utils.deprecation import MiddlewareMixin from django.utils.http import cookie_date diff --git a/zerver/models.py b/zerver/models.py index c0f12d9ba9..7cad52e980 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -1,11 +1,10 @@ from typing import Any, DefaultDict, Dict, List, Set, Tuple, TypeVar, \ - Union, Optional, Sequence, AbstractSet, Pattern, AnyStr, Callable, Iterable + Union, Optional, Sequence, AbstractSet, Callable, Iterable from typing.re import Match from django.db import models -from django.db.models.query import QuerySet, F -from django.db.models import Manager, CASCADE, Sum -from django.db.models.functions import Length +from django.db.models.query import QuerySet +from django.db.models import Manager, CASCADE from django.conf import settings from django.contrib.auth.models import AbstractBaseUser, UserManager, \ PermissionsMixin @@ -30,27 +29,23 @@ from zerver.lib.timestamp import datetime_to_timestamp from django.db.models.signals import pre_save, post_save, post_delete from django.utils.translation import ugettext_lazy as _ from zerver.lib import cache -from zerver.lib.validator import check_int, check_float, \ +from zerver.lib.validator import check_int, \ check_short_string, check_long_string, validate_choice_field, check_date, \ check_url, check_list from zerver.lib.name_restrictions import is_disposable_domain from zerver.lib.types import Validator, ExtendedValidator, \ - ProfileDataElement, ProfileData, FieldTypeData, FieldElement, \ + ProfileDataElement, ProfileData, FieldTypeData, \ RealmUserValidator -from django.utils.encoding import force_text - from bitfield import BitField from bitfield.types import BitHandler from collections import defaultdict, OrderedDict from datetime import timedelta import pylibmc import re -import logging import sre_constants import time import datetime -import sys MAX_TOPIC_NAME_LENGTH = 60 MAX_MESSAGE_LENGTH = 10000 diff --git a/zerver/signals.py b/zerver/signals.py index df04320a3e..56d8f7ae4a 100644 --- a/zerver/signals.py +++ b/zerver/signals.py @@ -1,10 +1,8 @@ - -from typing import Any, Dict, Optional +from typing import Any, Optional from django.conf import settings from django.contrib.auth.signals import user_logged_in from django.dispatch import receiver -from django.template import loader from django.utils.timezone import \ get_current_timezone_name as timezone_get_current_timezone_name from django.utils.timezone import now as timezone_now diff --git a/zerver/templatetags/app_filters.py b/zerver/templatetags/app_filters.py index fe59148a5a..faaf0685a4 100644 --- a/zerver/templatetags/app_filters.py +++ b/zerver/templatetags/app_filters.py @@ -1,4 +1,3 @@ -import os from html import unescape from typing import Any, Dict, List, Optional @@ -7,8 +6,7 @@ import markdown.extensions.admonition import markdown.extensions.codehilite import markdown.extensions.extra import markdown.extensions.toc -from django.conf import settings -from django.template import Library, engines, loader +from django.template import Library, engines from django.utils.safestring import mark_safe from jinja2.exceptions import TemplateNotFound @@ -21,7 +19,6 @@ import zerver.lib.bugdown.help_settings_links import zerver.lib.bugdown.help_relative_links import zerver.lib.bugdown.help_emoticon_translations_table import zerver.lib.bugdown.include -from zerver.context_processors import zulip_default_context from zerver.lib.cache import ignore_unhashable_lru_cache register = Library() diff --git a/zerver/worker/queue_processors.py b/zerver/worker/queue_processors.py index 5cdb74d0e3..7192e6c44c 100644 --- a/zerver/worker/queue_processors.py +++ b/zerver/worker/queue_processors.py @@ -14,8 +14,8 @@ from django.db import connection from django.core.handlers.wsgi import WSGIRequest from django.core.handlers.base import BaseHandler from zerver.models import \ - get_client, get_system_bot, ScheduledEmail, PreregistrationUser, \ - get_user_profile_by_id, Message, Realm, Service, UserMessage, UserProfile, \ + get_client, get_system_bot, PreregistrationUser, \ + get_user_profile_by_id, Message, Realm, UserMessage, UserProfile, \ Client from zerver.lib.context_managers import lockfile from zerver.lib.error_notify import do_report_error @@ -27,7 +27,7 @@ from zerver.lib.push_notifications import handle_push_notification, handle_remov initialize_push_notifications from zerver.lib.actions import do_send_confirmation_email, \ do_update_user_activity, do_update_user_activity_interval, do_update_user_presence, \ - internal_send_message, check_send_message, extract_recipients, \ + internal_send_message, \ render_incoming_message, do_update_embedded_data, do_mark_stream_messages_as_read from zerver.lib.url_preview import preview as url_preview from zerver.lib.digest import handle_digest_email @@ -35,7 +35,6 @@ from zerver.lib.send_email import send_future_email, send_email_from_dict, \ FromAddress, EmailNotDeliveredException, handle_send_email_format_changes from zerver.lib.email_mirror import process_message as mirror_email from zerver.lib.streams import access_stream_by_id -from zerver.decorator import JsonableError from zerver.tornado.socket import req_redis_key, respond_send_message from confirmation.models import Confirmation, create_confirmation_link from zerver.lib.db import reset_queries @@ -56,8 +55,6 @@ import datetime import logging import requests from io import StringIO -import re -import importlib logger = logging.getLogger(__name__)