Skip to content
Snippets Groups Projects
Unverified Commit 5331fb5b authored by David Robertson's avatar David Robertson Committed by GitHub
Browse files

allow `on_invalidate=None` in `@cached` methods (#12769)

parent 6edefef6
Branches
Tags
No related merge requests found
Tweak the mypy plugin so that `@cached` can accept `on_invalidate=None`.
...@@ -21,7 +21,7 @@ from typing import Callable, Optional, Type ...@@ -21,7 +21,7 @@ from typing import Callable, Optional, Type
from mypy.nodes import ARG_NAMED_OPT from mypy.nodes import ARG_NAMED_OPT
from mypy.plugin import MethodSigContext, Plugin from mypy.plugin import MethodSigContext, Plugin
from mypy.typeops import bind_self from mypy.typeops import bind_self
from mypy.types import CallableType, NoneType from mypy.types import CallableType, NoneType, UnionType
class SynapsePlugin(Plugin): class SynapsePlugin(Plugin):
...@@ -72,13 +72,20 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType: ...@@ -72,13 +72,20 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
# Third, we add an optional "on_invalidate" argument. # Third, we add an optional "on_invalidate" argument.
# #
# This is a callable which accepts no input and returns nothing. # This is a either
calltyp = CallableType( # - a callable which accepts no input and returns nothing, or
arg_types=[], # - None.
arg_kinds=[], calltyp = UnionType(
arg_names=[], [
ret_type=NoneType(), NoneType(),
fallback=ctx.api.named_generic_type("builtins.function", []), CallableType(
arg_types=[],
arg_kinds=[],
arg_names=[],
ret_type=NoneType(),
fallback=ctx.api.named_generic_type("builtins.function", []),
),
]
) )
arg_types.append(calltyp) arg_types.append(calltyp)
...@@ -95,7 +102,7 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType: ...@@ -95,7 +102,7 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
def plugin(version: str) -> Type[SynapsePlugin]: def plugin(version: str) -> Type[SynapsePlugin]:
# This is the entry point of the plugin, and let's us deal with the fact # 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 # that the mypy plugin interface is *not* stable by looking at the version
# string. # string.
# #
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
import logging import logging
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
Callable,
Collection, Collection,
Dict, Dict,
FrozenSet, FrozenSet,
...@@ -634,7 +635,7 @@ class RoomMemberWorkerStore(EventsWorkerStore): ...@@ -634,7 +635,7 @@ class RoomMemberWorkerStore(EventsWorkerStore):
) )
async def get_rooms_for_user( async def get_rooms_for_user(
self, user_id: str, on_invalidate=None self, user_id: str, on_invalidate: Optional[Callable[[], None]] = None
) -> FrozenSet[str]: ) -> FrozenSet[str]:
"""Returns a set of room_ids the user is currently joined to. """Returns a set of room_ids the user is currently joined to.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment