mypy: Finalize migration of cache.py to python3 function annotation.

- Use forward declarations of some types from models.py to avoid cycles.
- Remove cache.py from linter rule exclude list to ensure it stays that way.
This commit is contained in:
neiljp (Neil Pilgrim)
2018-03-16 16:51:05 +00:00
committed by Tim Abbott
parent 005cb6bd03
commit 966ca7015f
2 changed files with 8 additions and 17 deletions

View File

@@ -477,7 +477,6 @@ def build_custom_checkers(by_lang):
# Zerver files that we should just clean. # Zerver files that we should just clean.
'zerver/tests', 'zerver/tests',
'zerver/lib/api_test_helpers.py', 'zerver/lib/api_test_helpers.py',
'zerver/lib/cache.py',
'zerver/lib/request.py', 'zerver/lib/request.py',
'zerver/views/streams.py', 'zerver/views/streams.py',
# thumbor is (currently) python2 only # thumbor is (currently) python2 only

View File

@@ -303,12 +303,10 @@ def user_profile_by_email_cache_key(email: Text) -> Text:
# with high likelihood be ASCII-only for the foreseeable future. # with high likelihood be ASCII-only for the foreseeable future.
return 'user_profile_by_email:%s' % (make_safe_digest(email.strip()),) return 'user_profile_by_email:%s' % (make_safe_digest(email.strip()),)
def user_profile_cache_key_id(email, realm_id): def user_profile_cache_key_id(email: Text, realm_id: int) -> Text:
# type: (Text, int) -> Text
return u"user_profile:%s:%s" % (make_safe_digest(email.strip()), realm_id,) return u"user_profile:%s:%s" % (make_safe_digest(email.strip()), realm_id,)
def user_profile_cache_key(email, realm): def user_profile_cache_key(email: Text, realm: 'Realm') -> Text:
# type: (Text, Realm) -> Text
return user_profile_cache_key_id(email, realm.id) return user_profile_cache_key_id(email, realm.id)
def bot_profile_cache_key(email: Text) -> Text: def bot_profile_cache_key(email: Text) -> Text:
@@ -342,16 +340,14 @@ bot_dict_fields = ['id', 'full_name', 'short_name', 'bot_type', 'email',
'bot_owner__email', 'avatar_source', 'bot_owner__email', 'avatar_source',
'avatar_version'] # type: List[str] 'avatar_version'] # type: List[str]
def bot_dicts_in_realm_cache_key(realm): def bot_dicts_in_realm_cache_key(realm: 'Realm') -> Text:
# type: (Realm) -> Text
return "bot_dicts_in_realm:%s" % (realm.id,) return "bot_dicts_in_realm:%s" % (realm.id,)
def get_stream_cache_key(stream_name: Text, realm_id: int) -> Text: def get_stream_cache_key(stream_name: Text, realm_id: int) -> Text:
return "stream_by_realm_and_name:%s:%s" % ( return "stream_by_realm_and_name:%s:%s" % (
realm_id, make_safe_digest(stream_name.strip().lower())) realm_id, make_safe_digest(stream_name.strip().lower()))
def delete_user_profile_caches(user_profiles): def delete_user_profile_caches(user_profiles: Iterable['UserProfile']) -> None:
# type: (Iterable[UserProfile]) -> None
keys = [] keys = []
for user_profile in user_profiles: for user_profile in user_profiles:
keys.append(user_profile_by_email_cache_key(user_profile.email)) keys.append(user_profile_by_email_cache_key(user_profile.email))
@@ -361,8 +357,7 @@ def delete_user_profile_caches(user_profiles):
cache_delete_many(keys) cache_delete_many(keys)
def delete_display_recipient_cache(user_profile): def delete_display_recipient_cache(user_profile: 'UserProfile') -> None:
# type: (UserProfile) -> None
from zerver.models import Subscription # We need to import here to avoid cyclic dependency. from zerver.models import Subscription # We need to import here to avoid cyclic dependency.
recipient_ids = Subscription.objects.filter(user_profile=user_profile) recipient_ids = Subscription.objects.filter(user_profile=user_profile)
recipient_ids = recipient_ids.values_list('recipient_id', flat=True) recipient_ids = recipient_ids.values_list('recipient_id', flat=True)
@@ -427,12 +422,10 @@ def flush_realm(sender: Any, **kwargs: Any) -> None:
cache_delete(bot_dicts_in_realm_cache_key(realm)) cache_delete(bot_dicts_in_realm_cache_key(realm))
cache_delete(realm_alert_words_cache_key(realm)) cache_delete(realm_alert_words_cache_key(realm))
def realm_alert_words_cache_key(realm): def realm_alert_words_cache_key(realm: 'Realm') -> Text:
# type: (Realm) -> Text
return "realm_alert_words:%s" % (realm.string_id,) return "realm_alert_words:%s" % (realm.string_id,)
def realm_first_visible_message_id_cache_key(realm): def realm_first_visible_message_id_cache_key(realm: 'Realm') -> Text:
# type: (Realm) -> Text
return u"realm_first_visible_message_id:%s" % (realm.string_id,) return u"realm_first_visible_message_id:%s" % (realm.string_id,)
# Called by models.py to flush the stream cache whenever we save a stream # Called by models.py to flush the stream cache whenever we save a stream
@@ -453,8 +446,7 @@ def flush_stream(sender: Any, **kwargs: Any) -> None:
def to_dict_cache_key_id(message_id: int) -> Text: def to_dict_cache_key_id(message_id: int) -> Text:
return 'message_dict:%d' % (message_id,) return 'message_dict:%d' % (message_id,)
def to_dict_cache_key(message): def to_dict_cache_key(message: 'Message') -> Text:
# type: (Message) -> Text
return to_dict_cache_key_id(message.id) return to_dict_cache_key_id(message.id)
def flush_message(sender: Any, **kwargs: Any) -> None: def flush_message(sender: Any, **kwargs: Any) -> None: