Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

mypy plugin to check @cached return types #14911

Merged
merged 44 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
eb24cdb
WIP mypy plugin to check `@cached` return types
Jan 25, 2023
d2cbac3
Whoops, Sequence is immutable
Jan 31, 2023
90631ac
WIP
Jan 31, 2023
4eb7de9
Merge remote-tracking branch 'origin/develop' into dmr/mypy-check-at-…
clokep Sep 12, 2023
ad1c28a
Update comments.
clokep Sep 12, 2023
88323dd
Simplify code due to other knowledge.
clokep Sep 12, 2023
676c858
Treat containers more similarly.
clokep Sep 12, 2023
008ef3f
cachedList wraps Mapping
clokep Sep 12, 2023
8aa4e87
Fix-up errors in tests.
clokep Sep 13, 2023
0f3c036
Ignore a few calls which purposefully (?) return mutable objects.
clokep Sep 13, 2023
9c94574
Data exfilitration is read-only and update admin APIs.
clokep Sep 13, 2023
f5fec7f
Update account_data & tags methods to be immutable.
clokep Sep 13, 2023
9a62053
FIx-up push related caching.
clokep Sep 13, 2023
cc61862
Update filtering to return immutable objects.
clokep Sep 13, 2023
a27a67f
Update relations with immutable.
clokep Sep 13, 2023
8f7f4d7
Update receipts code.
clokep Sep 13, 2023
9fdc5a1
Update e2e keys & devices.
clokep Sep 13, 2023
d8cce5b
Update appservice stuff.
clokep Sep 13, 2023
ef61f3d
Ensure current hosts is immutable.
clokep Sep 13, 2023
96faa34
Properly check attrs for frozen-ness.
clokep Sep 13, 2023
2f75929
Kick CI
clokep Sep 14, 2023
7849b26
Merge remote-tracking branch 'origin/develop' into dmr/mypy-check-at-…
clokep Sep 14, 2023
8b1b15b
Fix-up return value of get_latest_event_ids_in_room.
clokep Sep 14, 2023
451c9b1
Revert "Kick CI"
clokep Sep 14, 2023
d52e30c
Newsfragment
clokep Sep 14, 2023
47b7ba7
FIx-up sync changes.
clokep Sep 14, 2023
ee77d82
Merge remote-tracking branch 'origin/develop' into dmr/mypy-check-at-…
clokep Sep 18, 2023
0f02ad1
Merge remote-tracking branch 'origin/develop' into dmr/mypy-check-at-…
clokep Sep 19, 2023
51a1a5f
Merge remote-tracking branch 'origin/develop' into dmr/mypy-check-at-…
clokep Sep 20, 2023
07c4531
Merge branch 'develop' into dmr/mypy-check-at-cached
clokep Sep 25, 2023
745ad61
Correct context
erikjohnston Sep 29, 2023
03b0e40
Remove ignores for call-sites.
clokep Sep 29, 2023
2c07b1f
Merge remote-tracking branch 'origin/develop' into dmr/mypy-check-at-…
clokep Sep 29, 2023
460ed3c
Add ignores at definition sites.
clokep Sep 29, 2023
fbecb56
Actually check cachedList.
clokep Sep 29, 2023
dba8e72
Fix incorrect generic.
clokep Sep 29, 2023
4f06d85
Lint
clokep Sep 29, 2023
3875662
Abstract shared code.
clokep Sep 29, 2023
fb4ff5d
ServerAclEvaluator is immutable.
clokep Sep 29, 2023
06ddf65
Update comments.
clokep Sep 29, 2023
9b7ee03
Lint
clokep Sep 29, 2023
e2f599d
Update comments and remove unnecessary argument.
clokep Oct 2, 2023
da377cf
Merge remote-tracking branch 'origin/develop' into dmr/mypy-check-at-…
clokep Oct 2, 2023
a2956a6
Fix duplicate word.
clokep Oct 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/14911.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
171 changes: 163 additions & 8 deletions scripts-dev/mypy_synapse_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@
can crop up, e.g the cache descriptors.
"""

from typing import Callable, Optional, Type
from typing import Callable, Optional, Tuple, Type

import mypy.types
from mypy.erasetype import remove_instance_last_known_values
from mypy.nodes import ARG_NAMED_OPT
from mypy.errorcodes import ErrorCode
from mypy.nodes import ARG_NAMED_OPT, Var
from mypy.plugin import MethodSigContext, Plugin
from mypy.typeops import bind_self
from mypy.types import CallableType, Instance, NoneType, UnionType
from mypy.types import (
AnyType,
CallableType,
Instance,
NoneType,
TupleType,
TypeAliasType,
UninhabitedType,
UnionType,
)


class SynapsePlugin(Plugin):
Expand All @@ -49,10 +60,10 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
3. an optional keyword argument `on_invalidated` should be added.
"""

# First we mark this as a bound function signature.
signature = bind_self(ctx.default_signature)
# 1. Mark this as a bound function signature.
signature: CallableType = bind_self(ctx.default_signature)

# Secondly, we remove any "cache_context" args.
# 2. 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
Expand All @@ -72,7 +83,7 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
arg_names.pop(context_arg_index)
arg_kinds.pop(context_arg_index)

# Third, we add an optional "on_invalidate" argument.
# 3. Add an optional "on_invalidate" argument.
#
# This is a either
# - a callable which accepts no input and returns nothing, or
Expand All @@ -94,7 +105,7 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
arg_names.append("on_invalidate")
arg_kinds.append(ARG_NAMED_OPT) # Arg is an optional kwarg.

# Finally we ensure the return type is a Deferred.
# 4. Ensure the return type is a Deferred.
if (
isinstance(signature.ret_type, Instance)
and signature.ret_type.type.fullname == "twisted.internet.defer.Deferred"
Expand Down Expand Up @@ -131,9 +142,153 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
ret_type=ret_type,
)

# 5. Complain loudly if we are returning something mutable
check_is_cacheable(signature, ctx, ret_type)

return signature


def check_is_cacheable(
signature: CallableType,
ctx: MethodSigContext,
deferred_return_type: Instance,
) -> None:
# The previous code wraps the return type into a Deferred.
assert deferred_return_type.type.fullname == "twisted.internet.defer.Deferred"
return_type = deferred_return_type.args[0]

verbose = ctx.api.options.verbosity >= 1
# TODO Technically a cachedList only needs immutable values, but forcing them
# to return Mapping instead of Dict is fine.
ok, note = is_cacheable(return_type, signature, verbose)

if ok:
message = f"function {signature.name} is @cached, returning {return_type}"
else:
message = f"function {signature.name} is @cached, but has mutable return value {return_type}"

if note:
message += f" ({note})"
message = message.replace("builtins.", "").replace("typing.", "")

# TODO The context is the context of the caller, not the method itself.
if ok and note:
ctx.api.note(message, ctx.context) # type: ignore[attr-defined]
elif not ok:
ctx.api.fail(message, ctx.context, code=AT_CACHED_MUTABLE_RETURN)


# Immutable simple values.
IMMUTABLE_VALUE_TYPES = {
"builtins.bool",
"builtins.int",
"builtins.float",
"builtins.str",
"builtins.bytes",
}

# Types defined in Synapse which are known to be immutable.
IMMUTABLE_CUSTOM_TYPES = {
"synapse.synapse_rust.push.FilteredPushRules",
# This is technically not immutable, but close enough.
"signedjson.types.VerifyKey",
}

# Immutable containers only if the values are also immutable.
IMMUTABLE_CONTAINER_TYPES_REQUIRING_IMMUTABLE_ELEMENTS = {
"builtins.frozenset",
"builtins.tuple",
"typing.AbstractSet",
"typing.Sequence",
"immutabledict.immutabledict",
}

MUTABLE_CONTAINER_TYPES = {
"builtins.set",
"builtins.list",
"builtins.dict",
}

AT_CACHED_MUTABLE_RETURN = ErrorCode(
"synapse-@cached-mutable",
"@cached() should have an immutable return type",
"General",
)


def is_cacheable(
rt: mypy.types.Type, signature: CallableType, verbose: bool
) -> Tuple[bool, Optional[str]]:
"""
Returns: a 2-tuple (cacheable, message).
- cachable: False means the type is definitely not cacheable;
true means anything else.
- Optional message.
"""

# This should probably be done via a TypeVisitor. Apologies to the reader!
if isinstance(rt, AnyType):
return True, ("may be mutable" if verbose else None)

elif isinstance(rt, Instance):
if (
rt.type.fullname in IMMUTABLE_VALUE_TYPES
or rt.type.fullname in IMMUTABLE_CUSTOM_TYPES
):
return True, None

elif rt.type.fullname == "typing.Mapping":
return is_cacheable(rt.args[1], signature, verbose)

elif rt.type.fullname in IMMUTABLE_CONTAINER_TYPES_REQUIRING_IMMUTABLE_ELEMENTS:
# E.g. Collection[T] is cachable iff T is cachable.
return is_cacheable(rt.args[0], signature, verbose)

elif rt.type.fullname in MUTABLE_CONTAINER_TYPES:
return False, None

elif "attrs" in rt.type.metadata:
frozen = rt.type.metadata["attrs"]["frozen"]
if frozen:
for attribute in rt.type.metadata["attrs"]["attributes"]:
attribute_name = attribute["name"]
symbol_node = rt.type.names[attribute_name].node
assert isinstance(symbol_node, Var)
assert symbol_node.type is not None
ok, note = is_cacheable(symbol_node.type, signature, verbose)
if not ok:
return False, f"non-frozen attrs property: {attribute_name}"
# All attributes were frozen.
return True, None
else:
return False, "non-frozen attrs class"

else:
return False, f"Don't know how to handle {rt.type.fullname}"

elif isinstance(rt, NoneType):
return True, None

elif isinstance(rt, (TupleType, UnionType)):
for item in rt.items:
ok, note = is_cacheable(item, signature, verbose)
if not ok:
return False, note
# This discards notes but that's probably fine
return True, None

elif isinstance(rt, TypeAliasType):
return is_cacheable(mypy.types.get_proper_type(rt), signature, verbose)

# The tests check what happens if you raise an Exception, so they don't return.
elif isinstance(rt, UninhabitedType) and rt.is_noreturn:
# There's no return value, just consider it cachable.
return True, None

else:
return False, f"Don't know how to handle {type(rt).__qualname__} return type"


def plugin(version: str) -> Type[SynapsePlugin]:
# This is the entry point of the plugin, and lets us deal with the fact
# that the mypy plugin interface is *not* stable by looking at the version
Expand Down
2 changes: 1 addition & 1 deletion synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ async def on_send_join_request(
state_event_ids: Collection[str]
servers_in_room: Optional[Collection[str]]
if caller_supports_partial_state:
summary = await self.store.get_room_summary(room_id)
summary = await self.store.get_room_summary(room_id) # type: ignore[synapse-@cached-mutable]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exciting to see!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terrifying IMO of how we use this.

clokep marked this conversation as resolved.
Show resolved Hide resolved
state_event_ids = _get_event_ids_for_partial_state_join(
event, prev_state_ids, summary
)
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ async def _get_threads_for_events(
event_ids = [eid for eid in events_by_id.keys() if eid not in relations_by_id]

# Fetch thread summaries.
summaries = await self._main_store.get_thread_summaries(event_ids)
summaries = await self._main_store.get_thread_summaries(event_ids) # type: ignore[synapse-@cached-mutable]

# Limit fetching whether the requester has participated in a thread to
# events which are thread roots.
Expand Down Expand Up @@ -514,7 +514,7 @@ async def _fetch_edits() -> None:
Note that there is no use in limiting edits by ignored users since the
parent event should be ignored in the first place if the user is ignored.
"""
edits = await self._main_store.get_applicable_edits(
edits = await self._main_store.get_applicable_edits( # type: ignore[synapse-@cached-mutable]
[
event_id
for event_id, event in events_by_id.items()
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ async def compute_summary(
)

# this is heavily cached, thus: fast.
details = await self.store.get_room_summary(room_id)
details = await self.store.get_room_summary(room_id) # type: ignore[synapse-@cached-mutable]

name_id = state_ids.get((EventTypes.Name, ""))
canonical_alias_id = state_ids.get((EventTypes.CanonicalAlias, ""))
Expand Down Expand Up @@ -1336,7 +1336,7 @@ async def unread_notifs_for_room_id(
return RoomNotifCounts.empty()

with Measure(self.clock, "unread_notifs_for_room_id"):
return await self.store.get_unread_event_push_actions_by_room_for_user(
return await self.store.get_unread_event_push_actions_by_room_for_user( # type: ignore[synapse-@cached-mutable]
room_id,
sync_config.user.to_string(),
)
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/controllers/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ async def _get_joined_hosts(
# `get_joined_hosts` is called with the "current" state group for the
# room, and so consecutive calls will be for consecutive state groups
# which point to the previous state group.
cache = await self.stores.main._get_joined_hosts_cache(room_id)
cache = await self.stores.main._get_joined_hosts_cache(room_id) # type: ignore[synapse-@cached-mutable]

# If the state group in the cache matches, we already have the data we need.
if state_entry.state_group == cache.state_group:
Expand Down
5 changes: 3 additions & 2 deletions synapse/storage/databases/main/event_push_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class UserPushAction(EmailPushAction):
profile_tag: str


# TODO This is used as a cached value and is mutable.
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
@attr.s(slots=True, auto_attribs=True)
class NotifCounts:
"""
Expand All @@ -193,15 +194,15 @@ class NotifCounts:
highlight_count: int = 0


@attr.s(slots=True, auto_attribs=True)
@attr.s(slots=True, frozen=True, auto_attribs=True)
class RoomNotifCounts:
"""
The per-user, per-room count of notifications. Used by sync and push.
"""

main_timeline: NotifCounts
# Map of thread ID to the notification counts.
threads: Dict[str, NotifCounts]
threads: Mapping[str, NotifCounts]

@staticmethod
def empty() -> "RoomNotifCounts":
Expand Down
2 changes: 2 additions & 0 deletions synapse/storage/databases/main/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ def _get_references_for_events_txn(
def get_applicable_edit(self, event_id: str) -> Optional[EventBase]:
raise NotImplementedError()

# TODO: This returns a mutable object, which is generally bad.
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
@cachedList(cached_method_name="get_applicable_edit", list_name="event_ids")
async def get_applicable_edits(
self, event_ids: Collection[str]
Expand Down Expand Up @@ -602,6 +603,7 @@ def _get_applicable_edits_txn(txn: LoggingTransaction) -> Dict[str, str]:
def get_thread_summary(self, event_id: str) -> Optional[Tuple[int, EventBase]]:
raise NotImplementedError()

# TODO: This returns a mutable object, which is generally bad.
@cachedList(cached_method_name="get_thread_summary", list_name="event_ids")
async def get_thread_summaries(
self, event_ids: Collection[str]
Expand Down
1 change: 1 addition & 0 deletions synapse/storage/databases/main/roommember.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ async def _get_approximate_current_memberships_in_room(
)
return {row["event_id"]: row["membership"] for row in rows}

# TODO This returns a mutable object, which is generally confusing when using a cache.
@cached(max_entries=10000)
def _get_joined_hosts_cache(self, room_id: str) -> "_JoinedHostsCache":
return _JoinedHostsCache()
Expand Down
1 change: 1 addition & 0 deletions synapse/storage/roommember.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ProfileInfo:
display_name: Optional[str]


# TODO This is used as a cached value and is mutable.
@attr.s(slots=True, frozen=True, weakref_slot=False, auto_attribs=True)
class MemberSummary:
Comment on lines +48 to 50
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it? frozen=True here. Oh, but members is a List.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is due to members. The code is hard to refactor to make that immutable, unfortunately.

# A truncated list of (user_id, event_id) tuples for users of a given
Expand Down
2 changes: 1 addition & 1 deletion tests/rest/admin/test_server_notice.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def test_send_server_notice_delete_room(self) -> None:

# It doesn't really matter what API we use here, we just want to assert
# that the room doesn't exist.
summary = self.get_success(self.store.get_room_summary(first_room_id))
summary = self.get_success(self.store.get_room_summary(first_room_id)) # type: ignore[synapse-@cached-mutable]
# The summary should be empty since the room doesn't exist.
self.assertEqual(summary, {})

Expand Down
2 changes: 1 addition & 1 deletion tests/rest/client/test_shadow_banned.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_upgrade(self) -> None:

# It doesn't really matter what API we use here, we just want to assert
# that the room doesn't exist.
summary = self.get_success(self.store.get_room_summary(new_room_id))
summary = self.get_success(self.store.get_room_summary(new_room_id)) # type: ignore[synapse-@cached-mutable]
# The summary should be empty since the room doesn't exist.
self.assertEqual(summary, {})

Expand Down
Loading