mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 13:33:24 +00:00
mypy: Convert several directories to use typing.Text.
Specifically, these directories are converted: [analytics/, scripts/, tools/, zerver/management/, zilencer/, zproject/]
This commit is contained in:
committed by
Tim Abbott
parent
2288120155
commit
beaa62cafa
@@ -1,8 +1,7 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import logging
|
||||
from typing import Any, Set, Tuple, Optional
|
||||
from six import text_type
|
||||
from typing import Any, Set, Tuple, Optional, Text
|
||||
|
||||
from django.contrib.auth.backends import RemoteUserBackend
|
||||
from django.conf import settings
|
||||
@@ -25,7 +24,7 @@ from django.contrib.auth import authenticate
|
||||
from zerver.lib.utils import check_subdomain, get_subdomain
|
||||
|
||||
def pad_method_dict(method_dict):
|
||||
# type: (Dict[text_type, bool]) -> Dict[text_type, bool]
|
||||
# type: (Dict[Text, bool]) -> Dict[Text, bool]
|
||||
"""Pads an authentication methods dict to contain all auth backends
|
||||
supported by the software, regardless of whether they are
|
||||
configured on this server"""
|
||||
@@ -35,7 +34,7 @@ def pad_method_dict(method_dict):
|
||||
return method_dict
|
||||
|
||||
def auth_enabled_helper(backends_to_check, realm):
|
||||
# type: (List[text_type], Optional[Realm]) -> bool
|
||||
# type: (List[Text], Optional[Realm]) -> bool
|
||||
if realm is not None:
|
||||
enabled_method_dict = realm.authentication_methods_dict()
|
||||
pad_method_dict(enabled_method_dict)
|
||||
@@ -74,7 +73,7 @@ def github_auth_enabled(realm=None):
|
||||
return auth_enabled_helper([u'GitHub'], realm)
|
||||
|
||||
def common_get_active_user_by_email(email, return_data=None):
|
||||
# type: (text_type, Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
# type: (Text, Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
try:
|
||||
user_profile = get_user_profile_by_email(email)
|
||||
except UserProfile.DoesNotExist:
|
||||
@@ -99,14 +98,14 @@ class ZulipAuthMixin(object):
|
||||
return None
|
||||
|
||||
class SocialAuthMixin(ZulipAuthMixin):
|
||||
auth_backend_name = None # type: text_type
|
||||
auth_backend_name = None # type: Text
|
||||
|
||||
def get_email_address(self, *args, **kwargs):
|
||||
# type: (*Any, **Any) -> text_type
|
||||
# type: (*Any, **Any) -> Text
|
||||
raise NotImplementedError
|
||||
|
||||
def get_full_name(self, *args, **kwargs):
|
||||
# type: (*Any, **Any) -> text_type
|
||||
# type: (*Any, **Any) -> Text
|
||||
raise NotImplementedError
|
||||
|
||||
def authenticate(self, *args, **kwargs):
|
||||
@@ -172,7 +171,7 @@ class ZulipDummyBackend(ZulipAuthMixin):
|
||||
|
||||
def authenticate(self, username=None, realm_subdomain=None, use_dummy_backend=False,
|
||||
return_data=None):
|
||||
# type: (Optional[text_type], Optional[text_type], bool, Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
# type: (Optional[Text], Optional[Text], bool, Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
if use_dummy_backend:
|
||||
user_profile = common_get_active_user_by_email(username)
|
||||
if user_profile is None:
|
||||
@@ -192,7 +191,7 @@ class EmailAuthBackend(ZulipAuthMixin):
|
||||
"""
|
||||
|
||||
def authenticate(self, username=None, password=None, realm_subdomain=None, return_data=None):
|
||||
# type: (Optional[text_type], Optional[str], Optional[text_type], Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
# type: (Optional[Text], Optional[str], Optional[Text], Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
""" Authenticate a user based on email address as the user name. """
|
||||
if username is None or password is None:
|
||||
# Return immediately. Otherwise we will look for a SQL row with
|
||||
@@ -231,7 +230,7 @@ class GoogleMobileOauth2Backend(ZulipAuthMixin):
|
||||
"""
|
||||
|
||||
def authenticate(self, google_oauth2_token=None, realm_subdomain=None, return_data={}):
|
||||
# type: (Optional[str], Optional[text_type], Dict[str, Any]) -> Optional[UserProfile]
|
||||
# type: (Optional[str], Optional[Text], Dict[str, Any]) -> Optional[UserProfile]
|
||||
try:
|
||||
token_payload = googleapiclient.verify_id_token(google_oauth2_token, settings.GOOGLE_CLIENT_ID)
|
||||
except AppIdentityError:
|
||||
@@ -262,7 +261,7 @@ class ZulipRemoteUserBackend(RemoteUserBackend):
|
||||
create_unknown_user = False
|
||||
|
||||
def authenticate(self, remote_user, realm_subdomain=None):
|
||||
# type: (str, Optional[text_type]) -> Optional[UserProfile]
|
||||
# type: (str, Optional[Text]) -> Optional[UserProfile]
|
||||
if not remote_user:
|
||||
return None
|
||||
|
||||
@@ -304,7 +303,7 @@ class ZulipLDAPAuthBackendBase(ZulipAuthMixin, LDAPBackend):
|
||||
return set()
|
||||
|
||||
def django_to_ldap_username(self, username):
|
||||
# type: (text_type) -> text_type
|
||||
# type: (Text) -> Text
|
||||
if settings.LDAP_APPEND_DOMAIN:
|
||||
if not username.endswith("@" + settings.LDAP_APPEND_DOMAIN):
|
||||
raise ZulipLDAPException("Username does not match LDAP domain.")
|
||||
@@ -319,7 +318,7 @@ class ZulipLDAPAuthBackendBase(ZulipAuthMixin, LDAPBackend):
|
||||
|
||||
class ZulipLDAPAuthBackend(ZulipLDAPAuthBackendBase):
|
||||
def authenticate(self, username, password, realm_subdomain=None, return_data=None):
|
||||
# type: (text_type, str, Optional[text_type], Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
# type: (Text, str, Optional[Text], Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
try:
|
||||
username = self.django_to_ldap_username(username)
|
||||
user_profile = ZulipLDAPAuthBackendBase.authenticate(self, username, password)
|
||||
@@ -360,14 +359,14 @@ class ZulipLDAPAuthBackend(ZulipLDAPAuthBackendBase):
|
||||
# Just like ZulipLDAPAuthBackend, but doesn't let you log in.
|
||||
class ZulipLDAPUserPopulator(ZulipLDAPAuthBackendBase):
|
||||
def authenticate(self, username, password, realm_subdomain=None):
|
||||
# type: (text_type, str, Optional[text_type]) -> None
|
||||
# type: (Text, str, Optional[Text]) -> None
|
||||
return None
|
||||
|
||||
class DevAuthBackend(ZulipAuthMixin):
|
||||
# Allow logging in as any user without a password.
|
||||
# This is used for convenience when developing Zulip.
|
||||
def authenticate(self, username, realm_subdomain=None, return_data=None):
|
||||
# type: (text_type, Optional[text_type], Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
# type: (Text, Optional[Text], Optional[Dict[str, Any]]) -> Optional[UserProfile]
|
||||
user_profile = common_get_active_user_by_email(username, return_data=return_data)
|
||||
if user_profile is None:
|
||||
return None
|
||||
@@ -379,14 +378,14 @@ class GitHubAuthBackend(SocialAuthMixin, GithubOAuth2):
|
||||
auth_backend_name = u"GitHub"
|
||||
|
||||
def get_email_address(self, *args, **kwargs):
|
||||
# type: (*Any, **Any) -> Optional[text_type]
|
||||
# type: (*Any, **Any) -> Optional[Text]
|
||||
try:
|
||||
return kwargs['response']['email']
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
def get_full_name(self, *args, **kwargs):
|
||||
# type: (*Any, **Any) -> text_type
|
||||
# type: (*Any, **Any) -> Text
|
||||
try:
|
||||
return kwargs['response']['name']
|
||||
except KeyError:
|
||||
@@ -432,4 +431,4 @@ AUTH_BACKEND_NAME_MAP = {
|
||||
u'Google': GoogleMobileOauth2Backend,
|
||||
u'LDAP': ZulipLDAPAuthBackend,
|
||||
u'RemoteUser': ZulipRemoteUserBackend,
|
||||
} # type: Dict[text_type, Any]
|
||||
} # type: Dict[Text, Any]
|
||||
|
||||
Reference in New Issue
Block a user