This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix typing for @cached
wrapped functions
#8240
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be40e7f
Fixup typing in cache descriptors
erikjohnston e15f6f6
Pass return types through cached descriptors
erikjohnston f999af4
Add mypy plugin to correctly configure types for @cached
erikjohnston 00bca5f
Newsfile
erikjohnston 4a1bacc
Apply suggestions from code review
erikjohnston 4714ecb
Add type hints to decorator args
erikjohnston a7d3acb
Add some comments to plugin.
erikjohnston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix type hints for functions decorated with `@cached`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2020 The Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Callable, Optional | ||
|
||
from mypy.plugin import MethodSigContext, Plugin | ||
from mypy.typeops import bind_self | ||
from mypy.types import CallableType | ||
|
||
|
||
class SynapsePlugin(Plugin): | ||
def get_method_signature_hook( | ||
self, fullname: str | ||
) -> Optional[Callable[[MethodSigContext], CallableType]]: | ||
if fullname.startswith( | ||
"synapse.util.caches.descriptors._CachedFunction.__call__" | ||
): | ||
return cached_function_method_signature | ||
return None | ||
|
||
|
||
def cached_function_method_signature(ctx: MethodSigContext) -> CallableType: | ||
"""Fixes the `_CachedFunction.__call__` signature to be correct. | ||
|
||
It already has *almost* the correct signature, except: | ||
|
||
1. the `self` argument needs to be marked as "bound"; and | ||
2. any `cache_context` argument should be removed. | ||
""" | ||
|
||
# First we mark this as a bound function signature. | ||
signature = bind_self(ctx.default_signature) | ||
|
||
# Secondly, we remove any "cache_context" args. | ||
# | ||
# Note: We should be only doing this if `cache_context=True` is set, but if | ||
# it isn't then the code will raise an exception when its called anyway, so | ||
# its not the end of the world. | ||
context_arg_index = None | ||
for idx, name in enumerate(signature.arg_names): | ||
if name == "cache_context": | ||
context_arg_index = idx | ||
break | ||
|
||
if context_arg_index: | ||
arg_types = list(signature.arg_types) | ||
arg_types.pop(context_arg_index) | ||
|
||
arg_names = list(signature.arg_names) | ||
arg_names.pop(context_arg_index) | ||
|
||
arg_kinds = list(signature.arg_kinds) | ||
arg_kinds.pop(context_arg_index) | ||
|
||
signature = signature.copy_modified( | ||
arg_types=arg_types, arg_names=arg_names, arg_kinds=arg_kinds, | ||
) | ||
|
||
return signature | ||
|
||
|
||
def plugin(version: str): | ||
# ignore version argument if the plugin works with all mypy versions. | ||
return SynapsePlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -440,11 +440,11 @@ async def _get_missing_events_for_pdu(self, origin, pdu, prevs, min_depth): | |
if not prevs - seen: | ||
return | ||
|
||
latest = await self.store.get_latest_event_ids_in_room(room_id) | ||
latest_list = await self.store.get_latest_event_ids_in_room(room_id) | ||
|
||
# We add the prev events that we have seen to the latest | ||
# list to ensure the remote server doesn't give them to us | ||
latest = set(latest) | ||
latest = set(latest_list) | ||
latest |= seen | ||
|
||
logger.info( | ||
|
@@ -781,7 +781,7 @@ async def _process_received_pdu( | |
# keys across all devices. | ||
current_keys = [ | ||
key | ||
for device in cached_devices | ||
for device in cached_devices.values() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might have just been a plain bug, though I don't see it getting hit on matrix.org :/ |
||
for key in device.get("keys", {}).get("keys", {}).values() | ||
] | ||
|
||
|
@@ -2119,8 +2119,8 @@ async def _check_for_soft_fail( | |
if backfilled or event.internal_metadata.is_outlier(): | ||
return | ||
|
||
extrem_ids = await self.store.get_latest_event_ids_in_room(event.room_id) | ||
extrem_ids = set(extrem_ids) | ||
extrem_ids_list = await self.store.get_latest_event_ids_in_room(event.room_id) | ||
extrem_ids = set(extrem_ids_list) | ||
prev_event_ids = set(event.prev_event_ids()) | ||
|
||
if extrem_ids == prev_event_ids: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This vaguely feels like how
overload
works, but probably isn't worth the trouble figuring out here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, its entirely doable I think by threading through the
cache_context
literal, but yeah, it's not worth it.