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

Commit 5331fb5

Browse files
author
David Robertson
authored
allow on_invalidate=None in @cached methods (#12769)
1 parent 6edefef commit 5331fb5

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

changelog.d/12769.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tweak the mypy plugin so that `@cached` can accept `on_invalidate=None`.

scripts-dev/mypy_synapse_plugin.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from mypy.nodes import ARG_NAMED_OPT
2222
from mypy.plugin import MethodSigContext, Plugin
2323
from mypy.typeops import bind_self
24-
from mypy.types import CallableType, NoneType
24+
from mypy.types import CallableType, NoneType, UnionType
2525

2626

2727
class SynapsePlugin(Plugin):
@@ -72,13 +72,20 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
7272

7373
# Third, we add an optional "on_invalidate" argument.
7474
#
75-
# This is a callable which accepts no input and returns nothing.
76-
calltyp = CallableType(
77-
arg_types=[],
78-
arg_kinds=[],
79-
arg_names=[],
80-
ret_type=NoneType(),
81-
fallback=ctx.api.named_generic_type("builtins.function", []),
75+
# This is a either
76+
# - a callable which accepts no input and returns nothing, or
77+
# - None.
78+
calltyp = UnionType(
79+
[
80+
NoneType(),
81+
CallableType(
82+
arg_types=[],
83+
arg_kinds=[],
84+
arg_names=[],
85+
ret_type=NoneType(),
86+
fallback=ctx.api.named_generic_type("builtins.function", []),
87+
),
88+
]
8289
)
8390

8491
arg_types.append(calltyp)
@@ -95,7 +102,7 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
95102

96103

97104
def plugin(version: str) -> Type[SynapsePlugin]:
98-
# This is the entry point of the plugin, and let's us deal with the fact
105+
# This is the entry point of the plugin, and lets us deal with the fact
99106
# that the mypy plugin interface is *not* stable by looking at the version
100107
# string.
101108
#

synapse/storage/databases/main/roommember.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import logging
1616
from typing import (
1717
TYPE_CHECKING,
18+
Callable,
1819
Collection,
1920
Dict,
2021
FrozenSet,
@@ -634,7 +635,7 @@ def _get_users_server_still_shares_room_with_txn(
634635
)
635636

636637
async def get_rooms_for_user(
637-
self, user_id: str, on_invalidate=None
638+
self, user_id: str, on_invalidate: Optional[Callable[[], None]] = None
638639
) -> FrozenSet[str]:
639640
"""Returns a set of room_ids the user is currently joined to.
640641

0 commit comments

Comments
 (0)