zerver: Use Python 3 syntax for typing.

Tweaked by tabbott to fix some minor whitespace errors.
This commit is contained in:
rht 2017-11-27 06:33:05 +00:00 committed by Tim Abbott
parent 0ec2a9d259
commit a1cc720860
10 changed files with 267 additions and 496 deletions

View File

@ -7,8 +7,7 @@ from django.conf import settings
from django.core.cache import cache
from django.db.models.signals import post_migrate
def flush_cache(sender, **kwargs):
# type: (AppConfig, **Any) -> None
def flush_cache(sender: AppConfig, **kwargs: Any) -> None:
logging.info("Clearing memcached cache after migrations")
cache.clear()
@ -16,8 +15,7 @@ def flush_cache(sender, **kwargs):
class ZerverConfig(AppConfig):
name = "zerver" # type: str
def ready(self):
# type: () -> None
def ready(self) -> None:
import zerver.signals
if settings.POST_MIGRATION_CACHE_FLUSHING:

View File

@ -22,8 +22,7 @@ from zerver.lib.realm_icon import get_realm_icon_url
from version import ZULIP_VERSION
def common_context(user):
# type: (UserProfile) -> Dict[str, Any]
def common_context(user: UserProfile) -> Dict[str, Any]:
"""Common context used for things like outgoing emails that don't
have a request.
"""
@ -34,15 +33,13 @@ def common_context(user):
'external_host': settings.EXTERNAL_HOST,
}
def get_realm_from_request(request):
# type: (HttpRequest) -> Optional[Realm]
def get_realm_from_request(request: HttpRequest) -> Optional[Realm]:
if hasattr(request, "user") and hasattr(request.user, "realm"):
return request.user.realm
subdomain = get_subdomain(request)
return get_realm(subdomain)
def zulip_default_context(request):
# type: (HttpRequest) -> Dict[str, Any]
def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
"""Context available to all Zulip Jinja2 templates that have a request
passed in. Designed to provide the long list of variables at the
bottom of this function in a wide range of situations: logged-in
@ -141,8 +138,7 @@ def zulip_default_context(request):
}
def add_metrics(request):
# type: (HttpRequest) -> Dict[str, str]
def add_metrics(request: HttpRequest) -> Dict[str, str]:
return {
'dropboxAppKey': settings.DROPBOX_APP_KEY
}

View File

@ -58,27 +58,25 @@ class _RespondAsynchronously:
# mode.
RespondAsynchronously = _RespondAsynchronously()
def asynchronous(method):
# type: (Callable[..., Union[HttpResponse, _RespondAsynchronously]]) -> Callable[..., Union[HttpResponse, _RespondAsynchronously]]
AsyncWrapperT = Callable[..., Union[HttpResponse, _RespondAsynchronously]]
def asynchronous(method: Callable[..., Union[HttpResponse, _RespondAsynchronously]]) -> AsyncWrapperT:
# TODO: this should be the correct annotation when mypy gets fixed: type:
# (Callable[[HttpRequest, base.BaseHandler, Sequence[Any], Dict[str, Any]],
# Union[HttpResponse, _RespondAsynchronously]]) ->
# Callable[[HttpRequest, Sequence[Any], Dict[str, Any]], Union[HttpResponse, _RespondAsynchronously]]
# TODO: see https://github.com/python/mypy/issues/1655
@wraps(method)
def wrapper(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> Union[HttpResponse, _RespondAsynchronously]
def wrapper(request: HttpRequest, *args: Any,
**kwargs: Any) -> Union[HttpResponse, _RespondAsynchronously]:
return method(request, handler=request._tornado_handler, *args, **kwargs)
if getattr(method, 'csrf_exempt', False):
wrapper.csrf_exempt = True # type: ignore # https://github.com/JukkaL/mypy/issues/1170
return wrapper
def cachify(method):
# type: (Callable[..., ReturnT]) -> Callable[..., ReturnT]
def cachify(method: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
dct = {} # type: Dict[Tuple[Any, ...], ReturnT]
def cache_wrapper(*args):
# type: (*Any) -> ReturnT
def cache_wrapper(*args: Any) -> ReturnT:
tup = tuple(args)
if tup in dct:
return dct[tup]
@ -87,8 +85,8 @@ def cachify(method):
return result
return cache_wrapper
def update_user_activity(request, user_profile, query):
# type: (HttpRequest, UserProfile, Optional[str]) -> None
def update_user_activity(request: HttpRequest, user_profile: UserProfile,
query: Optional[str]) -> None:
# update_active_status also pushes to rabbitmq, and it seems
# redundant to log that here as well.
if request.META["PATH_INFO"] == '/json/users/me/presence':
@ -108,11 +106,9 @@ def update_user_activity(request, user_profile, query):
queue_json_publish("user_activity", event, lambda event: None)
# Based on django.views.decorators.http.require_http_methods
def require_post(func):
# type: (ViewFuncT) -> ViewFuncT
def require_post(func: ViewFuncT) -> ViewFuncT:
@wraps(func)
def wrapper(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def wrapper(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
if (request.method != "POST" and
not (request.method == "SOCKET" and
request.META['zulip.emulated_method'] == "POST")):
@ -126,11 +122,9 @@ def require_post(func):
return func(request, *args, **kwargs)
return wrapper # type: ignore # https://github.com/python/mypy/issues/1927
def require_realm_admin(func):
# type: (ViewFuncT) -> ViewFuncT
def require_realm_admin(func: ViewFuncT) -> ViewFuncT:
@wraps(func)
def wrapper(request, user_profile, *args, **kwargs):
# type: (HttpRequest, UserProfile, *Any, **Any) -> HttpResponse
def wrapper(request: HttpRequest, user_profile: UserProfile, *args: Any, **kwargs: Any) -> HttpResponse:
if not user_profile.is_realm_admin:
raise JsonableError(_("Must be a realm administrator"))
return func(request, user_profile, *args, **kwargs)
@ -138,8 +132,7 @@ def require_realm_admin(func):
from zerver.lib.user_agent import parse_user_agent
def get_client_name(request, is_browser_view):
# type: (HttpRequest, bool) -> Text
def get_client_name(request: HttpRequest, is_browser_view: bool) -> Text:
# If the API request specified a client in the request content,
# that has priority. Otherwise, extract the client from the
# User-Agent.
@ -184,19 +177,16 @@ class InvalidZulipServerError(JsonableError):
code = ErrorCode.INVALID_ZULIP_SERVER
data_fields = ['role']
def __init__(self, role):
# type: (Text) -> None
def __init__(self, role: Text) -> None:
self.role = role # type: Text
@staticmethod
def msg_format():
# type: () -> Text
def msg_format() -> Text:
return "Zulip server auth failure: {role} is not registered"
class InvalidZulipServerKeyError(JsonableError):
@staticmethod
def msg_format():
# type: () -> Text
def msg_format() -> Text:
return "Zulip server auth failure: key does not match role {role}"
def validate_api_key(request, role, api_key, is_webhook=False,
@ -233,8 +223,7 @@ def validate_api_key(request, role, api_key, is_webhook=False,
return user_profile
def validate_account_and_subdomain(request, user_profile):
# type: (HttpRequest, UserProfile) -> None
def validate_account_and_subdomain(request: HttpRequest, user_profile: UserProfile) -> None:
if not user_profile.is_active:
raise JsonableError(_("Account not active"))
@ -255,8 +244,7 @@ def validate_account_and_subdomain(request, user_profile):
user_profile.email, user_profile.realm.subdomain, get_subdomain(request)))
raise JsonableError(_("Account is not associated with this subdomain"))
def access_user_by_api_key(request, api_key, email=None):
# type: (HttpRequest, Text, Optional[Text]) -> UserProfile
def access_user_by_api_key(request: HttpRequest, api_key: Text, email: Optional[Text]=None) -> UserProfile:
try:
user_profile = get_user_profile_by_api_key(api_key)
except UserProfile.DoesNotExist:
@ -272,12 +260,11 @@ def access_user_by_api_key(request, api_key, email=None):
return user_profile
# Use this for webhook views that don't get an email passed in.
def api_key_only_webhook_view(client_name):
# type: (Text) -> Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]
WrappedViewFuncT = Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]
def api_key_only_webhook_view(client_name: Text) -> WrappedViewFuncT:
# TODO The typing here could be improved by using the Extended Callable types:
# https://mypy.readthedocs.io/en/latest/kinds_of_types.html#extended-callable-types
def _wrapped_view_func(view_func):
# type: (Callable[..., HttpResponse]) -> Callable[..., HttpResponse]
def _wrapped_view_func(view_func: Callable[..., HttpResponse]) -> Callable[..., HttpResponse]:
@csrf_exempt
@has_request_variables
@wraps(view_func)
@ -341,18 +328,16 @@ def redirect_to_login(next, login_url=None,
return HttpResponseRedirect(urllib.parse.urlunparse(login_url_parts))
# From Django 1.8
def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
# type: (Callable[[HttpResponse], bool], Optional[Text], Text) -> Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]
def user_passes_test(test_func: Callable[[HttpResponse], bool], login_url: Optional[Text]=None,
redirect_field_name: Text=REDIRECT_FIELD_NAME) -> WrappedViewFuncT:
"""
Decorator for views that checks that the user passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
def decorator(view_func):
# type: (Callable[..., HttpResponse]) -> Callable[..., HttpResponse]
def decorator(view_func: Callable[..., HttpResponse]) -> Callable[..., HttpResponse]:
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def _wrapped_view(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
if test_func(request):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
@ -369,8 +354,7 @@ def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIE
return _wrapped_view
return decorator
def logged_in_and_active(request):
# type: (HttpRequest) -> bool
def logged_in_and_active(request: HttpRequest) -> bool:
if not request.user.is_authenticated:
return False
if not request.user.is_active:
@ -379,8 +363,7 @@ def logged_in_and_active(request):
return False
return user_matches_subdomain(get_subdomain(request), request.user)
def do_login(request, user_profile):
# type: (HttpRequest, UserProfile) -> None
def do_login(request: HttpRequest, user_profile: UserProfile) -> None:
"""Creates a session, logging in the user, using the Django method,
and also adds helpful data needed by our server logs.
"""
@ -388,31 +371,25 @@ def do_login(request, user_profile):
request._email = user_profile.email
process_client(request, user_profile, is_browser_view=True)
def log_view_func(view_func):
# type: (ViewFuncT) -> ViewFuncT
def log_view_func(view_func: ViewFuncT) -> ViewFuncT:
@wraps(view_func)
def _wrapped_view_func(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def _wrapped_view_func(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
request._query = view_func.__name__
return view_func(request, *args, **kwargs)
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
def add_logging_data(view_func):
# type: (ViewFuncT) -> ViewFuncT
def add_logging_data(view_func: ViewFuncT) -> ViewFuncT:
@wraps(view_func)
def _wrapped_view_func(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def _wrapped_view_func(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
request._email = request.user.email
process_client(request, request.user, is_browser_view=True,
query=view_func.__name__)
return rate_limit()(view_func)(request, *args, **kwargs)
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
def human_users_only(view_func):
# type: (ViewFuncT) -> ViewFuncT
def human_users_only(view_func: ViewFuncT) -> ViewFuncT:
@wraps(view_func)
def _wrapped_view_func(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def _wrapped_view_func(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
if request.user.is_bot:
return json_error(_("This endpoint does not accept bot requests."))
return view_func(request, *args, **kwargs)
@ -433,12 +410,10 @@ def zulip_login_required(function=None,
return actual_decorator(add_logging_data(function))
return actual_decorator
def require_server_admin(view_func):
# type: (ViewFuncT) -> ViewFuncT
def require_server_admin(view_func: ViewFuncT) -> ViewFuncT:
@zulip_login_required
@wraps(view_func)
def _wrapped_view_func(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def _wrapped_view_func(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
if not request.user.is_staff:
return HttpResponseRedirect(settings.HOME_NOT_LOGGED_IN)
@ -449,10 +424,8 @@ def require_server_admin(view_func):
# user_profile to the view function's arguments list, since we have to
# look it up anyway. It is deprecated in favor on the REST API
# versions.
def authenticated_api_view(is_webhook=False):
# type: (bool) -> Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]
def _wrapped_view_func(view_func):
# type: (Callable[..., HttpResponse]) -> Callable[..., HttpResponse]
def authenticated_api_view(is_webhook: bool=False) -> WrappedViewFuncT:
def _wrapped_view_func(view_func: Callable[..., HttpResponse]) -> Callable[..., HttpResponse]:
@csrf_exempt
@require_post
@has_request_variables
@ -474,14 +447,11 @@ def authenticated_api_view(is_webhook=False):
# A more REST-y authentication decorator, using, in particular, HTTP Basic
# authentication.
def authenticated_rest_api_view(is_webhook=False):
# type: (bool) -> Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]
def _wrapped_view_func(view_func):
# type: (Callable[..., HttpResponse]) -> Callable[..., HttpResponse]
def authenticated_rest_api_view(is_webhook: bool=False) -> WrappedViewFuncT:
def _wrapped_view_func(view_func: Callable[..., HttpResponse]) -> Callable[..., HttpResponse]:
@csrf_exempt
@wraps(view_func)
def _wrapped_func_arguments(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def _wrapped_func_arguments(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
# First try block attempts to get the credentials we need to do authentication
try:
# Grab the base64-encoded authentication string, decode it, and split it into
@ -507,11 +477,9 @@ def authenticated_rest_api_view(is_webhook=False):
return _wrapped_func_arguments
return _wrapped_view_func
def process_as_post(view_func):
# type: (ViewFuncT) -> ViewFuncT
def process_as_post(view_func: ViewFuncT) -> ViewFuncT:
@wraps(view_func)
def _wrapped_view_func(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def _wrapped_view_func(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
# Adapted from django/http/__init__.py.
# So by default Django doesn't populate request.POST for anything besides
# POST requests. We want this dict populated for PATCH/PUT, so we have to
@ -539,8 +507,9 @@ def process_as_post(view_func):
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
def authenticate_log_and_execute_json(request, view_func, *args, **kwargs):
# type: (HttpRequest, Callable[..., HttpResponse], *Any, **Any) -> HttpResponse
def authenticate_log_and_execute_json(request: HttpRequest,
view_func: Callable[..., HttpResponse],
*args: Any, **kwargs: Any) -> HttpResponse:
if not request.user.is_authenticated:
return json_error(_("Not logged in"), status=401)
user_profile = request.user
@ -557,8 +526,7 @@ def authenticate_log_and_execute_json(request, view_func, *args, **kwargs):
# Checks if the request is a POST request and that the user is logged
# in. If not, return an error (the @login_required behavior of
# redirecting to a login page doesn't make sense for json views)
def authenticated_json_post_view(view_func):
# type: (ViewFuncT) -> ViewFuncT
def authenticated_json_post_view(view_func: ViewFuncT) -> ViewFuncT:
@require_post
@has_request_variables
@wraps(view_func)
@ -568,8 +536,7 @@ def authenticated_json_post_view(view_func):
return authenticate_log_and_execute_json(request, view_func, *args, **kwargs)
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
def authenticated_json_view(view_func):
# type: (ViewFuncT) -> ViewFuncT
def authenticated_json_view(view_func: ViewFuncT) -> ViewFuncT:
@wraps(view_func)
def _wrapped_view_func(request,
*args, **kwargs):
@ -577,20 +544,17 @@ def authenticated_json_view(view_func):
return authenticate_log_and_execute_json(request, view_func, *args, **kwargs)
return _wrapped_view_func # type: ignore # https://github.com/python/mypy/issues/1927
def is_local_addr(addr):
# type: (Text) -> bool
def is_local_addr(addr: Text) -> bool:
return addr in ('127.0.0.1', '::1')
# These views are used by the main Django server to notify the Tornado server
# of events. We protect them from the outside world by checking a shared
# secret, and also the originating IP (for now).
def authenticate_notify(request):
# type: (HttpRequest) -> bool
def authenticate_notify(request: HttpRequest) -> bool:
return (is_local_addr(request.META['REMOTE_ADDR']) and
request.POST.get('secret') == settings.SHARED_SECRET)
def client_is_exempt_from_rate_limiting(request):
# type: (HttpRequest) -> bool
def client_is_exempt_from_rate_limiting(request: HttpRequest) -> bool:
# Don't rate limit requests from Django that come from our own servers,
# and don't rate-limit dev instances
@ -598,20 +562,17 @@ def client_is_exempt_from_rate_limiting(request):
(is_local_addr(request.META['REMOTE_ADDR']) or
settings.DEBUG_RATE_LIMITING))
def internal_notify_view(is_tornado_view):
# type: (bool) -> Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]
def internal_notify_view(is_tornado_view: bool) -> WrappedViewFuncT:
# The typing here could be improved by using the Extended Callable types:
# https://mypy.readthedocs.io/en/latest/kinds_of_types.html#extended-callable-types
"""Used for situations where something running on the Zulip server
needs to make a request to the (other) Django/Tornado processes running on
the server."""
def _wrapped_view_func(view_func):
# type: (Callable[..., HttpResponse]) -> Callable[..., HttpResponse]
def _wrapped_view_func(view_func: Callable[..., HttpResponse]) -> Callable[..., HttpResponse]:
@csrf_exempt
@require_post
@wraps(view_func)
def _wrapped_func_arguments(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def _wrapped_func_arguments(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
if not authenticate_notify(request):
return json_error(_('Access denied'), status=403)
is_tornado_request = hasattr(request, '_tornado_handler')
@ -627,52 +588,46 @@ def internal_notify_view(is_tornado_view):
return _wrapped_view_func
# Converter functions for use with has_request_variables
def to_non_negative_int(s):
# type: (Text) -> int
def to_non_negative_int(s: Text) -> int:
x = int(s)
if x < 0:
raise ValueError("argument is negative")
return x
def to_not_negative_int_or_none(s):
# type: (Text) -> Optional[int]
def to_not_negative_int_or_none(s: Text) -> Optional[int]:
if s:
return to_non_negative_int(s)
return None
def flexible_boolean(boolean):
# type: (Text) -> bool
def flexible_boolean(boolean: Text) -> bool:
"""Returns True for any of "1", "true", or "True". Returns False otherwise."""
if boolean in ("1", "true", "True"):
return True
else:
return False
def to_utc_datetime(timestamp):
# type: (Text) -> datetime.datetime
def to_utc_datetime(timestamp: Text) -> datetime.datetime:
return timestamp_to_datetime(float(timestamp))
WrapperT = Callable[[Callable[..., ReturnT]], Callable[..., ReturnT]]
def statsd_increment(counter, val=1):
# type: (Text, int) -> Callable[[Callable[..., ReturnT]], Callable[..., ReturnT]]
"""Increments a statsd counter on completion of the
decorated function.
Pass the name of the counter to this decorator-returning function."""
def wrapper(func):
# type: (Callable[..., ReturnT]) -> Callable[..., ReturnT]
def wrapper(func: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
@wraps(func)
def wrapped_func(*args, **kwargs):
# type: (*Any, **Any) -> ReturnT
def wrapped_func(*args: Any, **kwargs: Any) -> ReturnT:
ret = func(*args, **kwargs)
statsd.incr(counter, val)
return ret
return wrapped_func
return wrapper
def rate_limit_user(request, user, domain):
# type: (HttpRequest, UserProfile, Text) -> None
def rate_limit_user(request: HttpRequest, user: UserProfile, domain: Text) -> None:
"""Returns whether or not a user was rate limited. Will raise a RateLimited exception
if the user has been rate limited, otherwise returns and modifies request to contain
the rate limit information"""
@ -693,17 +648,14 @@ def rate_limit_user(request, user, domain):
request._ratelimit_remaining = calls_remaining
request._ratelimit_secs_to_freedom = time_reset
def rate_limit(domain='all'):
# type: (Text) -> Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]
def rate_limit(domain: Text='all') -> Callable[[Callable[..., HttpResponse]], Callable[..., HttpResponse]]:
"""Rate-limits a view. Takes an optional 'domain' param if you wish to
rate limit different types of API calls independently.
Returns a decorator"""
def wrapper(func):
# type: (Callable[..., HttpResponse]) -> Callable[..., HttpResponse]
def wrapper(func: Callable[..., HttpResponse]) -> Callable[..., HttpResponse]:
@wraps(func)
def wrapped_func(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse
def wrapped_func(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
# It is really tempting to not even wrap our original function
# when settings.RATE_LIMITING is False, but it would make

View File

@ -5,8 +5,7 @@ from django.http import HttpRequest
from django.views.debug import SafeExceptionReporterFilter
class ZulipExceptionReporterFilter(SafeExceptionReporterFilter):
def get_post_parameters(self, request):
# type: (HttpRequest) -> Dict[str, Any]
def get_post_parameters(self, request: HttpRequest) -> Dict[str, Any]:
filtered_post = SafeExceptionReporterFilter.get_post_parameters(self, request).copy()
filtered_vars = ['content', 'secret', 'password', 'key', 'api-key', 'subject', 'stream',
'subscriptions', 'to', 'csrfmiddlewaretoken', 'api_key']

View File

@ -42,8 +42,7 @@ WRONG_SUBDOMAIN_ERROR = "Your Zulip account is not a member of the " + \
"organization associated with this subdomain. " + \
"Please contact %s with any questions!" % (FromAddress.SUPPORT,)
def email_is_not_mit_mailing_list(email):
# type: (Text) -> None
def email_is_not_mit_mailing_list(email: Text) -> None:
"""Prevent MIT mailing lists from signing up for Zulip"""
if "@mit.edu" in email:
username = email.rsplit("@", 1)[0]
@ -56,8 +55,7 @@ def email_is_not_mit_mailing_list(email):
else:
raise AssertionError("Unexpected DNS error")
def check_subdomain_available(subdomain):
# type: (str) -> None
def check_subdomain_available(subdomain: str) -> None:
error_strings = {
'too short': _("Subdomain needs to have length 3 or greater."),
'extremal dash': _("Subdomain cannot start or end with a '-'."),
@ -86,9 +84,7 @@ class RegistrationForm(forms.Form):
password = forms.CharField(widget=forms.PasswordInput, max_length=MAX_PASSWORD_LENGTH)
realm_subdomain = forms.CharField(max_length=Realm.MAX_REALM_SUBDOMAIN_LENGTH, required=False)
def __init__(self, *args, **kwargs):
# type: (*Any, **Any) -> None
def __init__(self, *args: Any, **kwargs: Any) -> None:
# Since the superclass doesn't except random extra kwargs, we
# remove it from the kwargs dict before initializing.
self.realm_creation = kwargs['realm_creation']
@ -101,15 +97,13 @@ class RegistrationForm(forms.Form):
max_length=Realm.MAX_REALM_NAME_LENGTH,
required=self.realm_creation)
def clean_full_name(self):
# type: () -> Text
def clean_full_name(self) -> Text:
try:
return check_full_name(self.cleaned_data['full_name'])
except JsonableError as e:
raise ValidationError(e.msg)
def clean_realm_subdomain(self):
# type: () -> str
def clean_realm_subdomain(self) -> str:
if not self.realm_creation:
# This field is only used if realm_creation
return ""
@ -127,14 +121,12 @@ class ToSForm(forms.Form):
class HomepageForm(forms.Form):
email = forms.EmailField()
def __init__(self, *args, **kwargs):
# type: (*Any, **Any) -> None
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.realm = kwargs.pop('realm', None)
self.from_multiuse_invite = kwargs.pop('from_multiuse_invite', False)
super().__init__(*args, **kwargs)
def clean_email(self):
# type: () -> str
def clean_email(self) -> str:
"""Returns the email if and only if the user's email address is
allowed to join the realm they are trying to join."""
email = self.cleaned_data['email']
@ -166,8 +158,7 @@ class HomepageForm(forms.Form):
return email
def email_is_not_disposable(email):
# type: (Text) -> None
def email_is_not_disposable(email: Text) -> None:
if is_disposable_domain(email_to_domain(email)):
raise ValidationError(_("Please use your real email address."))
@ -177,8 +168,7 @@ class RealmCreationForm(forms.Form):
email_is_not_disposable])
class LoggingSetPasswordForm(SetPasswordForm):
def save(self, commit=True):
# type: (bool) -> UserProfile
def save(self, commit: bool=True) -> UserProfile:
do_change_password(self.user, self.cleaned_data['new_password1'],
commit=commit)
return self.user
@ -251,8 +241,7 @@ class CreateUserForm(forms.Form):
email = forms.EmailField()
class OurAuthenticationForm(AuthenticationForm):
def clean(self):
# type: () -> Dict[str, Any]
def clean(self) -> Dict[str, Any]:
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
@ -300,16 +289,14 @@ class OurAuthenticationForm(AuthenticationForm):
return field_name
class MultiEmailField(forms.Field):
def to_python(self, emails):
# type: (Text) -> List[Text]
def to_python(self, emails: Text) -> List[Text]:
"""Normalize data to a list of strings."""
if not emails:
return []
return [email.strip() for email in emails.split(',')]
def validate(self, emails):
# type: (List[Text]) -> None
def validate(self, emails: List[Text]) -> None:
"""Check if value consists only of valid emails."""
super().validate(emails)
for email in emails:
@ -319,8 +306,7 @@ class FindMyTeamForm(forms.Form):
emails = MultiEmailField(
help_text=_("Add up to 10 comma-separated email addresses."))
def clean_emails(self):
# type: () -> List[Text]
def clean_emails(self) -> List[Text]:
emails = self.cleaned_data['emails']
if len(emails) > 10:
raise forms.ValidationError(_("Please enter at most 10 emails."))

View File

@ -13,8 +13,7 @@ from django.views.debug import ExceptionReporter, get_exception_reporter_filter
from zerver.lib.queue import queue_json_publish
def add_request_metadata(report, request):
# type: (Dict[str, Any], HttpRequest) -> None
def add_request_metadata(report: Dict[str, Any], request: HttpRequest) -> None:
report['path'] = request.path
report['method'] = request.method
report['remote_addr'] = request.META.get('REMOTE_ADDR', None),
@ -60,12 +59,10 @@ class AdminZulipHandler(logging.Handler):
# adapted in part from django/utils/log.py
def __init__(self):
# type: () -> None
def __init__(self) -> None:
logging.Handler.__init__(self)
def emit(self, record):
# type: (logging.LogRecord) -> None
def emit(self, record: logging.LogRecord) -> None:
try:
if record.exc_info:
stack_trace = ''.join(traceback.format_exception(*record.exc_info)) # type: Optional[str]

View File

@ -30,8 +30,7 @@ from zerver.models import Realm, flush_per_request_caches, get_realm
logger = logging.getLogger('zulip.requests')
def record_request_stop_data(log_data):
# type: (MutableMapping[str, Any]) -> None
def record_request_stop_data(log_data: MutableMapping[str, Any]) -> None:
log_data['time_stopped'] = time.time()
log_data['remote_cache_time_stopped'] = get_remote_cache_time()
log_data['remote_cache_requests_stopped'] = get_remote_cache_requests()
@ -40,12 +39,10 @@ def record_request_stop_data(log_data):
if settings.PROFILE_ALL_REQUESTS:
log_data["prof"].disable()
def async_request_stop(request):
# type: (HttpRequest) -> None
def async_request_stop(request: HttpRequest) -> None:
record_request_stop_data(request._log_data)
def record_request_restart_data(log_data):
# type: (MutableMapping[str, Any]) -> None
def record_request_restart_data(log_data: MutableMapping[str, Any]) -> None:
if settings.PROFILE_ALL_REQUESTS:
log_data["prof"].enable()
log_data['time_restarted'] = time.time()
@ -54,16 +51,14 @@ def record_request_restart_data(log_data):
log_data['bugdown_time_restarted'] = get_bugdown_time()
log_data['bugdown_requests_restarted'] = get_bugdown_requests()
def async_request_restart(request):
# type: (HttpRequest) -> None
def async_request_restart(request: HttpRequest) -> None:
if "time_restarted" in request._log_data:
# Don't destroy data when being called from
# finish_current_handler
return
record_request_restart_data(request._log_data)
def record_request_start_data(log_data):
# type: (MutableMapping[str, Any]) -> None
def record_request_start_data(log_data: MutableMapping[str, Any]) -> None:
if settings.PROFILE_ALL_REQUESTS:
log_data["prof"] = cProfile.Profile()
log_data["prof"].enable()
@ -74,18 +69,15 @@ def record_request_start_data(log_data):
log_data['bugdown_time_start'] = get_bugdown_time()
log_data['bugdown_requests_start'] = get_bugdown_requests()
def timedelta_ms(timedelta):
# type: (float) -> float
def timedelta_ms(timedelta: float) -> float:
return timedelta * 1000
def format_timedelta(timedelta):
# type: (float) -> str
def format_timedelta(timedelta: float) -> str:
if (timedelta >= 1):
return "%.1fs" % (timedelta)
return "%.0fms" % (timedelta_ms(timedelta),)
def is_slow_query(time_delta, path):
# type: (float, Text) -> bool
def is_slow_query(time_delta: float, path: Text) -> bool:
if time_delta < 1.2:
return False
is_exempt = \
@ -232,16 +224,15 @@ class LogRequests(MiddlewareMixin):
# We primarily are doing logging using the process_view hook, but
# for some views, process_view isn't run, so we call the start
# method here too
def process_request(self, request):
# type: (HttpRequest) -> None
def process_request(self, request: HttpRequest) -> None:
maybe_tracemalloc_listen()
request._log_data = dict()
record_request_start_data(request._log_data)
if connection.connection is not None:
connection.connection.queries = []
def process_view(self, request, view_func, args, kwargs):
# type: (HttpRequest, Callable[..., HttpResponse], List[str], Dict[str, Any]) -> None
def process_view(self, request: HttpRequest, view_func: Callable[..., HttpResponse],
args: List[str], kwargs: Dict[str, Any]) -> None:
# process_request was already run; we save the initialization
# time (i.e. the time between receiving the request and
# figuring out which view function to call, which is primarily
@ -253,8 +244,8 @@ class LogRequests(MiddlewareMixin):
if connection.connection is not None:
connection.connection.queries = []
def process_response(self, request, response):
# type: (HttpRequest, StreamingHttpResponse) -> StreamingHttpResponse
def process_response(self, request: HttpRequest,
response: StreamingHttpResponse) -> StreamingHttpResponse:
# The reverse proxy might have sent us the real external IP
remote_ip = request.META.get('HTTP_X_REAL_IP')
if remote_ip is None:
@ -283,8 +274,7 @@ class LogRequests(MiddlewareMixin):
return response
class JsonErrorHandler(MiddlewareMixin):
def process_exception(self, request, exception):
# type: (HttpRequest, Exception) -> Optional[HttpResponse]
def process_exception(self, request: HttpRequest, exception: Exception) -> Optional[HttpResponse]:
if isinstance(exception, JsonableError):
return json_response_from_error(exception)
if request.error_format == "JSON":
@ -293,12 +283,11 @@ class JsonErrorHandler(MiddlewareMixin):
return None
class TagRequests(MiddlewareMixin):
def process_view(self, request, view_func, args, kwargs):
# type: (HttpRequest, Callable[..., HttpResponse], List[str], Dict[str, Any]) -> None
def process_view(self, request: HttpRequest, view_func: Callable[..., HttpResponse],
args: List[str], kwargs: Dict[str, Any]) -> None:
self.process_request(request)
def process_request(self, request):
# type: (HttpRequest) -> None
def process_request(self, request: HttpRequest) -> None:
if request.path.startswith("/api/") or request.path.startswith("/json/"):
request.error_format = "JSON"
else:
@ -309,25 +298,21 @@ class CsrfFailureError(JsonableError):
code = ErrorCode.CSRF_FAILED
data_fields = ['reason']
def __init__(self, reason):
# type: (Text) -> None
def __init__(self, reason: Text) -> None:
self.reason = reason # type: Text
@staticmethod
def msg_format():
# type: () -> Text
def msg_format() -> Text:
return _("CSRF Error: {reason}")
def csrf_failure(request, reason=""):
# type: (HttpRequest, Text) -> HttpResponse
def csrf_failure(request: HttpRequest, reason: Text="") -> HttpResponse:
if request.error_format == "JSON":
return json_response_from_error(CsrfFailureError(reason))
else:
return html_csrf_failure(request, reason)
class RateLimitMiddleware(MiddlewareMixin):
def process_response(self, request, response):
# type: (HttpRequest, HttpResponse) -> HttpResponse
def process_response(self, request: HttpRequest, response: HttpResponse) -> HttpResponse:
if not settings.RATE_LIMITING:
return response
@ -342,8 +327,7 @@ class RateLimitMiddleware(MiddlewareMixin):
response['X-RateLimit-Remaining'] = str(request._ratelimit_remaining)
return response
def process_exception(self, request, exception):
# type: (HttpRequest, Exception) -> Optional[HttpResponse]
def process_exception(self, request: HttpRequest, exception: Exception) -> Optional[HttpResponse]:
if isinstance(exception, RateLimited):
resp = json_error(
_("API usage exceeded rate limit"),
@ -355,16 +339,14 @@ class RateLimitMiddleware(MiddlewareMixin):
return None
class FlushDisplayRecipientCache(MiddlewareMixin):
def process_response(self, request, response):
# type: (HttpRequest, HttpResponse) -> HttpResponse
def process_response(self, request: HttpRequest, response: HttpResponse) -> HttpResponse:
# We flush the per-request caches after every request, so they
# are not shared at all between requests.
flush_per_request_caches()
return response
class SessionHostDomainMiddleware(SessionMiddleware):
def process_response(self, request, response):
# type: (HttpRequest, HttpResponse) -> HttpResponse
def process_response(self, request: HttpRequest, response: HttpResponse) -> HttpResponse:
try:
request.get_host()
except DisallowedHost:
@ -431,8 +413,7 @@ class SetRemoteAddrFromForwardedFor(MiddlewareMixin):
is set in the request, then it has properly been set by NGINX.
Therefore HTTP_X_FORWARDED_FOR's value is trusted.
"""
def process_request(self, request):
# type: (HttpRequest) -> None
def process_request(self, request: HttpRequest) -> None:
try:
real_ip = request.META['HTTP_X_FORWARDED_FOR']
except KeyError:

File diff suppressed because it is too large Load Diff

View File

@ -12,8 +12,7 @@ from django.utils.timezone import now as timezone_now
from zerver.lib.send_email import FromAddress, send_email
from zerver.models import UserProfile
def get_device_browser(user_agent):
# type: (str) -> Optional[str]
def get_device_browser(user_agent: str) -> Optional[str]:
user_agent = user_agent.lower()
if "zulip" in user_agent:
return "Zulip"
@ -35,8 +34,7 @@ def get_device_browser(user_agent):
return None
def get_device_os(user_agent):
# type: (str) -> Optional[str]
def get_device_os(user_agent: str) -> Optional[str]:
user_agent = user_agent.lower()
if "windows" in user_agent:
return "Windows"
@ -55,9 +53,7 @@ def get_device_os(user_agent):
@receiver(user_logged_in, dispatch_uid="only_on_login")
def email_on_new_login(sender, user, request, **kwargs):
# type: (Any, UserProfile, Any, **Any) -> None
def email_on_new_login(sender: Any, user: UserProfile, request: Any, **kwargs: Any) -> None:
# We import here to minimize the dependencies of this module,
# since it runs as part of `manage.py` initialization
from zerver.context_processors import common_context

View File

@ -11,8 +11,8 @@ from pipeline.storage import PipelineMixin
from zerver.lib.str_utils import force_str
class AddHeaderMixin:
def post_process(self, paths, dry_run=False, **kwargs):
# type: (Dict[str, Tuple[ZulipStorage, str]], bool, **Any) -> List[Tuple[str, str, bool]]
def post_process(self, paths: Dict[str, Tuple['ZulipStorage', str]], dry_run: bool=False,
**kwargs: Any) -> List[Tuple[str, str, bool]]:
if dry_run:
return []
@ -58,8 +58,8 @@ class AddHeaderMixin:
class RemoveUnminifiedFilesMixin:
def post_process(self, paths, dry_run=False, **kwargs):
# type: (Dict[str, Tuple[ZulipStorage, str]], bool, **Any) -> List[Tuple[str, str, bool]]
def post_process(self, paths: Dict[str, Tuple['ZulipStorage', str]], dry_run: bool=False,
**kwargs: Any) -> List[Tuple[str, str, bool]]:
if dry_run:
return []
@ -88,8 +88,7 @@ if settings.PRODUCTION:
"staticfiles.json")
orig_path = ManifestStaticFilesStorage.path
def path(self, name):
# type: (Any, str) -> str
def path(self: Any, name: str) -> str:
if name == ManifestStaticFilesStorage.manifest_name:
return name
return orig_path(self, name)