diff --git a/zerver/lib/bugdown/__init__.py b/zerver/lib/bugdown/__init__.py index 91a3fc9a9f..d93af0c41e 100644 --- a/zerver/lib/bugdown/__init__.py +++ b/zerver/lib/bugdown/__init__.py @@ -65,7 +65,7 @@ class BugdownRenderingException(Exception): pass def url_embed_preview_enabled_for_realm(message): - # type: (Message) -> bool + # type: (Optional[Message]) -> bool if message is not None: realm = message.get_realm() else: diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 0aa07f5e85..be91921b3d 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -92,7 +92,7 @@ class UploadSerializeMixin(SerializeMixin): class ZulipTestCase(TestCase): # Ensure that the test system just shows us diffs - maxDiff = None # type: int + maxDiff = None # type: Optional[int] ''' WRAPPER_COMMENT: diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.py index 00502df192..7f3df7b077 100644 --- a/zerver/lib/test_helpers.py +++ b/zerver/lib/test_helpers.py @@ -256,7 +256,7 @@ class POSTRequestMock(object): method = "POST" def __init__(self, post_data, user_profile): - # type: (Dict[str, Any], UserProfile) -> None + # type: (Dict[str, Any], Optional[UserProfile]) -> None self.GET = {} # type: Dict[str, Any] self.POST = post_data self.user = user_profile diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py index 432831f4b9..d2178ccdbc 100644 --- a/zerver/lib/validator.py +++ b/zerver/lib/validator.py @@ -79,7 +79,7 @@ def check_none_or(sub_validator): return f def check_list(sub_validator, length=None): - # type: (Validator, Optional[int]) -> Validator + # type: (Optional[Validator], Optional[int]) -> Validator def f(var_name, val): # type: (str, Any) -> Optional[str] if not isinstance(val, list): diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index f3f7b89f5f..92e9f50a81 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -455,6 +455,7 @@ class GitHubAuthBackendTest(ZulipTestCase): mock.patch('zerver.views.auth.login'): response = dict(email=self.email, name=self.name) result = self.backend.do_auth(response=response) + assert(result is not None) self.assertNotIn('subdomain=1', result.url) def test_github_backend_do_auth_with_non_existing_subdomain(self): @@ -465,6 +466,7 @@ class GitHubAuthBackendTest(ZulipTestCase): self.backend.strategy.session_set('subdomain', 'test') response = dict(email=self.email, name=self.name) result = self.backend.do_auth(response=response) + assert(result is not None) self.assertIn('subdomain=1', result.url) def test_github_backend_do_auth_with_subdomains(self): @@ -475,6 +477,7 @@ class GitHubAuthBackendTest(ZulipTestCase): self.backend.strategy.session_set('subdomain', 'zulip') response = dict(email=self.email, name=self.name) result = self.backend.do_auth(response=response) + assert(result is not None) self.assertEqual('http://zulip.testserver/accounts/login/subdomain/', result.url) def test_github_backend_do_auth_for_default(self): @@ -605,7 +608,7 @@ class GitHubAuthBackendTest(ZulipTestCase): self.backend.strategy.session_get = lambda k: session_data.get(k) def do_auth(*args, **kwargs): - # type: (*Any, **Any) -> UserProfile + # type: (*Any, **Any) -> None return_data = kwargs['return_data'] return_data['valid_attestation'] = True return None @@ -631,7 +634,7 @@ class GitHubAuthBackendTest(ZulipTestCase): self.backend.strategy.session_get = lambda k: session_data.get(k) def do_auth(*args, **kwargs): - # type: (*Any, **Any) -> UserProfile + # type: (*Any, **Any) -> None return_data = kwargs['return_data'] return_data['valid_attestation'] = True return None @@ -656,7 +659,7 @@ class GitHubAuthBackendTest(ZulipTestCase): self.backend.strategy.session_get = lambda k: session_data.get(k) def do_auth(*args, **kwargs): - # type: (*Any, **Any) -> UserProfile + # type: (*Any, **Any) -> None return_data = kwargs['return_data'] return_data['valid_attestation'] = True return None @@ -1062,7 +1065,7 @@ class GoogleLoginTest(GoogleOAuthTest): # type: () -> None token_response = ResponseMock(400, {}) with mock.patch("logging.warning") as m: - result = self.google_oauth2_test(token_response, None) + result = self.google_oauth2_test(token_response, ResponseMock(500, {})) self.assertEqual(result.status_code, 400) self.assertEqual(m.call_args_list[0][0][0], "User error converting Google oauth2 login to token: Response text") @@ -1071,7 +1074,7 @@ class GoogleLoginTest(GoogleOAuthTest): # type: () -> None token_response = ResponseMock(500, {}) with mock.patch("logging.error") as m: - result = self.google_oauth2_test(token_response, None) + result = self.google_oauth2_test(token_response, ResponseMock(500, {})) self.assertEqual(result.status_code, 400) self.assertEqual(m.call_args_list[0][0][0], "Could not convert google oauth2 code to access_token: Response text") @@ -1760,6 +1763,8 @@ class TestLDAP(ZulipTestCase): AUTH_LDAP_BIND_PASSWORD='', AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'): user_profile = self.backend.authenticate('hamlet@zulip.com', 'testing') + + assert(user_profile is not None) self.assertEqual(user_profile.email, 'hamlet@zulip.com') @override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',)) @@ -1932,6 +1937,7 @@ class TestLDAP(ZulipTestCase): AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'): user_profile = self.backend.authenticate('hamlet@zulip.com', 'testing', realm_subdomain=None) + assert(user_profile is not None) self.assertEqual(user_profile.email, 'hamlet@zulip.com') @override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',)) @@ -1949,6 +1955,7 @@ class TestLDAP(ZulipTestCase): AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'): user_profile = self.backend.authenticate('hamlet@zulip.com', 'testing', realm_subdomain='zulip') + assert(user_profile is not None) self.assertEqual(user_profile.email, 'hamlet@zulip.com') @override_settings(AUTHENTICATION_BACKENDS=('zproject.backends.ZulipLDAPAuthBackend',)) @@ -1967,6 +1974,7 @@ class TestLDAP(ZulipTestCase): AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=acme,dc=com'): user_profile = self.backend.authenticate('nonexisting@acme.com', 'testing', realm_subdomain='zulip') + assert(user_profile is not None) self.assertEqual(user_profile.email, 'nonexisting@acme.com') self.assertEqual(user_profile.full_name, 'NonExisting') self.assertEqual(user_profile.realm.string_id, 'zulip') diff --git a/zerver/tests/test_bugdown.py b/zerver/tests/test_bugdown.py index edb03e1c06..97557ee61f 100644 --- a/zerver/tests/test_bugdown.py +++ b/zerver/tests/test_bugdown.py @@ -177,7 +177,7 @@ class BugdownTest(ZulipTestCase): # type: () -> None format_tests, linkify_tests = self.load_bugdown_tests() - self.maxDiff = None # type: Optional[int] + self.maxDiff = None for name, test in six.iteritems(format_tests): converted = bugdown_convert(test['input']) @@ -197,7 +197,7 @@ class BugdownTest(ZulipTestCase): return payload % ("%s" % (href, target, href, url),) print("Running Bugdown Linkify tests") - self.maxDiff = None # type: Optional[int] + self.maxDiff = None with mock.patch('zerver.lib.url_preview.preview.link_embed_data_from_cache', return_value=None): for inline_url, reference, url in linkify_tests: try: @@ -301,8 +301,7 @@ class BugdownTest(ZulipTestCase): ret = bugdown.url_embed_preview_enabled_for_realm(message) self.assertEqual(ret, realm.inline_image_preview) - message = None - ret = bugdown.url_embed_preview_enabled_for_realm(message) + ret = bugdown.url_embed_preview_enabled_for_realm(None) self.assertEqual(ret, True) def test_inline_dropbox(self): diff --git a/zerver/tests/test_events.py b/zerver/tests/test_events.py index 2494b29da1..a854ba33d3 100644 --- a/zerver/tests/test_events.py +++ b/zerver/tests/test_events.py @@ -345,6 +345,8 @@ class EventsRegisterTest(ZulipTestCase): super(EventsRegisterTest, self).setUp() self.user_profile = self.example_user('hamlet') self.maxDiff = None # type: Optional[int] + user_profile = get_user_profile_by_email('hamlet@zulip.com') + maxDiff = None def create_bot(self, email): # type: (str) -> UserProfile diff --git a/zerver/tests/test_messages.py b/zerver/tests/test_messages.py index 3705f00778..69b6b954df 100644 --- a/zerver/tests/test_messages.py +++ b/zerver/tests/test_messages.py @@ -1594,6 +1594,7 @@ class MirroredMessageUsersTest(ZulipTestCase): (valid_input, mirror_sender) = \ create_mirrored_message_users(request, user, recipients) + assert(mirror_sender is not None) self.assertTrue(valid_input) self.assertEqual(mirror_sender.email, sender_email) self.assertTrue(mirror_sender.is_mirror_dummy) @@ -1945,6 +1946,7 @@ class CheckMessageTest(ZulipTestCase): # Try sending to stream that exists with no subscribers longer # after; this should send an error to the bot owner that the # stream doesn't exist + assert(sender.last_reminder is not None) sender.last_reminder = sender.last_reminder - datetime.timedelta(hours=1) sender.save(update_fields=["last_reminder"]) ret = check_message(sender, client, message_type_name, message_to, diff --git a/zerver/tests/test_narrow.py b/zerver/tests/test_narrow.py index 06560e92ea..35c88cfce4 100644 --- a/zerver/tests/test_narrow.py +++ b/zerver/tests/test_narrow.py @@ -455,7 +455,7 @@ class GetOldMessagesTest(ZulipTestCase): if message['id'] == message_id: message_to_assert = message break - + assert(message_to_assert is not None) self.assertEqual(len(message_to_assert['reactions']), 1) self.assertEqual(message_to_assert['reactions'][0]['emoji_name'], reaction_name) diff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.py index 6bdc803302..3f552dbb6e 100644 --- a/zerver/tests/test_outgoing_webhook_system.py +++ b/zerver/tests/test_outgoing_webhook_system.py @@ -8,7 +8,7 @@ from typing import Any, Union, Mapping, Callable, Text, List from zerver.lib.test_classes import ZulipTestCase from zerver.models import ( - get_realm_by_email_domain, + get_realm, UserProfile, Recipient, Service, @@ -44,7 +44,7 @@ class DoRestCallTests(ZulipTestCase): # type: (mock.Mock) -> None response = ResponseMock(200, {"message": "testing"}, '') with mock.patch('requests.request', return_value=response): - do_rest_call(rest_operation, None, None) + do_rest_call(rest_operation, None, None) # type: ignore self.assertTrue(mock_succeed_with_message.called) @mock.patch('zerver.lib.outgoing_webhook.request_retry') @@ -52,7 +52,7 @@ class DoRestCallTests(ZulipTestCase): # type: (mock.Mock) -> None response = ResponseMock(500, {"message": "testing"}, '') with mock.patch('requests.request', return_value=response): - do_rest_call(rest_operation, None, None) + do_rest_call(rest_operation, None, None) # type: ignore self.assertTrue(mock_request_retry.called) @mock.patch('zerver.lib.outgoing_webhook.fail_with_message') @@ -60,7 +60,7 @@ class DoRestCallTests(ZulipTestCase): # type: (mock.Mock) -> None response = ResponseMock(400, {"message": "testing"}, '') with mock.patch('requests.request', return_value=response): - do_rest_call(rest_operation, None, None) + do_rest_call(rest_operation, None, None) # type: ignore self.assertTrue(mock_fail_with_message.called) @mock.patch('logging.info') @@ -98,7 +98,7 @@ class TestMentionMessageTrigger(ZulipTestCase): self.user_profile = self.example_user("othello") self.bot_profile = do_create_user(email="foo-bot@zulip.com", password="test", - realm=get_realm_by_email_domain("zulip.com"), + realm=get_realm("zulip"), full_name="FooBot", short_name="foo-bot", bot_type=UserProfile.OUTGOING_WEBHOOK_BOT, diff --git a/zerver/tests/test_realm_domains.py b/zerver/tests/test_realm_domains.py index e6f87b331c..968d4f592a 100644 --- a/zerver/tests/test_realm_domains.py +++ b/zerver/tests/test_realm_domains.py @@ -135,7 +135,7 @@ class RealmDomainTest(ZulipTestCase): if realm_string_id is None: self.assertIsNone(realm) else: - self.assertIsNotNone(realm) + assert(realm is not None) self.assertEqual(realm.string_id, realm_string_id) assert_and_check('user@zulip.com', 'zulip') diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index c4bce1a99a..f9b54a8343 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -1787,7 +1787,7 @@ class LoginOrAskForRegistrationTestCase(ZulipTestCase): # type: () -> None request = POSTRequestMock({}, None) email = 'new@zulip.com' - user_profile = None # type: Optional[UserProfile] + user_profile = None full_name = 'New User' invalid_subdomain = False result = login_or_register_remote_user( @@ -1805,7 +1805,7 @@ class LoginOrAskForRegistrationTestCase(ZulipTestCase): # type: () -> None request = POSTRequestMock({}, None) email = 'new@zulip.com' - user_profile = None # type: Optional[UserProfile] + user_profile = None full_name = 'New User' invalid_subdomain = True response = login_or_register_remote_user( @@ -1820,8 +1820,8 @@ class LoginOrAskForRegistrationTestCase(ZulipTestCase): def test_invalid_email(self): # type: () -> None request = POSTRequestMock({}, None) - email = None # type: Optional[Text] - user_profile = None # type: Optional[UserProfile] + email = None + user_profile = None full_name = 'New User' invalid_subdomain = False response = login_or_register_remote_user( diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py index f3b576133a..57e0426669 100644 --- a/zerver/tests/test_subs.py +++ b/zerver/tests/test_subs.py @@ -824,7 +824,7 @@ class SubscriptionPropertiesTest(ZulipTestCase): found_sub = sub break - self.assertIsNotNone(found_sub) + assert(found_sub is not None) self.assertEqual(found_sub['color'], new_color) new_subs.remove(found_sub) diff --git a/zerver/views/auth.py b/zerver/views/auth.py index b2deb62487..cb8cfb3b17 100644 --- a/zerver/views/auth.py +++ b/zerver/views/auth.py @@ -85,7 +85,7 @@ def redirect_to_subdomain_login_url(): def login_or_register_remote_user(request, remote_username, user_profile, full_name='', invalid_subdomain=False, mobile_flow_otp=None, is_signup=False): - # type: (HttpRequest, Text, UserProfile, Text, bool, Optional[str], bool) -> HttpResponse + # type: (HttpRequest, Optional[Text], Optional[UserProfile], Text, bool, Optional[str], bool) -> HttpResponse if invalid_subdomain: # Show login page with an error message return redirect_to_subdomain_login_url() diff --git a/zerver/webhooks/trello/view/board_actions.py b/zerver/webhooks/trello/view/board_actions.py index f77a39a813..2a01b7f809 100644 --- a/zerver/webhooks/trello/view/board_actions.py +++ b/zerver/webhooks/trello/view/board_actions.py @@ -32,7 +32,7 @@ def get_proper_action(payload, action_type): # type: (Mapping[str, Any], Text) -> Text if action_type == 'updateBoard': data = get_action_data(payload) - if data.get('old').get('name'): + if data['old']['name']: return CHANGE_NAME raise UnknownUpdateBoardAction() return action_type @@ -40,34 +40,34 @@ def get_proper_action(payload, action_type): def get_subject(payload): # type: (Mapping[str, Any]) -> Text data = { - 'board_name': get_action_data(payload).get('board').get('name') + 'board_name': get_action_data(payload)['board']['name'] } return TRELLO_SUBJECT_TEMPLATE.format(**data) def get_body(payload, action_type): # type: (Mapping[str, Any], Text) -> Text message_body = ACTIONS_TO_FILL_BODY_MAPPER[action_type](payload, action_type) - creator = payload.get('action').get('memberCreator').get('fullName') + creator = payload['action']['memberCreator']['fullName'] return TRELLO_MESSAGE_TEMPLATE.format(full_name=creator, rest=message_body) def get_managed_member_body(payload, action_type): # type: (Mapping[str, Any], Text) -> Text data = { - 'member_name': payload.get('action').get('member').get('fullName'), + 'member_name': payload['action']['member']['fullName'], } return fill_appropriate_message_content(payload, action_type, data) def get_create_list_body(payload, action_type): # type: (Mapping[str, Any], Text) -> Text data = { - 'list_name': get_action_data(payload).get('list').get('name'), + 'list_name': get_action_data(payload)['list']['name'], } return fill_appropriate_message_content(payload, action_type, data) def get_change_name_body(payload, action_type): # type: (Mapping[str, Any], Text) -> Text data = { - 'old_name': get_action_data(payload).get('old').get('name'), + 'old_name': get_action_data(payload)['old']['name'] } return fill_appropriate_message_content(payload, action_type, data) @@ -85,11 +85,11 @@ def get_filled_board_url_template(payload): def get_board_name(payload): # type: (Mapping[str, Any]) -> Text - return get_action_data(payload).get('board').get('name') + return get_action_data(payload)['board']['name'] def get_board_url(payload): # type: (Mapping[str, Any]) -> Text - return u'https://trello.com/b/{}'.format(get_action_data(payload).get('board').get('shortLink')) + return u'https://trello.com/b/{}'.format(get_action_data(payload)['board']['shortLink']) def get_message_body(action_type): # type: (Text) -> Text @@ -97,7 +97,7 @@ def get_message_body(action_type): def get_action_data(payload): # type: (Mapping[str, Any]) -> Mapping[str, Any] - return payload.get('action').get('data') + return payload['action']['data'] ACTIONS_TO_FILL_BODY_MAPPER = { REMOVE_MEMBER: get_managed_member_body, diff --git a/zproject/backends.py b/zproject/backends.py index 2f70059945..fac2d0c0e7 100644 --- a/zproject/backends.py +++ b/zproject/backends.py @@ -355,7 +355,7 @@ class ZulipRemoteUserBackend(RemoteUserBackend): create_unknown_user = False def authenticate(self, remote_user, realm_subdomain=None): - # type: (str, Optional[Text]) -> Optional[UserProfile] + # type: (Optional[str], Optional[Text]) -> Optional[UserProfile] if not remote_user: return None @@ -375,23 +375,23 @@ class ZulipLDAPException(Exception): class ZulipLDAPAuthBackendBase(ZulipAuthMixin, LDAPBackend): # Don't use Django LDAP's permissions functions def has_perm(self, user, perm, obj=None): - # type: (UserProfile, Any, Any) -> bool + # type: (Optional[UserProfile], Any, Any) -> bool # Using Any type is safe because we are not doing anything with # the arguments. return False def has_module_perms(self, user, app_label): - # type: (UserProfile, str) -> bool + # type: (Optional[UserProfile], Optional[str]) -> bool return False def get_all_permissions(self, user, obj=None): - # type: (UserProfile, Any) -> Set + # type: (Optional[UserProfile], Any) -> Set # Using Any type is safe because we are not doing anything with # the arguments. return set() def get_group_permissions(self, user, obj=None): - # type: (UserProfile, Any) -> Set + # type: (Optional[UserProfile], Any) -> Set # Using Any type is safe because we are not doing anything with # the arguments. return set()