cache: Fix typing for generic_bulk_cached_fetch.

The typing for generic_bulk_cached_fetch is complicated, and was
recorded incorrectly previously for the case where a cache_transformer
function is required.  We fix this by adding the new CacheItemT, and
additionally add comments explaining what's going on with these types
for future reference.

Thanks to Mateusz Mandera for raising this issue.
This commit is contained in:
Tim Abbott
2019-08-08 12:34:06 -07:00
parent 2ada0a9bad
commit 27a0e307b6
4 changed files with 45 additions and 20 deletions

View File

@@ -124,13 +124,16 @@ def bulk_get_users(emails: List[str], realm: Optional[Realm],
where=[where_clause],
params=emails)
def user_to_email(user_profile: UserProfile) -> str:
return user_profile.email.lower()
return generic_bulk_cached_fetch(
# Use a separate cache key to protect us from conflicts with
# the get_user cache.
lambda email: 'bulk_get_users:' + user_profile_cache_key_id(email, realm_id),
fetch_users_by_email,
[email.lower() for email in emails],
id_fetcher=lambda user_profile: user_profile.email.lower()
id_fetcher=user_to_email,
)
def user_ids_to_users(user_ids: List[int], realm: Realm) -> List[UserProfile]: