1
0

Compare commits

...

4 Commits

Author SHA1 Message Date
Olivier Wilkinson (reivilibre)
9064a424d6 Appease runtime 2022-05-25 12:50:05 +01:00
Olivier Wilkinson (reivilibre)
e785ba8c5d Newsfile
Signed-off-by: Olivier Wilkinson (reivilibre) <oliverw@matrix.org>
2022-05-25 12:43:22 +01:00
Olivier Wilkinson (reivilibre)
7a537c3f43 Make get_global give you a return value with a stronger type 2022-05-25 12:41:35 +01:00
Olivier Wilkinson (reivilibre)
7f14735fb2 Introduce a manually-unrolled FrozenJsonDict type 2022-05-25 12:41:03 +01:00
3 changed files with 32 additions and 1 deletions

1
changelog.d/12874.misc Normal file
View File

@@ -0,0 +1 @@
Add stricter JSON type annotations to the Module API's account data manager.

View File

@@ -112,6 +112,7 @@ from synapse.storage.databases.main.roommember import ProfileInfo
from synapse.storage.state import StateFilter
from synapse.types import (
DomainSpecificString,
FrozenJsonDict,
JsonDict,
JsonMapping,
Requester,
@@ -1498,7 +1499,9 @@ class AccountDataManager:
f"{user_id} is not local to this homeserver; can't access account data for remote users."
)
async def get_global(self, user_id: str, data_type: str) -> Optional[JsonMapping]:
async def get_global(
self, user_id: str, data_type: str
) -> Optional[FrozenJsonDict]:
"""
Gets some global account data, of a specified type, for the specified user.

View File

@@ -77,6 +77,33 @@ JsonMapping = Mapping[str, Any]
JsonSerializable = object
# Mypy doesn't support recursive type definitions, so unroll one manually a few
# times. This makes this alias not entirely general-purpose, but it can be
# used for some basic sanity checking in e.g. module API return values.
# Bottom out at `object` so that the caller knows they aren't getting any more
# checking; they can always use casts if necessary.
_FrozenJson1 = Union[
str, int, float, None, "frozendict[str, object]", Tuple[object, ...]
]
_FrozenJson2 = Union[
str, int, float, None, "frozendict[str, _FrozenJson1]", Tuple[_FrozenJson1, ...]
]
_FrozenJson3 = Union[
str, int, float, None, "frozendict[str, _FrozenJson2]", Tuple[_FrozenJson2, ...]
]
# FrozenJson supports JSON-like data, but only using frozen data structures.
# (frozendicts instead of dicts and tuples instead of lists)
FrozenJson = _FrozenJson3
if TYPE_CHECKING:
# frozendict isn't subscriptable.
FrozenJsonDict = frozendict[str, _FrozenJson3]
else:
FrozenJsonDict = frozendict[str, Any]
# Note that this seems to require inheriting *directly* from Interface in order
# for mypy-zope to realize it is an interface.
class ISynapseReactor(